@SuppressWarnings("unchecked")
public boolean verify(final byte[] data, final byte[] signature) {
Assert.notEmpty(data, "data");
Assert.notEmpty(signature, "signature");
try {
CMSSignedData signedData = new CMSSignedData(signature);
CollectionStore certificatesStore = (CollectionStore) signedData.getCertificates();
// CollectionStore crlStore = (CollectionStore) signedData.getCRLs();
SignerInformationStore signerInformationStore = signedData.getSignerInfos();
boolean verified = true;
for (Object o : signerInformationStore.getSigners()) {
SignerInformation signerInformation = (SignerInformation) o;
Collection<Certificate> collection = certificatesStore.getMatches(null);
if (!collection.isEmpty()) {
for (Certificate cert : collection) {
JcaContentVerifierProviderBuilder jcaContentVerifierProviderBuilder = new JcaContentVerifierProviderBuilder();
jcaContentVerifierProviderBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);
ContentVerifierProvider contentVerifierProvider = jcaContentVerifierProviderBuilder.build((X509Certificate) cert);
JcaDigestCalculatorProviderBuilder digestCalculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder();
digestCalculatorProviderBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);
DigestCalculatorProvider digestCalculatorProvider = digestCalculatorProviderBuilder.build();
SignerInformationVerifier signerInformationVerifier = new SignerInformationVerifier(contentVerifierProvider, digestCalculatorProvider);
if (!signerInformation.verify(signerInformationVerifier)) {
verified = false;
}
}
}
}
if (verified) {
CMSProcessable signedContent = signedData.getSignedContent();
byte[] content = (byte[]) signedContent.getContent();
verified = Arrays.equals(data, content);
}
return verified;
} catch (Exception e) {