Search in sources :

Example 51 with SSLSocketFactory

use of javax.net.ssl.SSLSocketFactory in project camel by apache.

the class CamelSSLIRCConnection method connect.

@Override
public void connect() throws IOException {
    if (sslContextParameters == null) {
        super.connect();
    } else {
        if (level != 0) {
            throw new SocketException("Socket closed or already open (" + level + ")");
        }
        IOException exception = null;
        final SSLContext sslContext;
        try {
            sslContext = sslContextParameters.createSSLContext(camelContext);
        } catch (GeneralSecurityException e) {
            throw new RuntimeCamelException("Error in SSLContextParameters configuration or instantiation.", e);
        }
        final SSLSocketFactory sf = sslContext.getSocketFactory();
        SSLSocket s = null;
        for (int i = 0; i < ports.length && s == null; i++) {
            try {
                s = (SSLSocket) sf.createSocket(host, ports[i]);
                s.startHandshake();
                exception = null;
            } catch (SSLNotSupportedException exc) {
                if (s != null) {
                    s.close();
                }
                s = null;
                throw exc;
            } catch (IOException exc) {
                if (s != null) {
                    s.close();
                }
                s = null;
                exception = exc;
            }
        }
        if (exception != null) {
            // connection wasn't successful at any port
            throw exception;
        }
        prepare(s);
    }
}
Also used : SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) SSLSocket(javax.net.ssl.SSLSocket) SSLNotSupportedException(org.schwering.irc.lib.ssl.SSLNotSupportedException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 52 with SSLSocketFactory

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

the class URLConnectionTest method testHttpsWithCustomTrustManager.

public void testHttpsWithCustomTrustManager() throws Exception {
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    RecordingTrustManager trustManager = new RecordingTrustManager();
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
    HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    try {
        TestSSLContext testSSLContext = TestSSLContext.create();
        server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
        server.enqueue(new MockResponse().setBody("ABC"));
        server.enqueue(new MockResponse().setBody("DEF"));
        server.enqueue(new MockResponse().setBody("GHI"));
        server.play();
        URL url = server.getUrl("/");
        assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
        assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls);
        assertEquals(Arrays.asList("checkServerTrusted [" + "CN=" + hostName + " 1, " + "CN=Test Intermediate Certificate Authority 1, " + "CN=Test Root Certificate Authority 1" + "] RSA"), trustManager.calls);
    } finally {
        HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
        HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) SSLContext(javax.net.ssl.SSLContext) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 53 with SSLSocketFactory

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

the class WeakSSLConfigTest method shouldIncludeAllSuitesIfTheMagicThreeDoNotExist.

@Test
public void shouldIncludeAllSuitesIfTheMagicThreeDoNotExist() throws Exception {
    SSLSocketFactory socketFactory = mock(SSLSocketFactory.class);
    String[] supportedSuites = { "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256", "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" };
    when(socketFactory.getSupportedCipherSuites()).thenReturn(supportedSuites);
    WeakSSLConfig goSSLConfig = new WeakSSLConfig(socketFactory);
    List<String> includedSuites = Arrays.asList(goSSLConfig.getCipherSuitesToBeIncluded());
    assertThat(includedSuites.size(), is(supportedSuites.length));
    for (String cipherSuite : includedSuites) {
        assertThat(Arrays.asList(supportedSuites).contains(cipherSuite), is(true));
    }
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 54 with SSLSocketFactory

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

the class WeakSSLConfigTest method shouldIncludeTheMagicThreeWhichAreSupportedByOurJetty.

@Test
public void shouldIncludeTheMagicThreeWhichAreSupportedByOurJetty() throws Exception {
    SSLSocketFactory socketFactory = mock(SSLSocketFactory.class);
    when(socketFactory.getSupportedCipherSuites()).thenReturn(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", "SSL_RSA_WITH_RC4_128_SHA", "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256", "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "SSL_RSA_WITH_RC4_128_MD5", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" });
    WeakSSLConfig sslConfig = new WeakSSLConfig(socketFactory);
    assertThat(Arrays.asList(sslConfig.getCipherSuitesToBeIncluded()), is(Arrays.asList("SSL_RSA_WITH_RC4_128_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_WITH_RC4_128_MD5")));
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 55 with SSLSocketFactory

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

the class GoServerTest method shouldLoadAllJarsInTheAddonsDirectoryIntoClassPath.

@Test
public void shouldLoadAllJarsInTheAddonsDirectoryIntoClassPath() throws Exception {
    File addonsDirectory = createInAddonDir("some-addon-dir");
    FileSystemUtils.createFile("addon-1.JAR", addonsDirectory);
    FileSystemUtils.createFile("addon-2.jar", addonsDirectory);
    FileSystemUtils.createFile("addon-3.jAR", addonsDirectory);
    FileSystemUtils.createFile("some-file-which-does-not-end-with-dot-jar.txt", addonsDirectory);
    File oneAddonDirectory = createInAddonDir("one-addon-dir");
    FileSystemUtils.createFile("addon-1.jar", oneAddonDirectory);
    File noAddonDirectory = createInAddonDir("no-addon-dir");
    SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
    when(sslSocketFactory.getSupportedCipherSuites()).thenReturn(new String[0]);
    GoServer goServerWithMultipleAddons = new GoServer(setAddonsPathTo(addonsDirectory), sslSocketFactory);
    goServerWithMultipleAddons.startServer();
    AppServerStub appServer = (AppServerStub) com.thoughtworks.go.util.ReflectionUtil.getField(goServerWithMultipleAddons, "server");
    assertExtraClasspath(appServer, "test-addons/some-addon-dir/addon-1.JAR", "test-addons/some-addon-dir/addon-2.jar", "test-addons/some-addon-dir/addon-3.jAR");
    GoServer goServerWithOneAddon = new GoServer(setAddonsPathTo(oneAddonDirectory), sslSocketFactory);
    goServerWithOneAddon.startServer();
    appServer = (AppServerStub) com.thoughtworks.go.util.ReflectionUtil.getField(goServerWithOneAddon, "server");
    assertExtraClasspath(appServer, "test-addons/one-addon-dir/addon-1.jar");
    GoServer goServerWithNoAddon = new GoServer(setAddonsPathTo(noAddonDirectory), sslSocketFactory);
    goServerWithNoAddon.startServer();
    appServer = (AppServerStub) com.thoughtworks.go.util.ReflectionUtil.getField(goServerWithNoAddon, "server");
    assertExtraClasspath(appServer, "");
    GoServer goServerWithInaccessibleAddonDir = new GoServer(setAddonsPathTo(new File("non-existent-directory")), sslSocketFactory);
    goServerWithInaccessibleAddonDir.startServer();
    appServer = (AppServerStub) com.thoughtworks.go.util.ReflectionUtil.getField(goServerWithNoAddon, "server");
    assertExtraClasspath(appServer, "");
}
Also used : SSLSocketFactory(javax.net.ssl.SSLSocketFactory) File(java.io.File) 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