for (Alias alias : this.parameters.getAliases()) {
PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) this.parameters.getStore().get(alias, StoreEntryType.PRIVATE_KEY);
if (privateKeyEntry == null) {
throw new SignerException("PrivateKey not found for alias '" + alias.getName() + "'");
}
PrivateKey privateKey = privateKeyEntry.getValue();
Certificate[] chain = privateKeyEntry.getChain();
Certificate certificate = chain[0];
JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder(signatureType.getAlgorithm());
if (ConditionUtils.isNotEmpty(this.parameters.getProvider())) {
contentSignerBuilder.setProvider(this.parameters.getProvider());
} else {
contentSignerBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);
}
ContentSigner contentSigner = contentSignerBuilder.build(privateKey);
JcaDigestCalculatorProviderBuilder digestCalculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder();
digestCalculatorProviderBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);
DigestCalculatorProvider digestCalculatorProvider = digestCalculatorProviderBuilder.build();
JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalculatorProvider);
if (this.parameters.isDataDigested()) {
Attribute attr = new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(data)));
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(attr);
signerInfoGeneratorBuilder.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v)));
}
SignerInfoGenerator signerInfoGenerator = signerInfoGeneratorBuilder.build(contentSigner, (X509Certificate) certificate);
signedDataGenerator.addSignerInfoGenerator(signerInfoGenerator);
signedDataGenerator.addCertificates(this.getCertificateStore(chain));
}
CMSTypedData content = null;
boolean encapsulate = true;
if (this.parameters.isDataDigested()) {
content = new CMSAbsentContent();
encapsulate = false;
} else {
if ((this.parameters.isMergeSignatures()) && (this.isSigned(data))) {
CMSSignedData signedData = new CMSSignedData(data);
signedDataGenerator.addSigners(signedData.getSignerInfos());
content = (CMSTypedData) signedData.getSignedContent();
} else {
content = new CMSProcessableByteArray(data);
}
if (PKCS7SignatureMode.DETACHED.equals(mode)) {
encapsulate = false;
}
}
CMSSignedData signedData = null;
if (this.parameters.isDataDigested()) {
signedData = signedDataGenerator.generate(CMSSignedGenerator.DATA, null, false, Security.getProvider(BouncyCastleProviderHelper.PROVIDER_NAME), true);
} else {
signedData = signedDataGenerator.generate(content, encapsulate);
}
if (timeStampClient != null) {
SignerInformationStore signerInformationStore = signedData.getSignerInfos();
List list = new ArrayList();
for (Object o : signerInformationStore.getSigners()) {
SignerInformation signerInformation = (SignerInformation) o;
TimeStamp timeStamp = timeStampClient.getTimeStamp(signerInformation.getSignature());
ASN1Primitive asn1Primitive = BouncyCastleProviderHelper.toASN1Primitive(timeStamp.getEncoded());
DERSet derSet = new DERSet(asn1Primitive);
Hashtable hashtable = new Hashtable();
Attribute attribute = new Attribute(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, derSet);
hashtable.put(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, attribute);
AttributeTable unsignedAtts = new AttributeTable(hashtable);
list.add(SignerInformation.replaceUnsignedAttributes(signerInformation, unsignedAtts));
}
SignerInformationStore tmpSignerInformationStore = new SignerInformationStore(list);
signedData = CMSSignedData.replaceSigners(signedData, tmpSignerInformationStore);
}
return signedData.getEncoded();
} catch (Exception e) {
throw new SignerException(e);
}
}