Example usage for org.bouncycastle.cms SignerInformation getSignature

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

Introduction

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

Prototype

public byte[] getSignature() 

Source Link

Document

return the encoded signature

Usage

From source file:se.tillvaxtverket.ttsigvalws.ttwssigvalidation.pdf.PdfSignatureVerifier.java

License:Open Source License

private static void checkTimestamps(CMSSignedDataParser sp, CMSSigVerifyResult sigResult) throws CMSException {
    List<TimeStampResult> timeStampResultList = sigResult.getTimStampResultList();
    sigResult.setTimStampResultList(timeStampResultList);
    SignerInformationStore signers = sp.getSignerInfos();
    Collection c = signers.getSigners();
    Iterator it = c.iterator();//w  ww .  ja va2s  . com
    if (!it.hasNext()) {
        return;
    }
    SignerInformation signer = (SignerInformation) it.next();

    //Collect and check time stamps
    AttributeTable unsignedAttributes = signer.getUnsignedAttributes();
    if (unsignedAttributes == null) {
        return;
    }
    ASN1EncodableVector timeStamps = unsignedAttributes
            .getAll(new ASN1ObjectIdentifier("1.2.840.113549.1.9.16.2.14"));
    if (timeStamps.size() == 0) {
        return;
    }
    for (int i = 0; i < timeStamps.size(); i++) {
        try {
            Attribute timestampAttr = Attribute.getInstance(timeStamps.get(i));
            byte[] timeStampBytes = timestampAttr.getAttrValues().getObjectAt(0).toASN1Primitive().getEncoded();
            TimeStampResult tsResult = new TimeStampResult();
            tsResult.setTimestamp(timeStampBytes);
            timeStampResultList.add(tsResult);

            InputStream tsis = new ByteArrayInputStream(timeStampBytes);
            CMSSignedDataParser tsSp = new CMSSignedDataParser(new BcDigestCalculatorProvider(), tsis);

            byte[] tsInfoBytes = IOUtils.toByteArray(tsSp.getSignedContent().getContentStream());
            TimeStampData timeStampData = PdfBoxSigUtil.getTimeStampData(tsInfoBytes);
            tsResult.setTsData(timeStampData);

            //Compare TimeStamp data hash with signature hash
            byte[] sigHash = getDigest(timeStampData.getImprintHashAlgo(), signer.getSignature());
            tsResult.setTimestampMatch(Arrays.equals(sigHash, timeStampData.getImprintDigest()));

            CMSSigVerifyResult tsSigResult = new CMSSigVerifyResult();
            tsSigResult.setSignedData(timeStampBytes);
            tsResult.setSignatureVerification(tsSigResult);

            verifyCMSSignature(tsSp, tsSigResult);
        } catch (Exception e) {
        }

    }
    sigResult.setTimStampResultList(timeStampResultList);
}