Example usage for org.bouncycastle.asn1.pkcs ContentInfo getContent

List of usage examples for org.bouncycastle.asn1.pkcs ContentInfo getContent

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs ContentInfo getContent.

Prototype

public ASN1Encodable getContent() 

Source Link

Usage

From source file:ca.trustpoint.m2m.M2mCertificateFactory.java

License:Apache License

/**
 * Generates a {@link java.security.cert.CertPath CertPath} object and initializes it with the
 * data read from the {@link java.io.InputStream InputStream} inStream. The data is assumed to be
 * in the specified encoding.//from w  ww .j  a  v  a  2s  .c o m
 *
 * <p>
 * The returned certificate path object can be typecast to the {@link M2mCertPath} class.
 *
 * @param inStream an {@link java.io.InputStream InputStream} containing the data
 * @param encoding the encoding used for the data
 * @return a {@link java.security.cert.CertPath CertPath} initialized with the data from the
 *         {@link java.io.InputStream InputStream}
 * @exception CertificateException if an exception occurs while decoding or the encoding requested
 *            is not supported
 */
@Override
public CertPath engineGenerateCertPath(InputStream inStream, String encoding) throws CertificateException {
    if (inStream == null) {
        throw new CertificateException("input stream is null");
    }

    try {
        ASN1InputStream aIn = new ASN1InputStream(inStream);
        ASN1Sequence seq = ASN1Sequence.getInstance(aIn.readObject());

        aIn.close();

        ASN1Encodable[] objs;
        List<M2mCertificate> certList;
        InputStream is;
        M2mCertificate cert;

        if (encoding.equals(SupportedEncodings.PKIPATH.getId())) {
            objs = seq.toArray();
            certList = new ArrayList<M2mCertificate>(objs.length);

            // certificates in PKIPATH encoding is from root to signer but M2MCerPath stores
            // certificates from signer to root so do it in reverse order.
            for (int i = objs.length - 1; i >= 0; i--) {
                is = new ByteArrayInputStream(objs[i].toASN1Primitive().getEncoded());
                cert = (M2mCertificate) engineGenerateCertificate(is);
                certList.add(cert);
            }
        } else if (encoding.equals(SupportedEncodings.PKCS7.getId())) {
            ContentInfo ci = ContentInfo.getInstance(seq);
            SignedData sd = SignedData.getInstance(ci.getContent());
            objs = sd.getCertificates().toArray();
            certList = new ArrayList<M2mCertificate>(objs.length);

            // certificates in PKCS7 encoding is from signer to root, the same order as in M2mCertPath
            for (int i = 0; i < objs.length; i++) {
                is = new ByteArrayInputStream(objs[i].toASN1Primitive().getEncoded());
                cert = (M2mCertificate) engineGenerateCertificate(is);
                certList.add(cert);
            }
        } else {
            throw new CertificateException("unknown encoding path: " + encoding);
        }

        return new M2mCertPath(certList);
    } catch (IOException e) {
        throw new CertificateException("IOException parsing PkiPath data: " + e, e);
    }
}

From source file:fi.laverca.Pkcs7.java

License:Apache License

/**
 * Convert a byte array to a PKCS7 SignedData object
 * @param bytes byte array/*from w  w  w . j  a  v a2  s .  co  m*/
 * @return PKCS7 SignedData object
 */
public static SignedData bytesToPkcs7SignedData(byte[] bytes) {

    if (bytes == null) {
        throw new IllegalArgumentException("null bytes");
    }

    ASN1InputStream ais = new ASN1InputStream(bytes);
    ASN1Object asn1 = null;
    try {
        asn1 = ais.readObject();
    } catch (IOException ioe) {
        throw new IllegalArgumentException("not a pkcs7 signature");
    } finally {
        try {
            ais.close();
        } catch (IOException e) {
            // Ignore
        }
    }

    ContentInfo ci = ContentInfo.getInstance(asn1);

    DERObjectIdentifier typeId = ci.getContentType();
    if (!typeId.equals(PKCSObjectIdentifiers.signedData)) {
        throw new IllegalArgumentException("not a pkcs7 signature");
    }

    return SignedData.getInstance(ci.getContent());
}

From source file:se.inera.intyg.webcert.web.service.signatur.asn1.ASN1UtilImpl.java

License:Open Source License

@Override
public String getValue(String identifier, InputStream asn1Signature) {
    ByteArrayInputStream bais = null;
    ASN1InputStream asn1InputStream = null;
    try {//from w ww. j a  v a  2 s .com
        bais = convertStream(asn1Signature);
        asn1InputStream = new ASN1InputStream(bais);
        DERObject obj = asn1InputStream.readObject();
        ContentInfo contentInfo = ContentInfo.getInstance(obj);

        // Extract certificates
        SignedData signedData = SignedData.getInstance(contentInfo.getContent());
        return findInCertificate(identifier, (DERObject) signedData.getCertificates().getObjectAt(0));
    } catch (IOException e) {
        LOG.error("Error parsing signature: {}", e.getMessage());
        throw new IllegalStateException(e);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(asn1InputStream);
    }
}