Search in sources :

Example 71 with CertificateFactory

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

the class KeyStore2Test method test_aliases.

/**
     * java.security.KeyStore#aliases()
     */
public void test_aliases() throws Exception {
    // Test for method java.util.Enumeration
    // java.security.KeyStore.aliases()
    // NOT COMPATIBLE WITH PCS#12
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.aliases();
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // KeyStore keyTest =
    // KeyStore.getInstance(KeyStore.getDefaultType());
    // alias 1
    keyTest.setCertificateEntry("alias1", cert[0]);
    // alias 2
    keyTest.setCertificateEntry("alias2", cert[0]);
    // alias 3
    keyTest.setCertificateEntry("alias3", cert[0]);
    // obtaining the aliase
    Enumeration<String> aliase = keyTest.aliases();
    Set<String> alia = new HashSet<String>();
    int i = 0;
    while (aliase.hasMoreElements()) {
        alia.add(aliase.nextElement());
        i++;
    }
    assertEquals("the wrong aliases were returned", i, 3);
    assertTrue("the wrong aliases were returned", alia.contains("alias1"));
    assertTrue("the wrong aliases were returned", alia.contains("alias2"));
    assertTrue("the wrong aliases were returned", alia.contains("alias3"));
}
Also used : KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) HashSet(java.util.HashSet)

Example 72 with CertificateFactory

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

the class HostnameVerifierTest method testSubjectAlt.

public void testSubjectAlt() throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream in = new ByteArrayInputStream(X509_MULTIPLE_SUBJECT_ALT);
    X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
    mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
    HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
    assertEquals(new X500Principal("CN=localhost"), x509.getSubjectX500Principal());
    assertTrue(verifier.verify("localhost", session));
    assertTrue(verifier.verify("localhost.localdomain", session));
    assertFalse(verifier.verify("local.host", session));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) X500Principal(javax.security.auth.x500.X500Principal) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 73 with CertificateFactory

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

the class HostnameVerifierTest method testSubjectAltName.

