@Override
@SuppressWarnings({ "unchecked" })
public Signature[] getSignatures(final byte[] data) {
try {
CMSSignedData signedData = new CMSSignedData(data);
Collection<X509CertificateHolder> certificates = signedData.getCertificates().getMatches(null);
SignerInformationStore signerInformationStore = signedData.getSignerInfos();
Collection<SignerInformation> informations = signerInformationStore.getSigners();
Collection<Signature> signatures = new ArrayList<Signature>();
// 1.2.840.113549.1.9.3 -- content type
// 1.2.840.113549.1.9.4 -- messagedigest
// 1.2.840.113549.1.9.5 -- sign time
// 1.2.840.113549.1.9.16.2.12 -- signcertificate
if (ConditionUtils.isNotEmpty(informations)) {
for (SignerInformation information : informations) {
Signature signature = new Signature();
signature.setEncoded(null); // FIXME
signature.setLocation(null); // FIXME
signature.setReason(null); // FIXME
signature.setSignatories(new ArrayList<Signatory>());
signature.setValid(Boolean.TRUE);
// TimeStamp
AttributeTable signedAttributeTable = information.getSignedAttributes();
AttributeTable unsignedAttributeTable = information.getUnsignedAttributes();
DERSequence timeStampDerSequence = this.getAttribute(signedAttributeTable, unsignedAttributeTable, PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
// DERSequence contentTypeDerSequence = this.getAttribute(signedAttributeTable, unsignedAttributeTable, PKCSObjectIdentifiers.pkcs_9_at_contentType);
// DERSequence messageDigestDerSequence = this.getAttribute(signedAttributeTable, unsignedAttributeTable, PKCSObjectIdentifiers.pkcs_9_at_messageDigest);
DERSequence signTimeDerSequence = this.getAttribute(signedAttributeTable, unsignedAttributeTable, PKCSObjectIdentifiers.pkcs_9_at_signingTime);
if (timeStampDerSequence != null) {
if (timeStampDerSequence.size() == 2) {
DERObject derObjectIdentifier = ((DERObject) timeStampDerSequence.getObjectAt(0)).toASN1Object();
DERObject derObjectValue = ((DERObject) timeStampDerSequence.getObjectAt(1)).toASN1Object();
if ((derObjectIdentifier instanceof ASN1ObjectIdentifier) && (derObjectValue instanceof DERSet)) {
// ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier) derObjectIdentifier;
DERSet set = (DERSet) derObjectValue;
DEREncodable encodable = set.getObjectAt(0);
TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(encodable.getDERObject().getEncoded()));
TimeStamp timeStamp = BouncyCastleTimeStampHelper.toTimeStamp(timeStampToken);
signature.setTimeStamp(timeStamp);
}
}
}