Search in sources :

Example 1 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project java-chassis by ServiceComb.

the class SSLManagerTest method testSSLManagerServerAndClient.

@Test
public void testSSLManagerServerAndClient(@Mocked final NetworkInterface nif) throws Exception {
    final InetAddress ia = Inet4Address.getByName("10.57.65.225");
    final Enumeration<NetworkInterface> interfaces = new Enumeration<NetworkInterface>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public NetworkInterface nextElement() {
            return nif;
        }
    };
    final Enumeration<InetAddress> ias = new Enumeration<InetAddress>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public InetAddress nextElement() {
            return ia;
        }
    };
    new Expectations() {

        @Mocked
        NetworkInterface nif;

        {
            NetworkInterface.getNetworkInterfaces();
            result = interfaces;
        }
    };
    new Expectations() {

        {
            nif.getInetAddresses();
            result = ias;
            ia.getHostAddress();
            result = "10.57.65.225";
        }
    };
    SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
    SSLCustom custom = new SSLCustom() {

        @Override
        public String getFullPath(String filename) {
            return DIR + "/ssl/" + filename;
        }

        @Override
        public char[] decode(char[] encrypted) {
            return encrypted;
        }
    };
    final SSLServerSocket serverSocket = SSLManager.createSSLServerSocket(option, custom);
    serverSocket.bind(new InetSocketAddress("127.0.0.1", 8886));
    String[] protos = serverSocket.getEnabledCipherSuites();
    String[] protosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(protos, protosExpected);
    String[] ciphers = serverSocket.getEnabledCipherSuites();
    String[] ciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(ciphers, ciphersExpected);
    Assert.assertEquals(serverSocket.getNeedClientAuth(), true);
    SSLOption clientoption = SSLOption.build(DIR + "/client.ssl.properties");
    SSLSocket clientsocket = SSLManager.createSSLSocket(clientoption, custom);
    String[] clientprotos = clientsocket.getEnabledCipherSuites();
    String[] clientprotosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientprotos, clientprotosExpected);
    String[] clientciphers = clientsocket.getEnabledCipherSuites();
    String[] clientciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientciphers, clientciphersExpected);
    Assert.assertEquals(clientsocket.getNeedClientAuth(), false);
    boolean validAssert = true;
    try {
        clientsocket.connect(new InetSocketAddress("127.0.0.1", 8886));
        new Thread() {

            public void run() {
                try {
                    SSLSocket s = (SSLSocket) serverSocket.accept();
                    s.addHandshakeCompletedListener(new HandshakeCompletedListener() {

                        @Override
                        public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                        }
                    });
                    s.getOutputStream().write(new byte[] { 0, 1 });
                } catch (IOException e) {
                    e.printStackTrace();
                    // this should not happen, do a false assert
                    Assert.assertEquals(false, true);
                }
            }
        }.start();
        clientsocket.startHandshake();
        clientsocket.close();
        serverSocket.close();
    // socked successfully opened and closed
    } catch (Exception e) {
        e.printStackTrace();
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Expectations(mockit.Expectations) Enumeration(java.util.Enumeration) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with HandshakeCompletedListener

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

the class SSLSocketTest method test_SSLSocket_close.

public void test_SSLSocket_close() throws Exception {
    TestSSLSocketPair pair = TestSSLSocketPair.create();
    SSLSocket server = pair.server;
    SSLSocket client = pair.client;
    assertFalse(server.isClosed());
    assertFalse(client.isClosed());
    InputStream input = client.getInputStream();
    OutputStream output = client.getOutputStream();
    server.close();
    client.close();
    assertTrue(server.isClosed());
    assertTrue(client.isClosed());
    // close after close is okay...
    server.close();
    client.close();
    // ...so are a lot of other operations...
    HandshakeCompletedListener l = new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent e) {
        }
    };
    client.addHandshakeCompletedListener(l);
    assertNotNull(client.getEnabledCipherSuites());
    assertNotNull(client.getEnabledProtocols());
    client.getEnableSessionCreation();
    client.getNeedClientAuth();
    assertNotNull(client.getSession());
    assertNotNull(client.getSSLParameters());
    assertNotNull(client.getSupportedProtocols());
    client.getUseClientMode();
    client.getWantClientAuth();
    client.removeHandshakeCompletedListener(l);
    client.setEnabledCipherSuites(new String[0]);
    client.setEnabledProtocols(new String[0]);
    client.setEnableSessionCreation(false);
    client.setNeedClientAuth(false);
    client.setSSLParameters(client.getSSLParameters());
    client.setWantClientAuth(false);
    // ...but some operations are expected to give SocketException...
    try {
        client.startHandshake();
        fail();
    } catch (SocketException expected) {
    }
    try {
        client.getInputStream();
        fail();
    } catch (SocketException expected) {
    }
    try {
        client.getOutputStream();
        fail();
    } catch (SocketException expected) {
    }
    try {
        input.read();
        fail();
    } catch (SocketException expected) {
    }
    try {
        input.read(null, -1, -1);
        fail();
    } catch (NullPointerException expected) {
        assertTrue(StandardNames.IS_RI);
    } catch (SocketException expected) {
        assertFalse(StandardNames.IS_RI);
    }
    try {
        output.write(-1);
        fail();
    } catch (SocketException expected) {
    }
    try {
        output.write(null, -1, -1);
        fail();
    } catch (NullPointerException expected) {
        assertTrue(StandardNames.IS_RI);
    } catch (SocketException expected) {
        assertFalse(StandardNames.IS_RI);
    }
    // ... and one gives IllegalArgumentException
    try {
        client.setUseClientMode(false);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    pair.close();
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) SocketException(java.net.SocketException) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream)

