Search in sources :

Example 26 with CRL

use of java.security.cert.CRL 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 27 with CRL

use of java.security.cert.CRL 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 28 with CRL

use of java.security.cert.CRL 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 29 with CRL

use of java.security.cert.CRL 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)

Example 30 with CRL

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

the class JDKX509CertificateFactory method engineGenerateCRLs.

/**
     * Returns a (possibly empty) collection view of the CRLs read from
     * the given input stream inStream.
     *
     * The inStream may contain a sequence of DER-encoded CRLs, or
     * a PKCS#7 CRL set.  This is a PKCS#7 SignedData object, with the
     * only signficant field being crls.  In particular the signature
     * and the contents are ignored.
     */
public Collection engineGenerateCRLs(InputStream inStream) throws CRLException {
    CRL crl;
    List crls = new ArrayList();
    while ((crl = engineGenerateCRL(inStream)) != null) {
        crls.add(crl);
    }
    return crls;
}
Also used : ArrayList(java.util.ArrayList) CertificateList(org.bouncycastle.asn1.x509.CertificateList) ArrayList(java.util.ArrayList) List(java.util.List) CRL(java.security.cert.CRL)

Aggregations

CRL (java.security.cert.CRL)37 X509CRL (java.security.cert.X509CRL)16 CRLException (java.security.cert.CRLException)11 CertificateException (java.security.cert.CertificateException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 Certificate (java.security.cert.Certificate)5 IOException (java.io.IOException)4 CertificateFactory (java.security.cert.CertificateFactory)4 DataInputStream (java.io.DataInputStream)3 InputStream (java.io.InputStream)3 CertificateFactorySpi (java.security.cert.CertificateFactorySpi)3 X509Certificate (java.security.cert.X509Certificate)3 List (java.util.List)3 FileInputStream (java.io.FileInputStream)2 X509CRLSelector (java.security.cert.X509CRLSelector)2 Iterator (java.util.Iterator)2 MyCertificateFactorySpi (org.apache.harmony.security.tests.support.cert.MyCertificateFactorySpi)2 AndroidOnly (dalvik.annotation.AndroidOnly)1