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());
}
}
Aggregations