Search in sources :

Example 6 with SSLHandshakeCallbacks

use of org.conscrypt.NativeCrypto.SSLHandshakeCallbacks in project robovm by robovm.

the class NativeCryptoTest method test_SSL_set_session.

public void test_SSL_set_session() throws Exception {
    try {
        NativeCrypto.SSL_set_session(NULL, NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        NativeCrypto.SSL_set_session(s, NULL);
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    {
        final long clientContext = NativeCrypto.SSL_CTX_new();
        final long serverContext = NativeCrypto.SSL_CTX_new();
        final ServerSocket listener = new ServerSocket(0);
        final long[] clientSession = new long[] { NULL };
        final long[] serverSession = new long[] { NULL };
        {
            Hooks cHooks = new Hooks() {

                @Override
                public long getContext() throws SSLException {
                    return clientContext;
                }

                @Override
                public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                    super.afterHandshake(NULL, s, NULL, sock, fd, callback);
                    clientSession[0] = session;
                }
            };
            Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

                @Override
                public long getContext() throws SSLException {
                    return serverContext;
                }

                @Override
                public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                    super.afterHandshake(NULL, s, NULL, sock, fd, callback);
                    serverSession[0] = session;
                }
            };
            Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
            Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
            client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
            server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        }
        assertEqualSessions(clientSession[0], serverSession[0]);
        {
            Hooks cHooks = new Hooks() {

                @Override
                public long getContext() throws SSLException {
                    return clientContext;
                }

                @Override
                public long beforeHandshake(long c) throws SSLException {
                    long s = NativeCrypto.SSL_new(clientContext);
                    NativeCrypto.SSL_set_session(s, clientSession[0]);
                    return s;
                }

                @Override
                public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                    assertEqualSessions(clientSession[0], session);
                    super.afterHandshake(NULL, s, NULL, sock, fd, callback);
                }
            };
            Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

                @Override
                public long getContext() throws SSLException {
                    return serverContext;
                }

                @Override
                public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                    assertEqualSessions(serverSession[0], session);
                    super.afterHandshake(NULL, s, NULL, sock, fd, callback);
                }
            };
            Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
            Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
            client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
            server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
        }
        NativeCrypto.SSL_SESSION_free(clientSession[0]);
        NativeCrypto.SSL_SESSION_free(serverSession[0]);
        NativeCrypto.SSL_CTX_free(serverContext);
        NativeCrypto.SSL_CTX_free(clientContext);
    }
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 7 with SSLHandshakeCallbacks

use of org.conscrypt.NativeCrypto.SSLHandshakeCallbacks in project robovm by robovm.

the class NativeCryptoTest method test_SSL_SESSION_get_version.

