SdkTLSSocketFactory extends SSLConnectionSocketFactory { @Override protected final void prepareSocket(final SSLSocket socket) { String[] supported = socket.getSupportedProtocols(); String[] enabled = socket.getEnabledProtocols(); if (LOG.isDebugEnabled()) { LOG.debug("socket.getSupportedProtocols(): " + Arrays.toString(supported) + ", socket.getEnabledProtocols(): " + Arrays.toString(enabled)); } List<String> target = new ArrayList<String>(); if (supported != null) { TLSProtocol[] values = TLSProtocol.values(); for (int i = 0; i < values.length; i++) { final String pname = values[i].getProtocolName(); if (existsIn(pname, supported)) { target.add(pname); } } } if (enabled != null) { for (String pname : enabled) { if (!target.contains(pname)) { target.add(pname); } } } if (target.size() > 0) { String[] enabling = target.toArray(new String[target.size()]); socket.setEnabledProtocols(enabling); if (LOG.isDebugEnabled()) { LOG.debug("TLS protocol enabled for SSL handshake: " + Arrays.toString(enabling)); } } } SdkTLSSocketFactory(final SSLContext sslContext, final HostnameVerifier hostnameVerifier); @Override Socket createSocket(HttpContext ctx); Socket connectSocket(
            final int connectTimeout,
            final Socket socket,
            final HttpHost host,
            final InetSocketAddress remoteAddress,
            final InetSocketAddress localAddress,
            final HttpContext context);  }
@Test public void preparedSocket_NullProtocols() throws NoSuchAlgorithmException { SdkTLSSocketFactory f = new SdkTLSSocketFactory(SSLContext.getDefault(), null); TestSSLSocket socket = new TestSSLSocket() { @Override public String[] getSupportedProtocols() { return null; } @Override public String[] getEnabledProtocols() { return null; } }; f.prepareSocket(socket); assertFalse(socket.wereProtocolsEnabled()); }
@Test public void typical() throws NoSuchAlgorithmException { SdkTLSSocketFactory f = new SdkTLSSocketFactory(SSLContext.getDefault(), null); TestSSLSocket socket = new TestSSLSocket() { @Override public String[] getSupportedProtocols() { return shuffle(new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}); } @Override public String[] getEnabledProtocols() { return shuffle(new String[]{"SSLv3", "TLSv1"}); } }; f.prepareSocket(socket); assertArrayEquals(new String[] {"TLSv1.2", "TLSv1.1", "TLSv1", "SSLv3" }, socket.getCapturedProtocols()); }
@Test public void noTLS() throws NoSuchAlgorithmException { SdkTLSSocketFactory f = new SdkTLSSocketFactory(SSLContext.getDefault(), null); TestSSLSocket socket = new TestSSLSocket() { @Override public String[] getSupportedProtocols() { return shuffle(new String[]{"SSLv2Hello", "SSLv3" }); } @Override public String[] getEnabledProtocols() { return new String[]{"SSLv3"}; } }; f.prepareSocket(socket); assertArrayEquals(new String[] {"SSLv3"}, socket.getCapturedProtocols()); }
@Test public void notIdeal() throws NoSuchAlgorithmException { SdkTLSSocketFactory f = new SdkTLSSocketFactory(SSLContext.getDefault(), null); TestSSLSocket socket = new TestSSLSocket() { @Override public String[] getSupportedProtocols() { return shuffle(new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1"}); } @Override public String[] getEnabledProtocols() { return shuffle(new String[]{"SSLv3", "TLSv1"}); } }; f.prepareSocket(socket); assertArrayEquals(new String[]{"TLSv1.1", "TLSv1", "SSLv3"}, socket.getCapturedProtocols()); }