Search in sources :

Example 61 with SSLHandshakeException

use of javax.net.ssl.SSLHandshakeException in project android_frameworks_base by ParanoidAndroid.

the class CertificateChainValidator method closeSocketThrowException.

private void closeSocketThrowException(SSLSocket socket, String errorMessage) throws IOException {
    if (HttpLog.LOGV) {
        HttpLog.v("validation error: " + errorMessage);
    }
    if (socket != null) {
        SSLSession session = socket.getSession();
        if (session != null) {
            session.invalidate();
        }
        socket.close();
    }
    throw new SSLHandshakeException(errorMessage);
}
Also used : SSLSession(javax.net.ssl.SSLSession) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 62 with SSLHandshakeException

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

the class URLConnectionTest method testConnectViaHttpsToUntrustedServer.

/**
     * Verify that we don't retry connections on certificate verification errors.
     *
     * http://code.google.com/p/android/issues/detail?id=13178
     */
public void testConnectViaHttpsToUntrustedServer() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create(TestKeyStore.getClientCA2(), TestKeyStore.getServer());
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    // unused
    server.enqueue(new MockResponse());
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    try {
        connection.getInputStream();
        fail();
    } catch (SSLHandshakeException expected) {
        assertTrue(expected.getCause() instanceof CertificateException);
    }
    assertEquals(0, server.getRequestCount());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) CertificateException(java.security.cert.CertificateException) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 63 with SSLHandshakeException

use of javax.net.ssl.SSLHandshakeException in project netty by netty.

the class SSLEngineTest method testMutualAuthDiffCertsClientFailure.

@Test
public void testMutualAuthDiffCertsClientFailure() throws Exception {
    File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
    File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
    String serverKeyPassword = null;
    File clientKeyFile = new File(getClass().getResource("test2_unencrypted.pem").getFile());
    File clientCrtFile = new File(getClass().getResource("test2.crt").getFile());
    String clientKeyPassword = null;
    // Server trusts client but client only trusts itself
    mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword, clientCrtFile, clientKeyFile, clientCrtFile, clientKeyPassword);
    assertTrue(clientLatch.await(2, TimeUnit.SECONDS));
    assertTrue(clientException instanceof SSLHandshakeException);
}
Also used : File(java.io.File) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.junit.Test)

Example 64 with SSLHandshakeException

use of javax.net.ssl.SSLHandshakeException in project netty by netty.

the class SSLEngineTest method mySetupMutualAuth.

private void mySetupMutualAuth(KeyManagerFactory serverKMF, final File serverTrustManager, KeyManagerFactory clientKMF, File clientTrustManager, ClientAuth clientAuth, final boolean failureExpected, final boolean serverInitEngine) throws SSLException, InterruptedException {
    serverSslCtx = SslContextBuilder.forServer(serverKMF).sslProvider(sslServerProvider()).trustManager(serverTrustManager).clientAuth(clientAuth).ciphers(null, IdentityCipherSuiteFilter.INSTANCE).sessionCacheSize(0).sessionTimeout(0).build();
    clientSslCtx = SslContextBuilder.forClient().sslProvider(sslClientProvider()).trustManager(clientTrustManager).keyManager(clientKMF).ciphers(null, IdentityCipherSuiteFilter.INSTANCE).sessionCacheSize(0).sessionTimeout(0).build();
    serverConnectedChannel = null;
    sb = new ServerBootstrap();
    cb = new Bootstrap();
    sb.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    sb.channel(NioServerSocketChannel.class);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.config().setAllocator(new TestByteBufAllocator(ch.config().getAllocator(), type));
            ChannelPipeline p = ch.pipeline();
            SslHandler handler = serverSslCtx.newHandler(ch.alloc());
            if (serverInitEngine) {
                mySetupMutualAuthServerInitSslHandler(handler);
            }
            p.addLast(handler);
            p.addLast(new MessageDelegatorChannelHandler(serverReceiver, serverLatch));
            p.addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt == SslHandshakeCompletionEvent.SUCCESS) {
                        if (failureExpected) {
                            serverException = new IllegalStateException("handshake complete. expected failure");
                        }
                        serverLatch.countDown();
                    } else if (evt instanceof SslHandshakeCompletionEvent) {
                        serverException = ((SslHandshakeCompletionEvent) evt).cause();
                        serverLatch.countDown();
                    }
                    ctx.fireUserEventTriggered(evt);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    if (cause.getCause() instanceof SSLHandshakeException) {
                        serverException = cause.getCause();
                        serverLatch.countDown();
                    } else {
                        serverException = cause;
                        ctx.fireExceptionCaught(cause);
                    }
                }
            });
            serverConnectedChannel = ch;
        }
    });
    cb.group(new NioEventLoopGroup());
    cb.channel(NioSocketChannel.class);
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.config().setAllocator(new TestByteBufAllocator(ch.config().getAllocator(), type));
            ChannelPipeline p = ch.pipeline();
            p.addLast(clientSslCtx.newHandler(ch.alloc()));
            p.addLast(new MessageDelegatorChannelHandler(clientReceiver, clientLatch));
            p.addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt == SslHandshakeCompletionEvent.SUCCESS) {
                        if (failureExpected) {
                            clientException = new IllegalStateException("handshake complete. expected failure");
                        }
                        clientLatch.countDown();
                    } else if (evt instanceof SslHandshakeCompletionEvent) {
                        clientException = ((SslHandshakeCompletionEvent) evt).cause();
                        clientLatch.countDown();
                    }
                    ctx.fireUserEventTriggered(evt);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    if (cause.getCause() instanceof SSLHandshakeException) {
                        clientException = cause.getCause();
                        clientLatch.countDown();
                    } else {
                        ctx.fireExceptionCaught(cause);
                    }
                }
            });
        }
    });
    serverChannel = sb.bind(new InetSocketAddress(0)).sync().channel();
    int port = ((InetSocketAddress) serverChannel.localAddress()).getPort();
    ChannelFuture ccf = cb.connect(new InetSocketAddress(NetUtil.LOCALHOST, port));
    assertTrue(ccf.awaitUninterruptibly().isSuccess());
    clientChannel = ccf.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) ChannelPipeline(io.netty.channel.ChannelPipeline) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 65 with SSLHandshakeException

