首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Base64摘要+ PFX(PKCS12) -> ETSI.CAdES.detached签名-> PAdES LTV

Base64摘要+ PFX(PKCS12) -> ETSI.CAdES.detached签名-> PAdES LTV
EN

Stack Overflow用户
提问于 2022-02-22 17:26:38
回答 1查看 604关注 0票数 0

我有一个创建Base64摘要的API。现在,我想要创建另一个API,它使用这个摘要和PFX,并创建一个ETSI.CAdES.detached签名和LTV信息(Certs链,OCSP响应,CRL),我想要嵌入到我的PDF中以获得一个使用第三个API的CAdES - LTV签名(我的第三个API将获取从这个API获得的CAdES签名和LTV信息,并将它们嵌入到我的PDF中).I不知道如何使用该摘要创建这个ETSI.CAdES.detached签名,以及一个带有Java和Bouncy的PFX,我试着遵循本教程github

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-03 12:39:31

正如您声明的那样,您有自己的代码来准备用于签名的PDF文件,并将签名容器注入其中。因此,你的问题基本上可以归结为

如何使用可用于创建CAdES基线B或to签名的BouncyCastle创建一个PAdES签名容器?

iText 7签名框架中的实现

由于我没有您现有的代码,所以我不得不在测试中使用不同的框架。为此,我使用了iText 7签名框架。

BouncyCastle确实包含一个生成CMS签名容器的CMSSignedDataGenerator

不幸的是,其中SignerInfo生成的默认实现并不兼容CAdES/PAdES,因为它不创建带符号的ESSCertIDv2属性。幸运的是,该实现被设计为允许插入自定义属性集。

因此,您可以使用自定义的CAdES创建PAdES基线签名所需的CMSSignedDataGenerator容器。

因此,当你准备好PDF签署时,你可以这样做:

代码语言:javascript
复制
InputStream data = [InputStream containing the PDF byte ranges to sign];
ContentSigner contentSigner = [BouncyCastle ContentSigner for your private key];
X509CertificateHolder x509CertificateHolder = [BouncyCastle X509CertificateHolder for your X.509 signer certificate];

DigestCalculatorProvider digestCalculatorProvider = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();


CMSTypedData msg = new CMSTypedDataInputStream(data);

CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

gen.addSignerInfoGenerator(
        new JcaSignerInfoGeneratorBuilder(digestCalculatorProvider)
                .setSignedAttributeGenerator(new PadesSignedAttributeGenerator())
                .setUnsignedAttributeGenerator(new PadesUnsignedAttributeGenerator())
                .build(contentSigner, x509CertificateHolder));

gen.addCertificates(new JcaCertStore(Collections.singleton(x509CertificateHolder)));

CMSSignedData sigData = gen.generate(msg, false);
byte[] cmsBytes = sigData.getEncoded();

