Search in sources :

Example 6 with ParsingException

use of sun.security.pkcs.ParsingException in project Bytecoder by mirkosertic.

the class X509Factory method parseX509orPKCS7Cert.

/*
     * Parses the data in the given input stream as a sequence of DER
     * encoded X.509 certificates (in binary or base 64 encoded format) OR
     * as a single PKCS#7 encoded blob (in binary or base64 encoded format).
     */
private Collection<? extends java.security.cert.Certificate> parseX509orPKCS7Cert(InputStream is) throws CertificateException, IOException {
    int peekByte;
    byte[] data;
    PushbackInputStream pbis = new PushbackInputStream(is);
    Collection<X509CertImpl> coll = new ArrayList<>();
    // Test the InputStream for end-of-stream.  If the stream's
    // initial state is already at end-of-stream then return
    // an empty collection.  Otherwise, push the byte back into the
    // stream and let readOneBlock look for the first certificate.
    peekByte = pbis.read();
    if (peekByte == -1) {
        return new ArrayList<>(0);
    } else {
        pbis.unread(peekByte);
        data = readOneBlock(pbis);
    }
    // data has been found.
    if (data == null) {
        throw new CertificateException("No certificate data found");
    }
    try {
        PKCS7 pkcs7 = new PKCS7(data);
        X509Certificate[] certs = pkcs7.getCertificates();
        // certs are optional in PKCS #7
        if (certs != null) {
            return Arrays.asList(certs);
        } else {
            // no certificates provided
            return new ArrayList<>(0);
        }
    } catch (ParsingException e) {
        while (data != null) {
            coll.add(new X509CertImpl(data));
            data = readOneBlock(pbis);
        }
    }
    return coll;
}
Also used : PKCS7(sun.security.pkcs.PKCS7) X509CertImpl(sun.security.x509.X509CertImpl) ParsingException(sun.security.pkcs.ParsingException)

Aggregations

PKCS7 (sun.security.pkcs.PKCS7)6 ParsingException (sun.security.pkcs.ParsingException)6 X509CRLImpl (sun.security.x509.X509CRLImpl)3 X509CertImpl (sun.security.x509.X509CertImpl)3 ArrayList (java.util.ArrayList)2 X509CRL (java.security.cert.X509CRL)1 X509Certificate (java.security.cert.X509Certificate)1