Search in sources :

Example 51 with CRLException

use of java.security.cert.CRLException in project robovm by robovm.

the class CertificateFactory4Test method test_generateCRLLjava_io_InputStream.

/**
     * java.security.cert.CertificateFactory#generateCRL(java.io.InputStream)
     */
public void test_generateCRLLjava_io_InputStream() throws Exception {
    CertificateFactory fact = CertificateFactory.getInstance("X.509");
    for (int i = 0; i < CRL_URLS.length; i++) {
        URL certUrl = new URL(BASE_URL + CRL_URLS[i]);
        try {
            InputStream is = certUrl.openStream();
            CRL crl = fact.generateCRL(is);
            assertNotNull("The CRL in \"" + certUrl.toExternalForm() + "\" were not parsed correctly", crl);
        } catch (IOException e) {
        // the certificate could not be found, skip it
        } catch (CRLException e) {
            fail("An exception was thrown while parsing \"" + certUrl.toExternalForm() + "\": " + e.getMessage());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) CRL(java.security.cert.CRL) CertificateFactory(java.security.cert.CertificateFactory) CRLException(java.security.cert.CRLException) URL(java.net.URL)

Example 52 with CRLException

use of java.security.cert.CRLException in project robovm by robovm.

the class CertificateFactorySpiTest method testCertificateFactorySpi01.

/**
     * Test for <code>CertificateFactorySpi</code> constructor
     * Assertion: constructs CertificateFactorySpi
     */
public void testCertificateFactorySpi01() throws CertificateException, CRLException {
    CertificateFactorySpi certFactorySpi = new extCertificateFactorySpi();
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
    try {
        certFactorySpi.engineGenerateCertPath(bais);
        fail("UnsupportedOperationException must be thrown");
    } catch (UnsupportedOperationException e) {
    }
    try {
        certFactorySpi.engineGenerateCertPath(bais, "");
        fail("UnsupportedOperationException must be thrown");
    } catch (UnsupportedOperationException e) {
    }
    try {
        List<Certificate> list = null;
        certFactorySpi.engineGenerateCertPath(list);
        fail("UnsupportedOperationException must be thrown");
    } catch (UnsupportedOperationException e) {
    }
    try {
        certFactorySpi.engineGetCertPathEncodings();
        fail("UnsupportedOperationException must be thrown");
    } catch (UnsupportedOperationException e) {
    }
    Certificate cc = certFactorySpi.engineGenerateCertificate(bais);
    assertNull("Not null Cerificate", cc);
    try {
        certFactorySpi.engineGenerateCertificate(null);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    Collection<? extends Certificate> col = certFactorySpi.engineGenerateCertificates(bais);
    assertNull("Not null Collection", col);
    try {
        certFactorySpi.engineGenerateCertificates(null);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    CRL ccCRL = certFactorySpi.engineGenerateCRL(bais);
    assertNull("Not null CRL", ccCRL);
    try {
        certFactorySpi.engineGenerateCRL(null);
        fail("CRLException must be thrown");
    } catch (CRLException e) {
    }
    Collection<? extends CRL> colCRL = certFactorySpi.engineGenerateCRLs(bais);
    assertNull("Not null CRL", colCRL);
    try {
        certFactorySpi.engineGenerateCRLs(null);
        fail("CRLException must be thrown");
    } catch (CRLException e) {
    }
}
Also used : CertificateFactorySpi(java.security.cert.CertificateFactorySpi) MyCertificateFactorySpi(org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi) ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) CRL(java.security.cert.CRL) CRLException(java.security.cert.CRLException) Certificate(java.security.cert.Certificate)

Example 53 with CRLException

use of java.security.cert.CRLException in project robovm by robovm.

the class CertificateFactorySpiTest method testCertificateFactorySpi03.

/**
     * Test for <code>CertificateFactorySpi</code> constructor
     * Assertion: constructs CertificateFactorySpi
     */
public void testCertificateFactorySpi03() throws CertificateException, CRLException {
    CertificateFactorySpi certFactorySpi = new MyCertificateFactorySpi();
    MyCertificateFactorySpi.putMode(false);
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
    DataInputStream dis = new DataInputStream(bais);
    try {
        certFactorySpi.engineGenerateCertPath(bais);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    try {
        certFactorySpi.engineGenerateCertPath(dis);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    try {
        certFactorySpi.engineGenerateCertPath(bais, "aa");
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    certFactorySpi.engineGenerateCertPath(dis, "");
    certFactorySpi.engineGenerateCertPath(dis, "ss");
    try {
        certFactorySpi.engineGenerateCertificate(bais);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    try {
        certFactorySpi.engineGenerateCertificates(null);
        fail("CertificateException must be thrown");
    } catch (CertificateException e) {
    }
    Certificate cert = certFactorySpi.engineGenerateCertificate(dis);
    assertNull("Result must be null", cert);
    Collection<? extends Certificate> col = certFactorySpi.engineGenerateCertificates(dis);
    assertNull("Result must be null", col);
    try {
        certFactorySpi.engineGenerateCRL(bais);
        fail("CRLException must be thrown");
    } catch (CRLException e) {
    }
    try {
        certFactorySpi.engineGenerateCRLs(null);
        fail("CRLException must be thrown");
    } catch (CRLException e) {
    }
    CRL crl = certFactorySpi.engineGenerateCRL(dis);
    assertNull("Result must be null", crl);
    Collection<? extends CRL> colcrl = certFactorySpi.engineGenerateCRLs(dis);
    assertNull("Result must be null", colcrl);
    List<Certificate> list = null;
    certFactorySpi.engineGenerateCertPath(list);
    Iterator<String> enc = certFactorySpi.engineGetCertPathEncodings();
    assertFalse("Incorrect Iterator", enc.hasNext());
}
Also used : CertificateFactorySpi(java.security.cert.CertificateFactorySpi) MyCertificateFactorySpi(org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi) CertificateException(java.security.cert.CertificateException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MyCertificateFactorySpi(org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi) CRL(java.security.cert.CRL) CRLException(java.security.cert.CRLException) Certificate(java.security.cert.Certificate)

Example 54 with CRLException

use of java.security.cert.CRLException in project XobotOS by xamarin.

the class X509CertFactoryImpl method getCRL.

/**
     * Returns the CRL object corresponding to the encoding provided
     * by the stream.
     * Resulting object is retrieved from the cache
     * if it contains such correspondence
     * and is constructed on the base of encoding
     * and stored in the cache otherwise.
     * @throws IOException if some decoding errors occur
     * (in the case of cache miss).
     */
private static CRL getCRL(InputStream inStream) throws CRLException, IOException {
    synchronized (CRL_CACHE) {
        inStream.mark(CRL_CACHE_SEED_LENGTH);
        byte[] buff = readBytes(inStream, CRL_CACHE_SEED_LENGTH);
        // read the prefix of the encoding
        inStream.reset();
        if (buff == null) {
            throw new CRLException("InputStream doesn't contain enough data");
        }
        long hash = CRL_CACHE.getHash(buff);
        if (CRL_CACHE.contains(hash)) {
            byte[] encoding = new byte[BerInputStream.getLength(buff)];
            if (encoding.length < CRL_CACHE_SEED_LENGTH) {
                throw new CRLException("Bad CRL encoding");
            }
            Streams.readFully(inStream, encoding);
            CRL res = (CRL) CRL_CACHE.get(hash, encoding);
            if (res != null) {
                return res;
            }
            res = new X509CRLImpl(encoding);
            CRL_CACHE.put(hash, encoding, res);
            return res;
        } else {
            X509CRL res = new X509CRLImpl(inStream);
            CRL_CACHE.put(hash, res.getEncoded(), res);
            return res;
        }
    }
}
Also used : X509CRL(java.security.cert.X509CRL) X509CRL(java.security.cert.X509CRL) CRL(java.security.cert.CRL) CRLException(java.security.cert.CRLException)

Example 55 with CRLException

use of java.security.cert.CRLException in project XobotOS by xamarin.

the class X509CertFactoryImpl method engineGenerateCRLs.

/**
     * @see java.security.cert.CertificateFactorySpi#engineGenerateCRLs(InputStream)
     * method documentation for more info
     */
public Collection<? extends CRL> engineGenerateCRLs(InputStream inStream) throws CRLException {
    if (inStream == null) {
        throw new CRLException("inStream == null");
    }
    ArrayList<CRL> result = new ArrayList<CRL>();
    try {
        if (!inStream.markSupported()) {
            inStream = new RestoringInputStream(inStream);
        }
        // if it is PEM encoded form this array will contain the encoding
        // so ((it is PEM) <-> (encoding != null))
        byte[] encoding = null;
        // The following by SEQUENCE ASN.1 tag, used for
        // recognizing the data format
        // (is it PKCS7 ContentInfo structure, X.509 CRL, or
        // unsupported encoding)
        int second_asn1_tag = -1;
        inStream.mark(1);
        int ch;
        while ((ch = inStream.read()) != -1) {
            // check if it is PEM encoded form
            if (ch == '-') {
                // beginning of PEM encoding ('-' char)
                // decode PEM chunk and store its content (ASN.1 encoding)
                encoding = decodePEM(inStream, FREE_BOUND_SUFFIX);
            } else if (ch == 0x30) {
                // beginning of ASN.1 sequence (0x30)
                encoding = null;
                inStream.reset();
                // prepare for data format determination
                inStream.mark(CRL_CACHE_SEED_LENGTH);
            } else {
                // unsupported data
                if (result.size() == 0) {
                    throw new CRLException("Unsupported encoding");
                } else {
                    // it can be trailing user data,
                    // so keep it in the stream
                    inStream.reset();
                    return result;
                }
            }
            // Check the data format
            BerInputStream in = (encoding == null) ? new BerInputStream(inStream) : new BerInputStream(encoding);
            // read the next ASN.1 tag
            second_asn1_tag = in.next();
            if (encoding == null) {
                // keep whole structure in the stream
                inStream.reset();
            }
            // check if it is a TBSCertList structure
            if (second_asn1_tag != ASN1Constants.TAG_C_SEQUENCE) {
                if (result.size() == 0) {
                    // whether it is PKCS7 structure
                    break;
                } else {
                    // so return what we already read
                    return result;
                }
            } else {
                if (encoding == null) {
                    result.add(getCRL(inStream));
                } else {
                    result.add(getCRL(encoding));
                }
            }
            inStream.mark(1);
        }
        if (result.size() != 0) {
            // the stream was read out
            return result;
        } else if (ch == -1) {
            throw new CRLException("There is no data in the stream");
        }
        // else: check if it is PKCS7
        if (second_asn1_tag == ASN1Constants.TAG_OID) {
            // it is PKCS7 ContentInfo structure, so decode it
            ContentInfo info = (ContentInfo) ((encoding != null) ? ContentInfo.ASN1.decode(encoding) : ContentInfo.ASN1.decode(inStream));
            // retrieve SignedData
            SignedData data = info.getSignedData();
            if (data == null) {
                throw new CRLException("Invalid PKCS7 data provided");
            }
            List<CertificateList> crls = data.getCRLs();
            if (crls != null) {
                for (CertificateList crl : crls) {
                    result.add(new X509CRLImpl(crl));
                }
            }
            return result;
        }
        // else: Unknown data format
        throw new CRLException("Unsupported encoding");
    } catch (IOException e) {
        throw new CRLException(e);
    }
}
Also used : SignedData(org.apache.harmony.security.pkcs7.SignedData) ArrayList(java.util.ArrayList) CertificateList(org.apache.harmony.security.x509.CertificateList) IOException(java.io.IOException) ContentInfo(org.apache.harmony.security.pkcs7.ContentInfo) X509CRL(java.security.cert.X509CRL) CRL(java.security.cert.CRL) BerInputStream(org.apache.harmony.security.asn1.BerInputStream) CRLException(java.security.cert.CRLException)

Aggregations

CRLException (java.security.cert.CRLException)63 IOException (java.io.IOException)26 CertificateException (java.security.cert.CertificateException)21 X509CRL (java.security.cert.X509CRL)14 CRL (java.security.cert.CRL)11 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Signature (java.security.Signature)8 CertificateFactory (java.security.cert.CertificateFactory)8 SignatureException (java.security.SignatureException)6 InputStream (java.io.InputStream)5 Certificate (java.security.cert.Certificate)5 X509CRLImpl (sun.security.x509.X509CRLImpl)5 CertificateParsingException (java.security.cert.CertificateParsingException)4 ArrayList (java.util.ArrayList)4 DataInputStream (java.io.DataInputStream)3 CertificateFactorySpi (java.security.cert.CertificateFactorySpi)3 X509CRLEntry (java.security.cert.X509CRLEntry)3 X509Certificate (java.security.cert.X509Certificate)3 X500Principal (javax.security.auth.x500.X500Principal)3 MyCertificateFactorySpi (org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi)3