Example usage for org.bouncycastle.cms SignerInformation replaceUnsignedAttributes

List of usage examples for org.bouncycastle.cms SignerInformation replaceUnsignedAttributes

Introduction

In this page you can find the example usage for org.bouncycastle.cms SignerInformation replaceUnsignedAttributes.

Prototype

public static SignerInformation replaceUnsignedAttributes(SignerInformation signerInformation,
        AttributeTable unsignedAttributes) 

Source Link

Document

Return a signer information object with the passed in unsigned attributes replacing the ones that are current associated with the object passed in.

Usage

From source file:org.votingsystem.signature.smime.SMIMEMessage.java

License:Open Source License

public void setTimeStampToken(TimeStampToken timeStampToken) throws Exception {
    if (timeStampToken == null)
        throw new Exception("timestamp token null");
    DERObject derObject = new ASN1InputStream(timeStampToken.getEncoded()).readObject();
    DERSet derset = new DERSet(derObject);
    Attribute timeStampAsAttribute = new Attribute(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, derset);
    Hashtable hashTable = new Hashtable();
    hashTable.put(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, timeStampAsAttribute);
    AttributeTable timeStampAsAttributeTable = new AttributeTable(hashTable);
    byte[] timeStampTokenHash = timeStampToken.getTimeStampInfo().getMessageImprintDigest();
    Iterator<SignerInformation> it = smimeSigned.getSignerInfos().getSigners().iterator();
    List<SignerInformation> newSigners = new ArrayList<SignerInformation>();
    while (it.hasNext()) {
        SignerInformation signer = it.next();
        byte[] digestBytes = CMSUtils.getSignerDigest(signer);
        if (Arrays.equals(timeStampTokenHash, digestBytes)) {
            log.info("setTimeStampToken - found signer");
            AttributeTable attributeTable = signer.getUnsignedAttributes();
            SignerInformation updatedSigner = null;
            if (attributeTable != null) {
                log.info("setTimeStampToken - signer with UnsignedAttributes");
                hashTable = attributeTable.toHashtable();
                hashTable.put(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, timeStampAsAttribute);
                timeStampAsAttributeTable = new AttributeTable(hashTable);
            }/*  w  w w  .j ava2 s . com*/
            updatedSigner = signer.replaceUnsignedAttributes(signer, timeStampAsAttributeTable);
            newSigners.add(updatedSigner);
        } else
            newSigners.add(signer);
    }
    SignerInformationStore newSignersStore = new SignerInformationStore(newSigners);
    CMSSignedData cmsdata = smimeSigned.replaceSigners(smimeSigned, newSignersStore);
    replaceSigners(cmsdata);
}