@SuppressWarnings({ "rawtypes", "unchecked" })
public byte[] sign(final byte[] data) {
Require.notNull(this.parameters, "parameters");
Assert.notEmpty(data, "data");
try {
CMSSignedDataGenerator signedDataGenerator = new CMSSignedDataGenerator();
TimeStampClient timeStampClient = this.parameters.getTimeStampClient();
PKCS7SignatureMode mode = this.parameters.getSignatureMode();
SignatureType signatureType = this.parameters.getSignatureType();
if (signatureType == null) {
signatureType = SignatureType.SHA1_RSA;
}
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();