Search in sources :

Example 1 with JcaX509CertSelectorConverter

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;
}
Also used : JcaX509CertSelectorConverter(org.bouncycastle.cms.jcajce.JcaX509CertSelectorConverter) Attribute(org.bouncycastle.asn1.cms.Attribute) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) AttributeTable(org.bouncycastle.asn1.cms.AttributeTable) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) Store(org.bouncycastle.util.Store) SignerInformation(org.bouncycastle.cms.SignerInformation) CertSelector(java.security.cert.CertSelector) CMSSignedData(org.bouncycastle.cms.CMSSignedData) DERSet(org.bouncycastle.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) SignerInformationStore(org.bouncycastle.cms.SignerInformationStore) SignerId(org.bouncycastle.cms.SignerId) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) Collection(java.util.Collection) ASN1EncodableVector(org.bouncycastle.asn1.ASN1EncodableVector) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier)

Aggregations

CertSelector (java.security.cert.CertSelector)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Hashtable (java.util.Hashtable)1 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 DERSet (org.bouncycastle.asn1.DERSet)1 Attribute (org.bouncycastle.asn1.cms.Attribute)1 AttributeTable (org.bouncycastle.asn1.cms.AttributeTable)1 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)1 CMSSignedData (org.bouncycastle.cms.CMSSignedData)1 SignerId (org.bouncycastle.cms.SignerId)1 SignerInformation (org.bouncycastle.cms.SignerInformation)1 SignerInformationStore (org.bouncycastle.cms.SignerInformationStore)1 JcaX509CertSelectorConverter (org.bouncycastle.cms.jcajce.JcaX509CertSelectorConverter)1 Store (org.bouncycastle.util.Store)1