use of javax.net.ssl.SSLHandshakeException in project netty by netty.

the class JdkSslEngineTest method testAlpnNoCompatibleProtocolsClientHandshakeFailure.

@Test
public void testAlpnNoCompatibleProtocolsClientHandshakeFailure() throws Exception {
    try {
        // initialization error.
        if (!JdkAlpnSslEngine.isAvailable()) {
            throw tlsExtensionNotFound(Protocol.ALPN);
        }
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        JdkApplicationProtocolNegotiator clientApn = new JdkAlpnApplicationProtocolNegotiator(true, true, PREFERRED_APPLICATION_LEVEL_PROTOCOL);
        JdkApplicationProtocolNegotiator serverApn = new JdkAlpnApplicationProtocolNegotiator(new ProtocolSelectorFactory() {

            @Override
            public ProtocolSelector newSelector(SSLEngine engine, Set<String> supportedProtocols) {
                return new ProtocolSelector() {

                    @Override
                    public void unsupported() {
                    }

                    @Override
                    public String select(List<String> protocols) {
                        return APPLICATION_LEVEL_PROTOCOL_NOT_COMPATIBLE;
                    }
                };
            }
        }, JdkBaseApplicationProtocolNegotiator.FAIL_SELECTION_LISTENER_FACTORY, APPLICATION_LEVEL_PROTOCOL_NOT_COMPATIBLE);
        SslContext serverSslCtx = new JdkSslServerContext(ssc.certificate(), ssc.privateKey(), null, null, IdentityCipherSuiteFilter.INSTANCE, serverApn, 0, 0);
        SslContext clientSslCtx = new JdkSslClientContext(null, InsecureTrustManagerFactory.INSTANCE, null, IdentityCipherSuiteFilter.INSTANCE, clientApn, 0, 0);
        setupHandlers(serverSslCtx, clientSslCtx);
        assertTrue(clientLatch.await(2, TimeUnit.SECONDS));
        assertTrue(clientException instanceof SSLHandshakeException);
    } catch (SkipTestException e) {
        // ALPN availability is dependent on the java version. If ALPN is not available because of
        // java version incompatibility don't fail the test, but instead just skip the test
        assumeNoException(e);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SSLEngine(javax.net.ssl.SSLEngine) ProtocolSelectorFactory(io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectorFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProtocolSelector(io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector) Test(org.junit.Test)

Aggregations

SSLHandshakeException (javax.net.ssl.SSLHandshakeException)90 IOException (java.io.IOException)29 Test (org.junit.Test)22 CertificateException (java.security.cert.CertificateException)18 URL (java.net.URL)15 SSLException (javax.net.ssl.SSLException)15 SocketException (java.net.SocketException)13 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)12 SSLProtocolException (javax.net.ssl.SSLProtocolException)10 Socket (java.net.Socket)9 SSLSocket (javax.net.ssl.SSLSocket)9 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)8 SocketTimeoutException (java.net.SocketTimeoutException)7 SSLSession (javax.net.ssl.SSLSession)7 InputStream (java.io.InputStream)6 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)6 Channel (io.netty.channel.Channel)5 InetSocketAddress (java.net.InetSocketAddress)5 MalformedURLException (java.net.MalformedURLException)5 ClosedChannelException (java.nio.channels.ClosedChannelException)5