@Test public void testBasicSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test public void testBasicDefaultHostnameVerifier() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier()); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); } finally { sslSocket.close(); } }
@Test public void testClientAuthSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test(expected=SSLException.class) public void testSSLTrustVerification() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); // Use default SSL context final SSLContext defaultsslcontext = SSLContexts.createDefault(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext, NoopHostnameVerifier.INSTANCE); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); sslSocket.close(); }
@Override protected void before() throws Throwable { final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build(); serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN); if(ProtocolScheme.https.equals(protocolScheme)) { serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); } registerHandlers(); server = serverBootstrap.create(); server.start(); host = new HttpHost("127.0.0.1", server.getLocalPort(), protocolScheme.name()); uri = URIUtils.rewriteURI(new URI("/"), host); }
@Test public void testSSLTrustVerificationOverride() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TrustStrategy trustStrategy = new TrustStrategy() { @Override public boolean isTrusted( final X509Certificate[] chain, final String authType) throws CertificateException { return chain.length == 1; } }; final SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(null, trustStrategy) .build(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( sslcontext, NoopHostnameVerifier.INSTANCE); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); sslSocket.close(); }