Search in sources :

Example 11 with X509CRLImpl

use of sun.security.x509.X509CRLImpl in project jdk8u_jdk by JetBrains.

the class PKCS7 method parseSignedData.

private void parseSignedData(DerValue val) throws ParsingException, IOException {
    DerInputStream dis = val.toDerInputStream();
    // Version
    version = dis.getBigInteger();
    // digestAlgorithmIds
    DerValue[] digestAlgorithmIdVals = dis.getSet(1);
    int len = digestAlgorithmIdVals.length;
    digestAlgorithmIds = new AlgorithmId[len];
    try {
        for (int i = 0; i < len; i++) {
            DerValue oid = digestAlgorithmIdVals[i];
            digestAlgorithmIds[i] = AlgorithmId.parse(oid);
        }
    } catch (IOException e) {
        ParsingException pe = new ParsingException("Error parsing digest AlgorithmId IDs: " + e.getMessage());
        pe.initCause(e);
        throw pe;
    }
    // contentInfo
    contentInfo = new ContentInfo(dis);
    CertificateFactory certfac = null;
    try {
        certfac = CertificateFactory.getInstance("X.509");
    } catch (CertificateException ce) {
    // do nothing
    }
    /*
         * check if certificates (implicit tag) are provided
         * (certificates are OPTIONAL)
         */
    if ((byte) (dis.peekByte()) == (byte) 0xA0) {
        DerValue[] certVals = dis.getSet(2, true);
        len = certVals.length;
        certificates = new X509Certificate[len];
        int count = 0;
        for (int i = 0; i < len; i++) {
            ByteArrayInputStream bais = null;
            try {
                byte tag = certVals[i].getTag();
                // CertificateChoices ignored.
                if (tag == DerValue.tag_Sequence) {
                    if (certfac == null) {
                        certificates[count] = new X509CertImpl(certVals[i]);
                    } else {
                        byte[] encoded = certVals[i].toByteArray();
                        bais = new ByteArrayInputStream(encoded);
                        certificates[count] = (X509Certificate) certfac.generateCertificate(bais);
                        bais.close();
                        bais = null;
                    }
                    count++;
                }
            } catch (CertificateException ce) {
                ParsingException pe = new ParsingException(ce.getMessage());
                pe.initCause(ce);
                throw pe;
            } catch (IOException ioe) {
                ParsingException pe = new ParsingException(ioe.getMessage());
                pe.initCause(ioe);
                throw pe;
            } finally {
                if (bais != null)
                    bais.close();
            }
        }
        if (count != len) {
            certificates = Arrays.copyOf(certificates, count);
        }
    }
    // check if crls (implicit tag) are provided (crls are OPTIONAL)
    if ((byte) (dis.peekByte()) == (byte) 0xA1) {
        DerValue[] crlVals = dis.getSet(1, true);
        len = crlVals.length;
        crls = new X509CRL[len];
        for (int i = 0; i < len; i++) {
            ByteArrayInputStream bais = null;
            try {
                if (certfac == null)
                    crls[i] = new X509CRLImpl(crlVals[i]);
                else {
                    byte[] encoded = crlVals[i].toByteArray();
                    bais = new ByteArrayInputStream(encoded);
                    crls[i] = (X509CRL) certfac.generateCRL(bais);
                    bais.close();
                    bais = null;
                }
            } catch (CRLException e) {
                ParsingException pe = new ParsingException(e.getMessage());
                pe.initCause(e);
                throw pe;
            } finally {
                if (bais != null)
                    bais.close();
            }
        }
    }
    // signerInfos
    DerValue[] signerInfoVals = dis.getSet(1);
    len = signerInfoVals.length;
    signerInfos = new SignerInfo[len];
    for (int i = 0; i < len; i++) {
        DerInputStream in = signerInfoVals[i].toDerInputStream();
        signerInfos[i] = new SignerInfo(in);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509CertImpl(sun.security.x509.X509CertImpl) X509CRLImpl(sun.security.x509.X509CRLImpl) CRLException(java.security.cert.CRLException)

Example 12 with X509CRLImpl

use of sun.security.x509.X509CRLImpl in project jdk8u_jdk by JetBrains.

the class X509Factory method intern.

/**
     * Return an interned X509CRLImpl for the given certificate.
     * For more information, see intern(X509Certificate).
     *
     * @param c The source X509CRL
     * @return An X509CRLImpl object that is either a cached CRL or a
     *      newly built X509CRLImpl from the provided X509CRL
     * @throws CRLException if failures occur while obtaining the DER
     *      encoding for CRL data.
     */
public static synchronized X509CRLImpl intern(X509CRL c) throws CRLException {
    if (c == null) {
        return null;
    }
    boolean isImpl = c instanceof X509CRLImpl;
    byte[] encoding;
    if (isImpl) {
        encoding = ((X509CRLImpl) c).getEncodedInternal();
    } else {
        encoding = c.getEncoded();
    }
    X509CRLImpl newC = getFromCache(crlCache, encoding);
    if (newC != null) {
        return newC;
    }
    if (isImpl) {
        newC = (X509CRLImpl) c;
    } else {
        newC = new X509CRLImpl(encoding);
        encoding = newC.getEncodedInternal();
    }
    addToCache(crlCache, encoding, newC);
    return newC;
}
Also used : X509CRLImpl(sun.security.x509.X509CRLImpl)

Example 13 with X509CRLImpl

use of sun.security.x509.X509CRLImpl in project Bytecoder by mirkosertic.

the class PKCS7 method parseSignedData.

private void parseSignedData(DerValue val) throws ParsingException, IOException {
    DerInputStream dis = val.toDerInputStream();
    // Version
    version = dis.getBigInteger();
    // digestAlgorithmIds
    DerValue[] digestAlgorithmIdVals = dis.getSet(1);
    int len = digestAlgorithmIdVals.length;
    digestAlgorithmIds = new AlgorithmId[len];
    try {
        for (int i = 0; i < len; i++) {
            DerValue oid = digestAlgorithmIdVals[i];
            digestAlgorithmIds[i] = AlgorithmId.parse(oid);
        }
    } catch (IOException e) {
        ParsingException pe = new ParsingException("Error parsing digest AlgorithmId IDs: " + e.getMessage());
        pe.initCause(e);
        throw pe;
    }
    // contentInfo
    contentInfo = new ContentInfo(dis);
    CertificateFactory certfac = null;
    try {
        certfac = CertificateFactory.getInstance("X.509");
    } catch (CertificateException ce) {
    // do nothing
    }
    /*
         * check if certificates (implicit tag) are provided
         * (certificates are OPTIONAL)
         */
    if ((byte) (dis.peekByte()) == (byte) 0xA0) {
        DerValue[] certVals = dis.getSet(2, true);
        len = certVals.length;
        certificates = new X509Certificate[len];
        int count = 0;
        for (int i = 0; i < len; i++) {
            ByteArrayInputStream bais = null;
            try {
                byte tag = certVals[i].getTag();
                // CertificateChoices ignored.
                if (tag == DerValue.tag_Sequence) {
                    if (certfac == null) {
                        certificates[count] = new X509CertImpl(certVals[i]);
                    } else {
                        byte[] encoded = certVals[i].toByteArray();
                        bais = new ByteArrayInputStream(encoded);
                        certificates[count] = (X509Certificate) certfac.generateCertificate(bais);
                        bais.close();
                        bais = null;
                    }
                    count++;
                }
            } catch (CertificateException ce) {
                ParsingException pe = new ParsingException(ce.getMessage());
                pe.initCause(ce);
                throw pe;
            } catch (IOException ioe) {
                ParsingException pe = new ParsingException(ioe.getMessage());
                pe.initCause(ioe);
                throw pe;
            } finally {
                if (bais != null)
                    bais.close();
            }
        }
        if (count != len) {
            certificates = Arrays.copyOf(certificates, count);
        }
    }
    // check if crls (implicit tag) are provided (crls are OPTIONAL)
    if ((byte) (dis.peekByte()) == (byte) 0xA1) {
        DerValue[] crlVals = dis.getSet(1, true);
        len = crlVals.length;
        crls = new X509CRL[len];
        for (int i = 0; i < len; i++) {
            ByteArrayInputStream bais = null;
            try {
                if (certfac == null)
                    crls[i] = new X509CRLImpl(crlVals[i]);
                else {
                    byte[] encoded = crlVals[i].toByteArray();
                    bais = new ByteArrayInputStream(encoded);
                    crls[i] = (X509CRL) certfac.generateCRL(bais);
                    bais.close();
                    bais = null;
                }
            } catch (CRLException e) {
                ParsingException pe = new ParsingException(e.getMessage());
                pe.initCause(e);
                throw pe;
            } finally {
                if (bais != null)
                    bais.close();
            }
        }
    }
    // signerInfos
    DerValue[] signerInfoVals = dis.getSet(1);
    len = signerInfoVals.length;
    signerInfos = new SignerInfo[len];
    for (int i = 0; i < len; i++) {
        DerInputStream in = signerInfoVals[i].toDerInputStream();
        signerInfos[i] = new SignerInfo(in);
    }
}
Also used : CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509CertImpl(sun.security.x509.X509CertImpl) X509CRLImpl(sun.security.x509.X509CRLImpl) CRLException(java.security.cert.CRLException)

Example 14 with X509CRLImpl

use of sun.security.x509.X509CRLImpl in project Bytecoder by mirkosertic.

the class X509Factory method parseX509orPKCS7CRL.

/*
     * Parses the data in the given input stream as a sequence of DER encoded
     * X.509 CRLs (in binary or base 64 encoded format) OR as a single PKCS#7
     * encoded blob (in binary or base 64 encoded format).
     */
private Collection<? extends java.security.cert.CRL> parseX509orPKCS7CRL(InputStream is) throws CRLException, IOException {
    int peekByte;
    byte[] data;
    PushbackInputStream pbis = new PushbackInputStream(is);
    Collection<X509CRLImpl> 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 CRL.
    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 CRLException("No CRL data found");
    }
    try {
        PKCS7 pkcs7 = new PKCS7(data);
        X509CRL[] crls = pkcs7.getCRLs();
        // CRLs are optional in PKCS #7
        if (crls != null) {
            return Arrays.asList(crls);
        } else {
            // no crls provided
            return new ArrayList<>(0);
        }
    } catch (ParsingException e) {
        while (data != null) {
            coll.add(new X509CRLImpl(data));
            data = readOneBlock(pbis);
        }
    }
    return coll;
}
Also used : PKCS7(sun.security.pkcs.PKCS7) ParsingException(sun.security.pkcs.ParsingException) X509CRLImpl(sun.security.x509.X509CRLImpl)

Example 15 with X509CRLImpl

use of sun.security.x509.X509CRLImpl in project Bytecoder by mirkosertic.

the class X509Factory method intern.

/**
 * Return an interned X509CRLImpl for the given certificate.
 * For more information, see intern(X509Certificate).
 *
 * @param c The source X509CRL
 * @return An X509CRLImpl object that is either a cached CRL or a
 *      newly built X509CRLImpl from the provided X509CRL
 * @throws CRLException if failures occur while obtaining the DER
 *      encoding for CRL data.
 */
public static synchronized X509CRLImpl intern(X509CRL c) throws CRLException {
    if (c == null) {
        return null;
    }
    boolean isImpl = c instanceof X509CRLImpl;
    byte[] encoding;
    if (isImpl) {
        encoding = ((X509CRLImpl) c).getEncodedInternal();
    } else {
        encoding = c.getEncoded();
    }
    X509CRLImpl newC = getFromCache(crlCache, encoding);
    if (newC != null) {
        return newC;
    }
    if (isImpl) {
        newC = (X509CRLImpl) c;
    } else {
        newC = new X509CRLImpl(encoding);
        encoding = newC.getEncodedInternal();
    }
    addToCache(crlCache, encoding, newC);
    return newC;
}
Also used : X509CRLImpl(sun.security.x509.X509CRLImpl)

Aggregations

X509CRLImpl (sun.security.x509.X509CRLImpl)18 CRLException (java.security.cert.CRLException)10 CertificateException (java.security.cert.CertificateException)6 X509CertImpl (sun.security.x509.X509CertImpl)6 X509CRL (java.security.cert.X509CRL)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)3 CertificateFactory (java.security.cert.CertificateFactory)3 PKCS7 (sun.security.pkcs.PKCS7)3 ParsingException (sun.security.pkcs.ParsingException)3 AlgorithmId (sun.security.x509.AlgorithmId)3 IOException (java.io.IOException)1 CertificateParsingException (java.security.cert.CertificateParsingException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1