(https://github.com/mkl-public/testarea-itext7/blob/master/src/main/java/mkl/testarea/itext7/signature/PadesSignatureContainerBc.java#L78 sign__)

byte[] cmsBytes包含要注入准备好的PDF签名占位符的字节。

需要下列助手类:

首先,InputStream的包装器包含要由BouncyCastle签名处理的PDF范围。

代码语言:javascript
复制
class CMSTypedDataInputStream implements CMSTypedData {
    InputStream in;

    public CMSTypedDataInputStream(InputStream is) {
        in = is;
    }

    @Override
    public ASN1ObjectIdentifier getContentType() {
        return PKCSObjectIdentifiers.data;
    }

    @Override
    public Object getContent() {
        return in;
    }

    @Override
    public void write(OutputStream out) throws IOException,
            CMSException {
        byte[] buffer = new byte[8 * 1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
    }
}

(https://github.com/mkl-public/testarea-itext7/blob/master/src/main/java/mkl/testarea/itext7/signature/PadesSignatureContainerBc.java#L111助手类CMSTypedDataInputStream__)

然后,为PAdES定制一个签名属性生成器:

代码语言:javascript
复制
class PadesSignedAttributeGenerator implements CMSAttributeTableGenerator {
    @Override
    public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params) throws CMSAttributeTableGenerationException {
        String currentAttribute = null;
        try {
            ASN1EncodableVector signedAttributes = new ASN1EncodableVector();
            currentAttribute = "SigningCertificateAttribute";
            AlgorithmIdentifier digAlgId = (AlgorithmIdentifier) params.get(CMSAttributeTableGenerator.DIGEST_ALGORITHM_IDENTIFIER);
            signedAttributes.add(createSigningCertificateAttribute(digAlgId));
            currentAttribute = "ContentTypeAttribute";
            ASN1ObjectIdentifier contentType = ASN1ObjectIdentifier.getInstance(params.get(CMSAttributeTableGenerator.CONTENT_TYPE));
            signedAttributes.add(new Attribute(CMSAttributes.contentType, new DERSet(contentType)));
            currentAttribute = "MessageDigestAttribute";
            byte[] messageDigest = (byte[])params.get(CMSAttributeTableGenerator.DIGEST);
            signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(messageDigest))));

            return new AttributeTable(signedAttributes);
        } catch (Exception e) {
            throw new CMSAttributeTableGenerationException(currentAttribute, e);
        }
    }

    Attribute createSigningCertificateAttribute(AlgorithmIdentifier digAlg) throws IOException, OperatorCreationException {
        final IssuerSerial issuerSerial = getIssuerSerial();
        DigestCalculator digestCalculator = digestCalculatorProvider.get(digAlg);
        digestCalculator.getOutputStream().write(x509CertificateHolder.getEncoded());
        final byte[] certHash = digestCalculator.getDigest();

        if (OIWObjectIdentifiers.idSHA1.equals(digAlg.getAlgorithm())) {
            final ESSCertID essCertID = new ESSCertID(certHash, issuerSerial);
            SigningCertificate signingCertificate = new SigningCertificate(essCertID);
            return new Attribute(id_aa_signingCertificate, new DERSet(signingCertificate));
        } else {
            ESSCertIDv2 essCertIdv2;
            if (NISTObjectIdentifiers.id_sha256.equals(digAlg.getAlgorithm())) {
                // SHA-256 is default
                essCertIdv2 = new ESSCertIDv2(null, certHash, issuerSerial);
            } else {
                essCertIdv2 = new ESSCertIDv2(digAlg, certHash, issuerSerial);
            }
            SigningCertificateV2 signingCertificateV2 = new SigningCertificateV2(essCertIdv2);
            return new Attribute(id_aa_signingCertificateV2, new DERSet(signingCertificateV2));
        }
    }

    IssuerSerial getIssuerSerial() {
        final X500Name issuerX500Name = x509CertificateHolder.getIssuer();
        final GeneralName generalName = new GeneralName(issuerX500Name);
        final GeneralNames generalNames = new GeneralNames(generalName);
        final BigInteger serialNumber = x509CertificateHolder.getSerialNumber();
        return new IssuerSerial(generalNames, serialNumber);
    }
}

(https://github.com/mkl-public/testarea-itext7/blob/master/src/main/java/mkl/testarea/itext7/signature/PadesSignatureContainerBc.java#L140助手类PadesSignedAttributeGenerator )

最后,为签名时间戳定制一个未签名的属性生成器:

代码语言:javascript
复制
class PadesUnsignedAttributeGenerator implements CMSAttributeTableGenerator {
    @Override
    public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params) throws CMSAttributeTableGenerationException {
        if (tsaClient == null)
            return null;
        try {
            ASN1EncodableVector unsignedAttributes = new ASN1EncodableVector();
            byte[] signature = (byte[])params.get(CMSAttributeTableGenerator.SIGNATURE);
            byte[] timestamp = tsaClient.getTimeStampToken(tsaClient.getMessageDigest().digest(signature));
            unsignedAttributes.add(new Attribute(id_aa_signatureTimeStampToken, new DERSet(ASN1Primitive.fromByteArray(timestamp))));
            return new AttributeTable(unsignedAttributes);
        } catch (Exception e) {
            throw new CMSAttributeTableGenerationException("", e);
        }
    }
}

