Search in sources :

Example 96 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_getSession.

public void test_SSLSocket_getSession() throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket ssl = (SSLSocket) sf.createSocket();
    SSLSession session = ssl.getSession();
    assertNotNull(session);
    assertFalse(session.isValid());
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSession(javax.net.ssl.SSLSession) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 97 with SSLSocket

use of javax.net.ssl.SSLSocket 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 98 with SSLSocket

use of javax.net.ssl.SSLSocket 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 99 with SSLSocket

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

the class SSLSessionBindingListenerTest method test_valueUnbound.

/**
     * @throws IOException
     * @throws UnknownHostException
     * javax.net.ssl.SSLSessionBindingListener#valueUnbound(SSLSessionBindingEvent event)
     */
public void test_valueUnbound() throws UnknownHostException, IOException {
    SSLSocket sock = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    SSLSession ss = sock.getSession();
    mySSLSessionBindingListener sbl = new mySSLSessionBindingListener();
    ss.putValue("test", sbl);
    ss.removeValue("test");
    assertTrue("valueUnbound was not called.", sbl.unboundDone);
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSession(javax.net.ssl.SSLSession)

Example 100 with SSLSocket

use of javax.net.ssl.SSLSocket in project Conversations by siacs.

the class XmppConnection method switchOverToTls.

private void switchOverToTls(final Tag currentTag) throws XmlPullParserException, IOException {
    tagReader.readTag();
    try {
        final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
        final InetAddress address = socket == null ? null : socket.getInetAddress();
        if (address == null) {
            throw new IOException("could not setup ssl");
        }
        final SSLSocket sslSocket = (SSLSocket) tlsFactoryVerifier.factory.createSocket(socket, address.getHostAddress(), socket.getPort(), true);
        if (sslSocket == null) {
            throw new IOException("could not initialize ssl socket");
        }
        SSLSocketHelper.setSecurity(sslSocket);
        if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), sslSocket.getSession())) {
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
            throw new SecurityException();
        }
        tagReader.setInputStream(sslSocket.getInputStream());
        tagWriter.setOutputStream(sslSocket.getOutputStream());
        sendStartStream();
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS connection established");
        features.encryptionEnabled = true;
        final Tag tag = tagReader.readTag();
        if (tag != null && tag.isStart("stream")) {
            processStream();
        } else {
            throw new IOException("server didn't restart stream after STARTTLS");
        }
        sslSocket.close();
    } catch (final NoSuchAlgorithmException | KeyManagementException e1) {
        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
        throw new SecurityException();
    }
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) Tag(eu.siacs.conversations.xml.Tag) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InetAddress(java.net.InetAddress) KeyManagementException(java.security.KeyManagementException)

Aggregations

SSLSocket (javax.net.ssl.SSLSocket)326 IOException (java.io.IOException)101 Test (org.junit.Test)62 SSLContext (javax.net.ssl.SSLContext)59 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)59 Socket (java.net.Socket)57 OutputStream (java.io.OutputStream)50 InetSocketAddress (java.net.InetSocketAddress)39 CertificateException (java.security.cert.CertificateException)33 SSLException (javax.net.ssl.SSLException)32 SSLSession (javax.net.ssl.SSLSession)31 InputStream (java.io.InputStream)30 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)30 SSLServerSocket (javax.net.ssl.SSLServerSocket)27 SocketTimeoutException (java.net.SocketTimeoutException)24 SocketException (java.net.SocketException)23 ServerSocket (java.net.ServerSocket)22 UnknownHostException (java.net.UnknownHostException)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 InputStreamReader (java.io.InputStreamReader)19