Example usage for org.bouncycastle.cms CMSTypedStream drain

List of usage examples for org.bouncycastle.cms CMSTypedStream drain

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSTypedStream drain.

Prototype

public void drain() throws IOException 

Source Link

Usage

From source file:org.ejbca.util.CMS.java

License:Open Source License

/**
 * @param is signed data to be verified//  ww w.j  av  a 2  s .  c om
 * @param os signature removed from signed data
 * @param cert the certificate with the public key that should do the verification
 * @return true if the signing was to with the private key corresponding to the public key in the certificate.
 * @throws Exception
 */
public static VerifyResult verify(final InputStream is, OutputStream os, X509Certificate cert)
        throws Exception {
    final InputStream bis = new BufferedInputStream(is, bufferSize);
    final OutputStream bos = new BufferedOutputStream(os, bufferSize);
    final CMSSignedDataParser sp = new CMSSignedDataParser(new BcDigestCalculatorProvider(), bis);
    final CMSTypedStream sc = sp.getSignedContent();
    final InputStream ris = sc.getContentStream();
    fromInToOut(ris, bos);
    os.close();
    sc.drain();
    @SuppressWarnings("rawtypes")
    final Iterator it = sp.getSignerInfos().getSigners().iterator();
    if (!it.hasNext()) {
        return null;
    }
    final SignerInformation signerInfo = (SignerInformation) it.next();
    final Attribute attribute = (Attribute) signerInfo.getSignedAttributes().getAll(CMSAttributes.signingTime)
            .get(0);
    final Date date = Time.getInstance(attribute.getAttrValues().getObjectAt(0).toASN1Primitive()).getDate();
    final SignerId id = signerInfo.getSID();
    boolean result = false;
    try {
        JcaDigestCalculatorProviderBuilder calculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder()
                .setProvider(BouncyCastleProvider.PROVIDER_NAME);
        JcaSignerInfoVerifierBuilder jcaSignerInfoVerifierBuilder = new JcaSignerInfoVerifierBuilder(
                calculatorProviderBuilder.build()).setProvider(BouncyCastleProvider.PROVIDER_NAME);
        result = signerInfo.verify(jcaSignerInfoVerifierBuilder.build(cert.getPublicKey()));
    } catch (Throwable t) { // NOPMD
        log.debug("Exception when verifying", t);
    }
    return new VerifyResult(date, result, id);
}

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

License:Open Source License

/**
 * Verifies one individual signature element of a signed PDF document
 *
 * @param signedData The SignedData of this signature
 * @param signedContentBytes The data being signed by this signature
 * @param sigResult The signature verification result object used to express
 * signature result data.//ww w  . j av  a 2 s  . c o  m
 * @param verifyPades The value true causes verification to check for the
 * signed signature certificate signed attributes. If present, this
 * attribute is validated against the provided signature certificate in
 * signed data.
 * @throws Exception
 */
public static void verifySign(byte[] signedData, byte[] signedContentBytes, CMSSigVerifyResult sigResult,
        boolean verifyPades) throws Exception {
    InputStream is = new ByteArrayInputStream(signedContentBytes);
    CMSSignedDataParser sp = new CMSSignedDataParser(new BcDigestCalculatorProvider(), new CMSTypedStream(is),
            signedData);
    CMSTypedStream signedContent = sp.getSignedContent();
    signedContent.drain();
    sigResult.setSignedData(signedData);

    verifyCMSSignature(sp, sigResult);
    checkTimestamps(sp, sigResult);
}