Search in sources :

Example 41 with CMSException

use of org.bouncycastle.cms.CMSException in project walle by Meituan-Dianping.

the class V1SchemeSigner method generateSignatureBlock.

private static byte[] generateSignatureBlock(SignerConfig signerConfig, byte[] signatureFileBytes) throws InvalidKeyException, CertificateEncodingException, SignatureException {
    JcaCertStore certs = new JcaCertStore(signerConfig.certificates);
    X509Certificate signerCert = signerConfig.certificates.get(0);
    String jcaSignatureAlgorithm = getJcaSignatureAlgorithm(signerCert.getPublicKey(), signerConfig.signatureDigestAlgorithm);
    try {
        ContentSigner signer = new JcaContentSignerBuilder(jcaSignatureAlgorithm).build(signerConfig.privateKey);
        CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
        gen.addSignerInfoGenerator(new SignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build(), SignerInfoSignatureAlgorithmFinder.INSTANCE).setDirectSignature(true).build(signer, new JcaX509CertificateHolder(signerCert)));
        gen.addCertificates(certs);
        CMSSignedData sigData = gen.generate(new CMSProcessableByteArray(signatureFileBytes), false);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) {
            DEROutputStream dos = new DEROutputStream(out);
            dos.writeObject(asn1.readObject());
        }
        return out.toByteArray();
    } catch (OperatorCreationException | CMSException | IOException e) {
        throw new SignatureException("Failed to generate signature", e);
    }
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ContentSigner(org.bouncycastle.operator.ContentSigner) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SignatureException(java.security.SignatureException) JcaX509CertificateHolder(org.bouncycastle.cert.jcajce.JcaX509CertificateHolder) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) SignerInfoGeneratorBuilder(org.bouncycastle.cms.SignerInfoGeneratorBuilder) JcaDigestCalculatorProviderBuilder(org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) DEROutputStream(org.bouncycastle.asn1.DEROutputStream) CMSException(org.bouncycastle.cms.CMSException)

Aggregations

CMSException (org.bouncycastle.cms.CMSException)41 CMSSignedData (org.bouncycastle.cms.CMSSignedData)30 IOException (java.io.IOException)28 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)19 X509Certificate (java.security.cert.X509Certificate)18 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)14 CMSSignedDataGenerator (org.bouncycastle.cms.CMSSignedDataGenerator)14 CMSProcessableByteArray (org.bouncycastle.cms.CMSProcessableByteArray)13 CertificateEncodingException (java.security.cert.CertificateEncodingException)11 CertificateException (java.security.cert.CertificateException)10 SignerInformation (org.bouncycastle.cms.SignerInformation)9 CMSAbsentContent (org.bouncycastle.cms.CMSAbsentContent)8 SignerInformationStore (org.bouncycastle.cms.SignerInformationStore)8 InputStream (java.io.InputStream)7 AttributeTable (org.bouncycastle.asn1.cms.AttributeTable)7 TSPException (org.bouncycastle.tsp.TSPException)7 CertificateCoreException (org.demoiselle.signer.core.exception.CertificateCoreException)7 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)6 Attribute (org.bouncycastle.asn1.cms.Attribute)6 CMSTypedData (org.bouncycastle.cms.CMSTypedData)6