(https://github.com/mkl-public/testarea-itext7/blob/master/src/main/java/mkl/testarea/itext7/signature/PadesSignatureContainerBc.java#L194助手类PadesUnsignedAttributeGenerator__)

这里我假设一个ITSAClient tsaClient,一个iText 7时间戳请求客户机。当然,您可以使用任意的RFC 3161时间戳请求客户端。

如果您已将私钥读入JCA/JCE PrivateKey pk中,则只需使用BouncyCastle JcaContentSignerBuilder创建所需的ContentSigner contentSigner,例如:

代码语言:javascript
复制
ContentSigner contentSigner = new JcaContentSignerBuilder("SHA512withRSA").build(pk);

(比较testSignPadesBaselineThttps://github.com/mkl-public/testarea-itext7/blob/master/src/test/java/mkl/testarea/itext7/signature/SignPadesBc.java#L65中的测试)

PDFBox 3签名框架中的实现

同时,您在评论中表示,您正在考虑使用PDFBox签名。幸运的是,上面提供的代码几乎不需要对PDFBox进行修改。

要在PDFBox中使用上面的代码,只需将其包装到PDFBox SignatureInterface框架中即可:

代码语言:javascript
复制
public class PadesSignatureContainerBc implements SignatureInterface {

    public PadesSignatureContainerBc(X509CertificateHolder x509CertificateHolder, ContentSigner contentSigner, TSAClient tsaClient) throws OperatorCreationException {
        this.contentSigner = contentSigner;
        this.tsaClient = tsaClient;
        this.x509CertificateHolder = x509CertificateHolder;

        digestCalculatorProvider = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
    }

    @Override
    public byte[] sign(InputStream content) throws IOException {
        try {
            CMSTypedData msg = new CMSTypedDataInputStream(content);

            CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

            gen.addSignerInfoGenerator(
                    new JcaSignerInfoGeneratorBuilder(digestCalculatorProvider)
                            .setSignedAttributeGenerator(new PadesSignedAttributeGenerator())
                            .setUnsignedAttributeGenerator(new PadesUnsignedAttributeGenerator())
                            .build(contentSigner, x509CertificateHolder));

            gen.addCertificates(new JcaCertStore(Collections.singleton(x509CertificateHolder)));

            CMSSignedData sigData = gen.generate(msg, false);
            return sigData.getEncoded();
        } catch (OperatorCreationException | GeneralSecurityException | CMSException e) {
            throw new IOException(e);
        }
    }

    final ContentSigner contentSigner;
    final X509CertificateHolder x509CertificateHolder;
    final TSAClient tsaClient;

    final DigestCalculatorProvider digestCalculatorProvider;

    class CMSTypedDataInputStream implements CMSTypedData {
        InputStream in;

        public CMSTypedDataInputStream(InputStream is) {
            in = is;
        }

        @Override
        public ASN1ObjectIdentifier getContentType() {
            return PKCSObjectIdentifiers.data;
        }

        @Override
        public Object getContent() {
            return in;
        }

        @Override
        public void write(OutputStream out) throws IOException,
                CMSException {
            byte[] buffer = new byte[8 * 1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
        }
    }

    class PadesSignedAttributeGenerator implements CMSAttributeTableGenerator {
        @Override
        public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params) throws CMSAttributeTableGenerationException {
            String currentAttribute = null;
            try {
                ASN1EncodableVector signedAttributes = new ASN1EncodableVector();
                currentAttribute = "SigningCertificateAttribute";
                AlgorithmIdentifier digAlgId = (AlgorithmIdentifier) params.get(CMSAttributeTableGenerator.DIGEST_ALGORITHM_IDENTIFIER);
                signedAttributes.add(createSigningCertificateAttribute(digAlgId));
                currentAttribute = "ContentType";
                ASN1ObjectIdentifier contentType = ASN1ObjectIdentifier.getInstance(params.get(CMSAttributeTableGenerator.CONTENT_TYPE));
                signedAttributes.add(new Attribute(CMSAttributes.contentType, new DERSet(contentType)));
                currentAttribute = "MessageDigest";
                byte[] messageDigest = (byte[])params.get(CMSAttributeTableGenerator.DIGEST);
                signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(messageDigest))));

                return new AttributeTable(signedAttributes);
            } catch (Exception e) {
                throw new CMSAttributeTableGenerationException(currentAttribute, e);
            }
        }

        Attribute createSigningCertificateAttribute(AlgorithmIdentifier digAlg) throws IOException, OperatorCreationException {
            final IssuerSerial issuerSerial = getIssuerSerial();
            DigestCalculator digestCalculator = digestCalculatorProvider.get(digAlg);
            digestCalculator.getOutputStream().write(x509CertificateHolder.getEncoded());
            final byte[] certHash = digestCalculator.getDigest();

            if (OIWObjectIdentifiers.idSHA1.equals(digAlg.getAlgorithm())) {
                final ESSCertID essCertID = new ESSCertID(certHash, issuerSerial);
                SigningCertificate signingCertificate = new SigningCertificate(essCertID);
                return new Attribute(id_aa_signingCertificate, new DERSet(signingCertificate));
            } else {
                ESSCertIDv2 essCertIdv2;
                if (NISTObjectIdentifiers.id_sha256.equals(digAlg.getAlgorithm())) {
                    // SHA-256 is default
                    essCertIdv2 = new ESSCertIDv2(null, certHash, issuerSerial);
                } else {
                    essCertIdv2 = new ESSCertIDv2(digAlg, certHash, issuerSerial);
                }
                SigningCertificateV2 signingCertificateV2 = new SigningCertificateV2(essCertIdv2);
                return new Attribute(id_aa_signingCertificateV2, new DERSet(signingCertificateV2));
            }
        }

        public IssuerSerial getIssuerSerial() {
            final X500Name issuerX500Name = x509CertificateHolder.getIssuer();
            final GeneralName generalName = new GeneralName(issuerX500Name);
            final GeneralNames generalNames = new GeneralNames(generalName);
            final BigInteger serialNumber = x509CertificateHolder.getSerialNumber();
            return new IssuerSerial(generalNames, serialNumber);
        }
    }

    class PadesUnsignedAttributeGenerator implements CMSAttributeTableGenerator {
        @Override
        public AttributeTable getAttributes(@SuppressWarnings("rawtypes") Map params) throws CMSAttributeTableGenerationException {
            if (tsaClient == null)
                return null;
            try {
                ASN1EncodableVector unsignedAttributes = new ASN1EncodableVector();
                byte[] signature = (byte[])params.get(CMSAttributeTableGenerator.SIGNATURE);
                byte[] timestamp = tsaClient.getTimeStampToken(new ByteArrayInputStream(signature)).getEncoded();
                unsignedAttributes.add(new Attribute(id_aa_signatureTimeStampToken, new DERSet(ASN1Primitive.fromByteArray(timestamp))));
                return new AttributeTable(unsignedAttributes);
            } catch (Exception e) {
                throw new CMSAttributeTableGenerationException("", e);
            }
        }
    }
}