Example 3 with HandshakeCompletedListener

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

the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener.

public void test_SSLSocket_HandshakeCompletedListener() throws Exception {
    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    final boolean[] handshakeCompletedListenerCalled = new boolean[1];
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent event) {
            try {
                SSLSession session = event.getSession();
                String cipherSuite = event.getCipherSuite();
                Certificate[] localCertificates = event.getLocalCertificates();
                Certificate[] peerCertificates = event.getPeerCertificates();
                javax.security.cert.X509Certificate[] peerCertificateChain = event.getPeerCertificateChain();
                Principal peerPrincipal = event.getPeerPrincipal();
                Principal localPrincipal = event.getLocalPrincipal();
                Socket socket = event.getSocket();
                if (false) {
                    System.out.println("Session=" + session);
                    System.out.println("CipherSuite=" + cipherSuite);
                    System.out.println("LocalCertificates=" + Arrays.toString(localCertificates));
                    System.out.println("PeerCertificates=" + Arrays.toString(peerCertificates));
                    System.out.println("PeerCertificateChain=" + Arrays.toString(peerCertificateChain));
                    System.out.println("PeerPrincipal=" + peerPrincipal);
                    System.out.println("LocalPrincipal=" + localPrincipal);
                    System.out.println("Socket=" + socket);
                }
                assertNotNull(session);
                byte[] id = session.getId();
                assertNotNull(id);
                assertEquals(32, id.length);
                assertNotNull(c.clientContext.getClientSessionContext().getSession(id));
                assertNotNull(cipherSuite);
                assertTrue(Arrays.asList(client.getEnabledCipherSuites()).contains(cipherSuite));
                assertTrue(Arrays.asList(c.serverSocket.getEnabledCipherSuites()).contains(cipherSuite));
                assertNull(localCertificates);
                assertNotNull(peerCertificates);
                TestKeyStore.assertChainLength(peerCertificates);
                assertNotNull(peerCertificates[0]);
                TestSSLContext.assertServerCertificateChain(c.clientTrustManager, peerCertificates);
                TestSSLContext.assertCertificateInKeyStore(peerCertificates[0], c.serverKeyStore);
                assertNotNull(peerCertificateChain);
                TestKeyStore.assertChainLength(peerCertificateChain);
                assertNotNull(peerCertificateChain[0]);
                TestSSLContext.assertCertificateInKeyStore(peerCertificateChain[0].getSubjectDN(), c.serverKeyStore);
                assertNotNull(peerPrincipal);
                TestSSLContext.assertCertificateInKeyStore(peerPrincipal, c.serverKeyStore);
                assertNull(localPrincipal);
                assertNotNull(socket);
                assertSame(client, socket);
                synchronized (handshakeCompletedListenerCalled) {
                    handshakeCompletedListenerCalled[0] = true;
                    handshakeCompletedListenerCalled.notify();
                }
                handshakeCompletedListenerCalled[0] = true;
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    client.startHandshake();
    future.get();
    if (!TestSSLContext.sslServerSocketSupportsSessionTickets()) {
        assertNotNull(c.serverContext.getServerSessionContext().getSession(client.getSession().getId()));
    }
    synchronized (handshakeCompletedListenerCalled) {
        while (!handshakeCompletedListenerCalled[0]) {
            handshakeCompletedListenerCalled.wait();
        }
    }
    client.close();
    server.close();
    c.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSession(javax.net.ssl.SSLSession) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) ExecutorService(java.util.concurrent.ExecutorService) Principal(java.security.Principal) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket)

