Search in sources :

Example 46 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project okhttp by square.

the class URLConnectionTest method testConnectViaSocketFactory.

public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
    SocketFactory uselessSocketFactory = new SocketFactory() {

        public Socket createSocket() {
            throw new IllegalArgumentException("useless");
        }

        public Socket createSocket(InetAddress host, int port) {
            return null;
        }

        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
            return null;
        }

        public Socket createSocket(String host, int port) {
            return null;
        }

        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
            return null;
        }
    };
    if (useHttps) {
        server.useHttps(sslClient.socketFactory, false);
        urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    }
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getResponseCode();
        fail();
    } catch (IllegalArgumentException expected) {
    }
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
    connection = urlFactory.open(server.url("/").url());
    assertEquals(200, connection.getResponseCode());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) InetAddress(java.net.InetAddress)

Example 47 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project zaproxy by zaproxy.

the class RelaxedX509TrustManager method getTunnelSSLSocketFactory.

// ZAP: added new ServerSocketFaktory with support of dynamic SSL certificates
public SSLSocketFactory getTunnelSSLSocketFactory(String hostname) {
    //	KeyStore ks;
    try {
        SSLContext ctx = SSLContext.getInstance(SSL);
        // Normally "SunX509", "IbmX509"...
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        SslCertificateService scs = CachedSslCertifificateServiceImpl.getService();
        KeyStore ks = scs.createCertForHost(hostname);
        kmf.init(ks, SslCertificateService.PASSPHRASE);
        java.security.SecureRandom x = new java.security.SecureRandom();
        x.setSeed(System.currentTimeMillis());
        ctx.init(kmf.getKeyManagers(), null, x);
        SSLSocketFactory tunnelSSLFactory = createDecoratedServerSslSocketFactory(ctx.getSocketFactory());
        return tunnelSSLFactory;
    } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException | InvalidKeyException | NoSuchProviderException | SignatureException | IOException e) {
        // friendly way?
        throw new RuntimeException(e);
    }
}
Also used : SslCertificateService(org.parosproxy.paros.security.SslCertificateService) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) NoSuchProviderException(java.security.NoSuchProviderException)

Example 48 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoByIpAddress.

/**
   * Tests opening an SSL/TLS connection to redis using the loopback address of
   * 127.0.0.1. This test should fail because "127.0.0.1" does not match the
   * certificate subject common name and there are no subject alternative names
   * in the certificate.
   * 
   * NOTE: This test relies on a feature that is only available as of Java 7 and later.
   * It is commented out but not removed in case support for Java 6 is dropped or
   * we find a way to have the CI run a specific set of tests on Java 7 and above.
   */
@Test
public void connectWithShardInfoByIpAddress() throws Exception {
    final URI uri = URI.create("rediss://127.0.0.1:6390");
    final SSLSocketFactory sslSocketFactory = createTrustStoreSslSocketFactory();
    // These SSL parameters ensure that we use the same hostname verifier used
    // for HTTPS.
    // Note: this options is only available in Java 7.
    final SSLParameters sslParameters = new SSLParameters();
    sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    try {
        jedis.get("foo");
        Assert.fail("The code did not throw the expected JedisConnectionException.");
    } catch (JedisConnectionException e) {
        Assert.assertEquals("Unexpected first inner exception.", SSLHandshakeException.class, e.getCause().getClass());
        Assert.assertEquals("Unexpected second inner exception.", CertificateException.class, e.getCause().getCause().getClass());
    }
    try {
        jedis.close();
    } catch (Throwable e1) {
    // Expected.
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) CertificateException(java.security.cert.CertificateException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.junit.Test)

Example 49 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfo.

/**
   * Tests opening an SSL/TLS connection to redis.
   * NOTE: This test relies on a feature that is only available as of Java 7 and later.
   * It is commented out but not removed in case support for Java 6 is dropped or
   * we find a way to have the CI run a specific set of tests on Java 7 and above.
   */
@Test
public void connectWithShardInfo() throws Exception {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    // These SSL parameters ensure that we use the same hostname verifier used
    // for HTTPS.
    // Note: this options is only available in Java 7.
    final SSLParameters sslParameters = new SSLParameters();
    sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    jedis.get("foo");
    jedis.disconnect();
    jedis.close();
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) Test(org.junit.Test)

Example 50 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoAndCustomHostnameVerifier.

/**
   * Tests opening an SSL/TLS connection to redis with a custom hostname
   * verifier.
   */
@Test
public void connectWithShardInfoAndCustomHostnameVerifier() {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    final SSLParameters sslParameters = new SSLParameters();
    HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    jedis.get("foo");
    jedis.disconnect();
    jedis.close();
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Aggregations

SSLSocketFactory (javax.net.ssl.SSLSocketFactory)403 SSLContext (javax.net.ssl.SSLContext)150 SSLSocket (javax.net.ssl.SSLSocket)134 IOException (java.io.IOException)106 X509TrustManager (javax.net.ssl.X509TrustManager)69 Socket (java.net.Socket)63 TrustManager (javax.net.ssl.TrustManager)56 HostnameVerifier (javax.net.ssl.HostnameVerifier)49 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)48 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)48 Test (org.junit.Test)46 KeyManagementException (java.security.KeyManagementException)45 URL (java.net.URL)41 CertificateException (java.security.cert.CertificateException)39 OkHttpClient (okhttp3.OkHttpClient)39 OutputStream (java.io.OutputStream)35 InputStream (java.io.InputStream)34 X509Certificate (java.security.cert.X509Certificate)34 SSLSession (javax.net.ssl.SSLSession)30 InetSocketAddress (java.net.InetSocketAddress)29