use of org.bouncycastle.cms.jcajce.JcaX509CertSelectorConverter in project gdmatrix by gdmatrix.
the class P7MUtils method addTimeStamp.
public static CMSSignedData addTimeStamp(String serviceURI, CMSSignedData signedData) throws Exception {
CMSSignedData newSignedData;
SignerInformationStore sigStore = signedData.getSignerInfos();
ArrayList siList = new ArrayList();
for (Object o : sigStore.getSigners()) {
// CertStore certStore =
// signedData.getCertificatesAndCRLs("Collection", "BC");
Store store = signedData.getCertificates();
SignerInformation si = (SignerInformation) o;
SignerId sigId = si.getSID();
JcaX509CertSelectorConverter converter = new JcaX509CertSelectorConverter();
CertSelector certSelector = converter.getCertSelector(sigId);
Collection certCollection = store.getMatches((Selector) certSelector);
// Collection certCollection = certStore.getCertificates(sigId);
X509Certificate certificate = (X509Certificate) certCollection.iterator().next();
System.out.println(certificate.getSubjectDN().getName());
// get signature
byte[] signature = si.getSignature();
// signed attributes
System.out.println("SignedAttributes:");
AttributeTable signedAttributes = si.getSignedAttributes();
printAttributeTable(signedAttributes);
// unsigned attributes
System.out.println("UnsignedAttributes:");
AttributeTable unsignedAttributes = si.getUnsignedAttributes();
printAttributeTable(unsignedAttributes);
ASN1ObjectIdentifier tsId = new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14");
Attribute att = unsignedAttributes == null ? null : unsignedAttributes.get(tsId);
if (att == null) {
System.out.println("creating timeStamp...");
ASN1EncodableVector tsVector = new ASN1EncodableVector();
ContentInfo timeStampToken = createTimeStamp(serviceURI, signature);
tsVector.add(timeStampToken);
DERSet attributeValues = new DERSet(tsVector);
att = new Attribute(tsId, attributeValues);
Hashtable attrMap = new Hashtable();
attrMap.put(tsId, att);
AttributeTable table = new AttributeTable(attrMap);
SignerInformation newSi = SignerInformation.replaceUnsignedAttributes(si, table);
siList.add(newSi);
} else {
System.out.println("timeStamp present");
}
}
if (// replace signers
!siList.isEmpty()) {
newSignedData = CMSSignedData.replaceSigners(signedData, new SignerInformationStore(siList));
newSignedData = new CMSSignedData(newSignedData.getEncoded());
} else
newSignedData = signedData;
return newSignedData;
}
Aggregations