Example 4 with HandshakeCompletedListener

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

the class SSLSocketTest method test_addHandshakeCompletedListener.

/**
     * javax.net.ssl.SSLSocket#addHandshakeCompletedListener(HandshakeCompletedListener listener)
     */
@AndroidOnly("RI doesn't throw the specified IAE")
public void test_addHandshakeCompletedListener() throws IOException {
    SSLSocket ssl = getSSLSocket();
    HandshakeCompletedListener ls = new HandshakeCL();
    try {
        ssl.addHandshakeCompletedListener(null);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    ssl.addHandshakeCompletedListener(ls);
    ssl.close();
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) SSLSocket(javax.net.ssl.SSLSocket) AndroidOnly(dalvik.annotation.AndroidOnly)

Example 5 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project incubator-servicecomb-java-chassis by apache.

the class SSLManagerTest method testSSLManagerServerAndClient.

@Test
public void testSSLManagerServerAndClient(@Mocked final NetworkInterface nif) throws Exception {
    final InetAddress ia = Inet4Address.getByName("10.57.65.225");
    final Enumeration<NetworkInterface> interfaces = new Enumeration<NetworkInterface>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public NetworkInterface nextElement() {
            return nif;
        }
    };
    final Enumeration<InetAddress> ias = new Enumeration<InetAddress>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public InetAddress nextElement() {
            return ia;
        }
    };
    new Expectations() {

        @Mocked
        NetworkInterface nif;

        {
            NetworkInterface.getNetworkInterfaces();
            result = interfaces;
        }
    };
    new Expectations() {

        {
            nif.getInetAddresses();
            result = ias;
            ia.getHostAddress();
            result = "10.57.65.225";
        }
    };
    SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
    SSLCustom custom = new SSLCustom() {

        @Override
        public String getFullPath(String filename) {
            return DIR + "/ssl/" + filename;
        }

        @Override
        public char[] decode(char[] encrypted) {
            return encrypted;
        }
    };
    final SSLServerSocket serverSocket = SSLManager.createSSLServerSocket(option, custom);
    serverSocket.bind(new InetSocketAddress("127.0.0.1", 8886));
    String[] protos = serverSocket.getEnabledCipherSuites();
    String[] protosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(protos, protosExpected);
    String[] ciphers = serverSocket.getEnabledCipherSuites();
    String[] ciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(ciphers, ciphersExpected);
    Assert.assertEquals(serverSocket.getNeedClientAuth(), true);
    SSLOption clientoption = SSLOption.build(DIR + "/client.ssl.properties");
    SSLSocket clientsocket = SSLManager.createSSLSocket(clientoption, custom);
    String[] clientprotos = clientsocket.getEnabledCipherSuites();
    String[] clientprotosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientprotos, clientprotosExpected);
    String[] clientciphers = clientsocket.getEnabledCipherSuites();
    String[] clientciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientciphers, clientciphersExpected);
    Assert.assertEquals(clientsocket.getNeedClientAuth(), false);
    boolean validAssert = true;
    try {
        clientsocket.connect(new InetSocketAddress("127.0.0.1", 8886));
        new Thread() {

            public void run() {
                try {
                    SSLSocket s = (SSLSocket) serverSocket.accept();
                    s.addHandshakeCompletedListener(new HandshakeCompletedListener() {

                        @Override
                        public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                        }
                    });
                    s.getOutputStream().write(new byte[] { 0, 1 });
                } catch (IOException e) {
                    e.printStackTrace();
                    // this should not happen, do a false assert
                    Assert.assertEquals(false, true);
                }
            }
        }.start();
        clientsocket.startHandshake();
        clientsocket.close();
        serverSocket.close();
    // socked successfully opened and closed
    } catch (Exception e) {
        e.printStackTrace();
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Expectations(mockit.Expectations) Enumeration(java.util.Enumeration) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

HandshakeCompletedListener (javax.net.ssl.HandshakeCompletedListener)19 SSLSocket (javax.net.ssl.SSLSocket)18 HandshakeCompletedEvent (javax.net.ssl.HandshakeCompletedEvent)13 IOException (java.io.IOException)10 SocketException (java.net.SocketException)6 InetSocketAddress (java.net.InetSocketAddress)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 SSLServerSocket (javax.net.ssl.SSLServerSocket)5 KeyManagementException (java.security.KeyManagementException)4 ExecutorService (java.util.concurrent.ExecutorService)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 InetAddress (java.net.InetAddress)3 NetworkInterface (java.net.NetworkInterface)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 CertificateException (java.security.cert.CertificateException)3 Enumeration (java.util.Enumeration)3