public void testSubjectAltName() throws Exception {
    /*
         * $ cat ./cert.cnf
         * [req]
         * distinguished_name=distinguished_name
         * req_extensions=req_extensions
         * x509_extensions=x509_extensions
         * [distinguished_name]
         * [req_extensions]
         * [x509_extensions]
         * subjectAltName=DNS:bar.com,DNS:baz.com
         *
         * $ openssl req -x509 -nodes -days 36500 -subj '/CN=foo.com' -config ./cert.cnf \
         *     -newkey rsa:512 -out cert.pem
         */
    String cert = "-----BEGIN CERTIFICATE-----\n" + "MIIBPTCB6KADAgECAgkA7zoHaaqNGHQwDQYJKoZIhvcNAQEFBQAwEjEQMA4GA1UE\n" + "AxMHZm9vLmNvbTAgFw0xMDEyMjAxODM5MzZaGA8yMTEwMTEyNjE4MzkzNlowEjEQ\n" + "MA4GA1UEAxMHZm9vLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC+gmoSxF+8\n" + "hbV+rgRQqHIJd50216OWQJbU3BvdlPbca779NYO4+UZWTFdBM8BdQqs3H4B5Agvp\n" + "y7HeSff1F7XRAgMBAAGjHzAdMBsGA1UdEQQUMBKCB2Jhci5jb22CB2Jhei5jb20w\n" + "DQYJKoZIhvcNAQEFBQADQQBXpZZPOY2Dy1lGG81JTr8L4or9jpKacD7n51eS8iqI\n" + "oTznPNuXHU5bFN0AAGX2ij47f/EahqTpo5RdS95P4sVm\n" + "-----END CERTIFICATE-----";
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream in = new ByteArrayInputStream(cert.getBytes("UTF-8"));
    X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
    mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
    HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
    assertFalse(verifier.verify("foo.com", session));
    assertTrue(verifier.verify("bar.com", session));
    assertTrue(verifier.verify("baz.com", session));
    assertFalse(verifier.verify("a.foo.com", session));
    assertFalse(verifier.verify("quux.com", session));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 74 with CertificateFactory

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

the class HostnameVerifierTest method testSubjectAltNameWithWildcard.

public void testSubjectAltNameWithWildcard() throws Exception {
    /*
         * $ cat ./cert.cnf
         * [req]
         * distinguished_name=distinguished_name
         * req_extensions=req_extensions
         * x509_extensions=x509_extensions
         * [distinguished_name]
         * [req_extensions]
         * [x509_extensions]
         * subjectAltName=DNS:bar.com,DNS:*.baz.com
         *
         * $ openssl req -x509 -nodes -days 36500 -subj '/CN=foo.com' -config ./cert.cnf \
         *     -newkey rsa:512 -out cert.pem
         */
    String cert = "-----BEGIN CERTIFICATE-----\n" + "MIIBPzCB6qADAgECAgkAnv/7Jv5r7pMwDQYJKoZIhvcNAQEFBQAwEjEQMA4GA1UE\n" + "AxMHZm9vLmNvbTAgFw0xMDEyMjAxODQ2MDFaGA8yMTEwMTEyNjE4NDYwMVowEjEQ\n" + "MA4GA1UEAxMHZm9vLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDAz2YXnyog\n" + "YdYLSFr/OEgSumtwqtZKJTB4wqTW/eKbBCEzxnyUMxWZIqUGu353PzwfOuWp2re3\n" + "nvVV+QDYQlh9AgMBAAGjITAfMB0GA1UdEQQWMBSCB2Jhci5jb22CCSouYmF6LmNv\n" + "bTANBgkqhkiG9w0BAQUFAANBAB8yrSl8zqy07i0SNYx2B/FnvQY734pxioaqFWfO\n" + "Bqo1ZZl/9aPHEWIwBrxYNVB0SGu/kkbt/vxqOjzzrkXukmI=\n" + "-----END CERTIFICATE-----";
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream in = new ByteArrayInputStream(cert.getBytes("UTF-8"));
    X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
    mySSLSession session = new mySSLSession(new X509Certificate[] { x509 });
    HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
    assertFalse(verifier.verify("foo.com", session));
    assertTrue(verifier.verify("bar.com", session));
    assertTrue(verifier.verify("a.baz.com", session));
    assertTrue(verifier.verify("baz.com", session));
    assertFalse(verifier.verify("a.foo.com", session));
    assertFalse(verifier.verify("a.bar.com", session));
    assertFalse(verifier.verify("quux.com", session));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 75 with CertificateFactory

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

the class myHostnameVerifier method getServerCertificates.

/*
     * @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
     */
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
    try {
        CertificateFactory cf = CertificateFactory.getInstance(typeDone);
        byte[] barr = TestUtils.getX509Certificate_v3();
        ByteArrayInputStream bis = new ByteArrayInputStream(barr);
        Certificate cert = cf.generateCertificate(bis);
        return new Certificate[] { cert };
    } catch (CertificateException se) {
        throw new SSLPeerUnverifiedException("No server's end-entity certificate");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) Certificate(java.security.cert.Certificate)

Aggregations

CertificateFactory (java.security.cert.CertificateFactory)550 X509Certificate (java.security.cert.X509Certificate)409 ByteArrayInputStream (java.io.ByteArrayInputStream)372 Certificate (java.security.cert.Certificate)272 CertificateException (java.security.cert.CertificateException)120 KeyFactory (java.security.KeyFactory)103 PrivateKey (java.security.PrivateKey)93 InputStream (java.io.InputStream)92 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)86 IOException (java.io.IOException)80 KeyStore (java.security.KeyStore)77 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)67 Entry (java.security.KeyStore.Entry)59 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)59 KeyStoreException (java.security.KeyStoreException)49 ArrayList (java.util.ArrayList)49 FileInputStream (java.io.FileInputStream)47 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 File (java.io.File)23 HashSet (java.util.HashSet)21