(PDFBox https://github.com/mkl-public/testarea-pdfbox2/blob/master/src/main/java/mkl/testarea/pdfbox2/sign/PadesSignatureContainerBc.java实现SignatureInterface__)

你可以这样用它

代码语言:javascript
复制
try (   PDDocument pdDocument = Loader.loadPDF(SOURCE_PDF)   )
{
    SignatureInterface signatureInterface = new PadesSignatureContainerBc(new X509CertificateHolder(chain[0].getEncoded()),
            new JcaContentSignerBuilder("SHA512withRSA").build(pk),
            new TSAClient(new URL("http://timestamp.server/rfc3161endpoint"), null, null, MessageDigest.getInstance("SHA-256")));

    PDSignature signature = new PDSignature();
    signature.setFilter(COSName.getPDFName("MKLx_PAdES_SIGNER"));
    signature.setSubFilter(COSName.getPDFName("ETSI.CAdES.detached"));
    signature.setName("Example User");
    signature.setLocation("Los Angeles, CA");
    signature.setReason("Testing");
    signature.setSignDate(Calendar.getInstance());
    pdDocument.addSignature(signature);

    ExternalSigningSupport externalSigning = pdDocument.saveIncrementalForExternalSigning(RESULT_OUTPUT);
    // invoke external signature service
    byte[] cmsSignature = signatureInterface.sign(externalSigning.getContent());
    // set signature bytes received from the service
    externalSigning.setSignature(cmsSignature);
}

(PDFBox https://github.com/mkl-public/testarea-pdfbox2/blob/master/src/test/java/mkl/testarea/pdfbox2/sign/SignPadesBc.java#L71 test testSignPadesBaselineT__)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71225696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档