Search in sources :

Example 81 with SSLSocket

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

the class SSLSocketTest method test_WantClientAuth.

/**
     * javax.net.ssl.SSLSocket#setWantClientAuth(boolean want)
     * javax.net.ssl.SSLSocket#getWantClientAuthCreation()
     */
public void test_WantClientAuth() throws UnknownHostException, IOException {
    SSLSocket ssl = getSSLSocket();
    ssl.setWantClientAuth(true);
    assertTrue(ssl.getWantClientAuth());
    ssl.setWantClientAuth(false);
    assertFalse(ssl.getWantClientAuth());
    ssl.close();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket)

Example 82 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_getEnabledProtocols.

public void test_SSLSocket_getEnabledProtocols() throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket ssl = (SSLSocket) sf.createSocket();
    String[] protocols = ssl.getEnabledProtocols();
    StandardNames.assertValidProtocols(StandardNames.SSL_SOCKET_PROTOCOLS, protocols);
    assertNotSame(protocols, ssl.getEnabledProtocols());
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 83 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_setUseClientMode.

private void test_SSLSocket_setUseClientMode(final boolean clientClientMode, final boolean serverClientMode) throws Exception {
    TestSSLContext c = TestSSLContext.create();
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<IOException> future = executor.submit(new Callable<IOException>() {

        @Override
        public IOException call() throws Exception {
            try {
                if (!serverClientMode) {
                    server.setSoTimeout(1 * 1000);
                }
                server.setUseClientMode(serverClientMode);
                server.startHandshake();
                return null;
            } catch (SSLHandshakeException e) {
                return e;
            } catch (SocketTimeoutException e) {
                return e;
            }
        }
    });
    executor.shutdown();
    if (!clientClientMode) {
        client.setSoTimeout(1 * 1000);
    }
    client.setUseClientMode(clientClientMode);
    client.startHandshake();
    IOException ioe = future.get();
    if (ioe != null) {
        throw ioe;
    }
    client.close();
    server.close();
    c.close();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) 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) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 84 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_setSSLParameters.

public void test_SSLSocket_setSSLParameters() throws Exception {
    SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket ssl = (SSLSocket) sf.createSocket();
    String[] defaultCipherSuites = ssl.getEnabledCipherSuites();
    String[] defaultProtocols = ssl.getEnabledProtocols();
    String[] supportedCipherSuites = ssl.getSupportedCipherSuites();
    String[] supportedProtocols = ssl.getSupportedProtocols();
    {
        SSLParameters p = new SSLParameters();
        ssl.setSSLParameters(p);
        assertEquals(Arrays.asList(defaultCipherSuites), Arrays.asList(ssl.getEnabledCipherSuites()));
        assertEquals(Arrays.asList(defaultProtocols), Arrays.asList(ssl.getEnabledProtocols()));
    }
    {
        SSLParameters p = new SSLParameters(supportedCipherSuites, supportedProtocols);
        ssl.setSSLParameters(p);
        assertEquals(Arrays.asList(supportedCipherSuites), Arrays.asList(ssl.getEnabledCipherSuites()));
        assertEquals(Arrays.asList(supportedProtocols), Arrays.asList(ssl.getEnabledProtocols()));
    }
    {
        SSLParameters p = new SSLParameters();
        p.setNeedClientAuth(true);
        assertFalse(ssl.getNeedClientAuth());
        assertFalse(ssl.getWantClientAuth());
        ssl.setSSLParameters(p);
        assertTrue(ssl.getNeedClientAuth());
        assertFalse(ssl.getWantClientAuth());
        p.setWantClientAuth(true);
        assertTrue(ssl.getNeedClientAuth());
        assertFalse(ssl.getWantClientAuth());
        ssl.setSSLParameters(p);
        assertFalse(ssl.getNeedClientAuth());
        assertTrue(ssl.getWantClientAuth());
        p.setWantClientAuth(false);
        assertFalse(ssl.getNeedClientAuth());
        assertTrue(ssl.getWantClientAuth());
        ssl.setSSLParameters(p);
        assertFalse(ssl.getNeedClientAuth());
        assertFalse(ssl.getWantClientAuth());
    }
}
Also used : SSLParameters(javax.net.ssl.SSLParameters) SSLSocket(javax.net.ssl.SSLSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 85 with SSLSocket

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

the class SSLSocketTest method test_SSLSocket_setSoWriteTimeout.

public void test_SSLSocket_setSoWriteTimeout() throws Exception {
    if (StandardNames.IS_RI) {
        // RI does not support write timeout on sockets
        return;
    }
    final TestSSLContext c = TestSSLContext.create();
    SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket();
    // Try to make the client SO_SNDBUF size as small as possible
    // (it can default to 512k or even megabytes).  Note that
    // socket(7) says that the kernel will double the request to
    // leave room for its own book keeping and that the minimal
    // value will be 2048. Also note that tcp(7) says the value
    // needs to be set before connect(2).
    int sendBufferSize = 1024;
    client.setSendBufferSize(sendBufferSize);
    sendBufferSize = client.getSendBufferSize();
    // In jb-mr2 it was found that we need to also set SO_RCVBUF
    // to a minimal size or the write would not block. While
    // tcp(2) says the value has to be set before listen(2), it
    // seems fine to set it before accept(2).
    final int recvBufferSize = 128;
    c.serverSocket.setReceiveBufferSize(recvBufferSize);
    client.connect(new InetSocketAddress(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();
    client.startHandshake();
    // Reflection is used so this can compile on the RI
    String expectedClassName = "com.android.org.conscrypt.OpenSSLSocketImpl";
    Class actualClass = client.getClass();
    assertEquals(expectedClassName, actualClass.getName());
    Method setSoWriteTimeout = actualClass.getMethod("setSoWriteTimeout", new Class[] { Integer.TYPE });
    setSoWriteTimeout.invoke(client, 1);
    try {
        // Add extra space to the write to exceed the send buffer
        // size and cause the write to block.
        final int extra = 1;
        client.getOutputStream().write(new byte[sendBufferSize + extra]);
        fail();
    } catch (SocketTimeoutException expected) {
    }
    future.get();
    client.close();
    server.close();
    c.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) Method(java.lang.reflect.Method) 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) SocketTimeoutException(java.net.SocketTimeoutException) ExecutorService(java.util.concurrent.ExecutorService)

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