Search in sources :

Example 16 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier 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 17 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project robovm by robovm.

the class HostnameVerifierTest method test_verify.

/**
     * javax.net.ssl.HostnameVerifier#verify(String hostname, SSLSession
     *        session)
     */
public final void test_verify() throws Exception {
    mySSLSession session = new mySSLSession("localhost", 1080, null);
    HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
    assertFalse(hv.verify("localhost", session));
}
Also used : org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 18 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project robovm by robovm.

the class myHostnameVerifier method test_setDefaultHostnameVerifier.

/**
     * javax.net.ssl.HttpsURLConnection#setDefaultHostnameVerifier()
     */
public final void test_setDefaultHostnameVerifier() {
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(null);
        fail("No expected IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    HostnameVerifier def = HttpsURLConnection.getDefaultHostnameVerifier();
    try {
        myHostnameVerifier hnv = new myHostnameVerifier();
        HttpsURLConnection.setDefaultHostnameVerifier(hnv);
        assertEquals(hnv, HttpsURLConnection.getDefaultHostnameVerifier());
    } finally {
        HttpsURLConnection.setDefaultHostnameVerifier(def);
    }
}
Also used : HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 19 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project robovm by robovm.

the class myHostnameVerifier method test_getDefaultHostnameVerifier.

/**
     * javax.net.ssl.HttpsURLConnection#getDefaultHostnameVerifier()
     */
public final void test_getDefaultHostnameVerifier() {
    HostnameVerifier verifyer = HttpsURLConnection.getDefaultHostnameVerifier();
    assertNotNull("Default hostname verifyer is null", verifyer);
}
Also used : HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 20 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project helios by spotify.

the class HostnameVerifierProviderTest method testHostnameVerificationDisabled.

@Test
public void testHostnameVerificationDisabled() {
    final HostnameVerifierProvider provider = new HostnameVerifierProvider(false, delegate);
    final HostnameVerifier verifier = provider.verifierFor("any.host");
    assertTrue(verifier.verify("example.com", sslSession));
    verifyNoMoreInteractions(delegate, sslSession);
}
Also used : HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Aggregations

HostnameVerifier (javax.net.ssl.HostnameVerifier)94 SSLSession (javax.net.ssl.SSLSession)41 SSLContext (javax.net.ssl.SSLContext)30 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)27 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)24 TrustManager (javax.net.ssl.TrustManager)19 IOException (java.io.IOException)18 URL (java.net.URL)18 X509Certificate (java.security.cert.X509Certificate)17 X509TrustManager (javax.net.ssl.X509TrustManager)17 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)14 SecureRandom (java.security.SecureRandom)14 InputStream (java.io.InputStream)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 CertificateException (java.security.cert.CertificateException)10 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)10 KeyManagementException (java.security.KeyManagementException)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8