Search in sources :

Example 6 with SignerException

use of com.bluenimble.platform.crypto.signer.SignerException in project serverless by bluenimble.

the class DefaultSigner method signWithCerts.

// Updated
private void signWithCerts(SecureDocument doc, PrivateKey key, X509Certificate[] certs) throws SignerException {
    if (certs == null || certs.length == 0) {
        throw new SignerException("A valid X509 Certificate is required");
    }
    String signAlg = "DSA".equals(key.getAlgorithm()) ? CMSSignedDataGenerator.DIGEST_SHA1 : CMSSignedDataGenerator.DIGEST_MD5;
    CMSSignedDataGenerator signGen = new CMSSignedDataGenerator();
    List<X509CertificateHolder> certList = new ArrayList<X509CertificateHolder>();
    try {
        ContentSigner contentSigner = new JcaContentSignerBuilder(signAlg).setProvider("BC").build(key);
        DigestCalculatorProvider digestCalcProv = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
        for (X509Certificate cert : certs) {
            X509CertificateHolder certHolder = new X509CertificateHolder(cert.getEncoded());
            certList.add(certHolder);
            SignerInfoGenerator signInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalcProv).build(contentSigner, cert);
            signGen.addSignerInfoGenerator(signInfoGeneratorBuilder);
        }
        JcaCertStore jcaCertStore = new JcaCertStore(certList);
        signGen.addCertificates(jcaCertStore);
        // signGen.addCRLs (jcaCertStore); TODO : not sure
        CMSProcessableByteArray content = new CMSProcessableByteArray(doc.getBytes());
        CMSSignedData signedData = signGen.generate(content, true);
        doc.setBytes(signedData.getEncoded());
    } catch (Throwable th) {
        throw new SignerException(th, th.getMessage());
    }
}
Also used : CMSSignedDataGenerator(org.bouncycastle.cms.CMSSignedDataGenerator) CMSProcessableByteArray(org.bouncycastle.cms.CMSProcessableByteArray) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) ArrayList(java.util.ArrayList) ContentSigner(org.bouncycastle.operator.ContentSigner) JcaCertStore(org.bouncycastle.cert.jcajce.JcaCertStore) CMSSignedData(org.bouncycastle.cms.CMSSignedData) X509Certificate(java.security.cert.X509Certificate) JcaSignerInfoGeneratorBuilder(org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder) DigestCalculatorProvider(org.bouncycastle.operator.DigestCalculatorProvider) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) SignerInfoGenerator(org.bouncycastle.cms.SignerInfoGenerator) JcaDigestCalculatorProviderBuilder(org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder) SignerException(com.bluenimble.platform.crypto.signer.SignerException)

Aggregations

SignerException (com.bluenimble.platform.crypto.signer.SignerException)6 SignatureAware (com.bluenimble.platform.crypto.SignatureAware)4 X509Certificate (java.security.cert.X509Certificate)3 StringKey (com.bluenimble.platform.crypto.signer.StringKey)2 Key (java.security.Key)2 MessageDigest (java.security.MessageDigest)2 PrivateKey (java.security.PrivateKey)2 Mac (javax.crypto.Mac)2 SecretKey (javax.crypto.SecretKey)2 CMSSignedData (org.bouncycastle.cms.CMSSignedData)2 Base64 (com.bluenimble.platform.crypto.Base64)1 SecureDocument (com.bluenimble.platform.crypto.SecureDocument)1 CertificatesManager (com.bluenimble.platform.crypto.generator.CertificatesManager)1 DefaultCertificatesManager (com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager)1 CertificateAcceptor (com.bluenimble.platform.crypto.signer.CertificateAcceptor)1 Signer (com.bluenimble.platform.crypto.signer.Signer)1 DefaultSigner (com.bluenimble.platform.crypto.signer.impl.DefaultSigner)1 StringSecureDocument (com.bluenimble.platform.crypto.signer.impl.StringSecureDocument)1 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1