public void test_SSL_SESSION_get_version() throws Exception {
    try {
        NativeCrypto.SSL_SESSION_get_version(NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    final ServerSocket listener = new ServerSocket(0);
    Hooks cHooks = new Hooks() {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            String v = NativeCrypto.SSL_SESSION_get_version(session);
            assertTrue(StandardNames.SSL_SOCKET_PROTOCOLS.contains(v));
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 8 with SSLHandshakeCallbacks

use of org.conscrypt.NativeCrypto.SSLHandshakeCallbacks in project robovm by robovm.

the class NativeCryptoTest method test_SSL_set_tlsext_host_name.

public void test_SSL_set_tlsext_host_name() throws Exception {
    // NULL SSL
    try {
        NativeCrypto.SSL_set_tlsext_host_name(NULL, null);
        fail();
    } catch (NullPointerException expected) {
    }
    final String hostname = "www.android.com";
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        // null hostname
        try {
            NativeCrypto.SSL_set_tlsext_host_name(s, null);
            fail();
        } catch (NullPointerException expected) {
        }
        // too long hostname
        try {
            char[] longHostname = new char[256];
            Arrays.fill(longHostname, 'w');
            NativeCrypto.SSL_set_tlsext_host_name(s, new String(longHostname));
            fail();
        } catch (SSLException expected) {
        }
        assertNull(NativeCrypto.SSL_get_servername(s));
        NativeCrypto.SSL_set_tlsext_host_name(s, new String(hostname));
        assertEquals(hostname, NativeCrypto.SSL_get_servername(s));
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    final ServerSocket listener = new ServerSocket(0);
    // normal
    Hooks cHooks = new Hooks() {

        @Override
        public long beforeHandshake(long c) throws SSLException {
            long s = super.beforeHandshake(c);
            NativeCrypto.SSL_set_tlsext_host_name(s, hostname);
            return s;
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            assertEquals(hostname, NativeCrypto.SSL_get_servername(s));
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) SSLException(javax.net.ssl.SSLException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 9 with SSLHandshakeCallbacks

use of org.conscrypt.NativeCrypto.SSLHandshakeCallbacks in project robovm by robovm.

the class NativeCryptoTest method test_SSL_do_handshake_clientCertificateRequested_throws_after_renegotiate.

/**
     * Usually if a RuntimeException is thrown by the
     * clientCertificateRequestedCalled callback, the caller sees it
     * during the call to NativeCrypto_SSL_do_handshake.  However, IIS
     * does not request client certs until after the initial
     * handshake. It does an SSL renegotiation, which means we need to
     * be able to deliver the callback's exception in cases like
     * SSL_read, SSL_write, and SSL_shutdown.
     */
public void test_SSL_do_handshake_clientCertificateRequested_throws_after_renegotiate() throws Exception {
    final ServerSocket listener = new ServerSocket(0);
    Hooks cHooks = new Hooks() {

        @Override
        public long beforeHandshake(long context) throws SSLException {
            long s = super.beforeHandshake(context);
            NativeCrypto.SSL_clear_mode(s, SSL_MODE_HANDSHAKE_CUTTHROUGH);
            return s;
        }

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            NativeCrypto.SSL_read(s, fd, callback, new byte[1], 0, 1, 0);
            fail();
            super.afterHandshake(session, s, c, sock, fd, callback);
        }

        @Override
        public void clientCertificateRequested(long s) {
            super.clientCertificateRequested(s);
            throw new RuntimeException("expected");
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            try {
                NativeCrypto.SSL_set_verify(s, NativeCrypto.SSL_VERIFY_PEER);
                NativeCrypto.SSL_set_options(s, NativeCrypto.SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
                NativeCrypto.SSL_renegotiate(s);
                NativeCrypto.SSL_write(s, fd, callback, new byte[] { 42 }, 0, 1, (int) ((TIMEOUT_SECONDS * 1000) / 2));
            } catch (IOException expected) {
            } finally {
                super.afterHandshake(session, s, c, sock, fd, callback);
            }
        }
    };
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    try {
        client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
        if (!"expected".equals(e.getCause().getMessage())) {
            throw e;
        }
    }
    server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 10 with SSLHandshakeCallbacks

use of org.conscrypt.NativeCrypto.SSLHandshakeCallbacks in project robovm by robovm.

the class NativeCryptoTest method test_SSL_get_peer_cert_chain.

public void test_SSL_get_peer_cert_chain() throws Exception {
    try {
        NativeCrypto.SSL_get_peer_cert_chain(NULL);
        fail();
    } catch (NullPointerException expected) {
    }
    final ServerSocket listener = new ServerSocket(0);
    Hooks cHooks = new Hooks() {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[][] cc = NativeCrypto.SSL_get_peer_cert_chain(s);
            assertEqualCertificateChains(getServerCertificates(), cc);
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, null);
    client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Aggregations

FileDescriptor (java.io.FileDescriptor)15 ServerSocket (java.net.ServerSocket)15 Socket (java.net.Socket)15 SSLHandshakeCallbacks (org.conscrypt.NativeCrypto.SSLHandshakeCallbacks)15 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 SSLException (javax.net.ssl.SSLException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 CertificateException (java.security.cert.CertificateException)3 SSLProtocolException (javax.net.ssl.SSLProtocolException)3 Future (java.util.concurrent.Future)2