Search in sources :

Example 11 with SSLHandshakeCallbacks

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

the class NativeCryptoTest method test_SSL_get_certificate.

public void test_SSL_get_certificate() throws Exception {
    try {
        NativeCrypto.SSL_get_certificate(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 {
            assertNull(NativeCrypto.SSL_get_certificate(s));
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            assertEqualCertificateChains(getServerCertificates(), NativeCrypto.SSL_get_certificate(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) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 12 with SSLHandshakeCallbacks

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

the class NativeCryptoTest method test_SSL_AlpnNegotiateSuccess.

public void test_SSL_AlpnNegotiateSuccess() throws Exception {
    final byte[] clientAlpnProtocols = new byte[] { 8, 'h', 't', 't', 'p', '/', '1', '.', '1', 3, 'f', 'o', 'o', 6, 's', 'p', 'd', 'y', '/', '2' };
    final byte[] serverAlpnProtocols = new byte[] { 6, 's', 'p', 'd', 'y', '/', '2', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r' };
    Hooks cHooks = new Hooks() {

        @Override
        public long beforeHandshake(long context) throws SSLException {
            NativeCrypto.SSL_CTX_set_alpn_protos(context, clientAlpnProtocols);
            return super.beforeHandshake(context);
        }

        @Override
        public void afterHandshake(long session, long ssl, long context, Socket socket, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[] negotiated = NativeCrypto.SSL_get0_alpn_selected(ssl);
            assertEquals("spdy/2", new String(negotiated));
            /*
                 * There is no callback on the client, so we can't enable
                 * cut-through
                 */
            assertEquals("ALPN should not enable cutthrough on the client", 0, NativeCrypto.SSL_get_mode(ssl) & SSL_MODE_HANDSHAKE_CUTTHROUGH);
            super.afterHandshake(session, ssl, context, socket, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public void afterHandshake(long session, long ssl, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[] negotiated = NativeCrypto.SSL_get0_alpn_selected(ssl);
            assertEquals("spdy/2", new String(negotiated));
            assertEquals("ALPN should not enable cutthrough on the server", 0, NativeCrypto.SSL_get_mode(ssl) & SSL_MODE_HANDSHAKE_CUTTHROUGH);
            super.afterHandshake(session, ssl, c, sock, fd, callback);
        }
    };
    ServerSocket listener = new ServerSocket(0);
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, null, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, null, serverAlpnProtocols);
    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 13 with SSLHandshakeCallbacks

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

the class NativeCryptoTest method test_SSL_renegotiate.

public void test_SSL_renegotiate() throws Exception {
    try {
        NativeCrypto.SSL_renegotiate(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[] buffer = new byte[1];
            NativeCrypto.SSL_read(s, fd, callback, buffer, 0, 1, 0);
            assertEquals(42, buffer[0]);
            super.afterHandshake(session, s, c, sock, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            NativeCrypto.SSL_renegotiate(s);
            NativeCrypto.SSL_write(s, fd, callback, new byte[] { 42 }, 0, 1, 0);
            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) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FileDescriptor(java.io.FileDescriptor)

Example 14 with SSLHandshakeCallbacks

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

the class NativeCryptoTest method test_SSL_read.

public void test_SSL_read() throws Exception {
    // NULL ssl
    try {
        NativeCrypto.SSL_read(NULL, null, null, null, 0, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    }
    // null FileDescriptor
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        try {
            NativeCrypto.SSL_read(s, null, DUMMY_CB, null, 0, 0, 0);
            fail();
        } catch (NullPointerException expected) {
        }
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    // null SSLHandshakeCallbacks
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        try {
            NativeCrypto.SSL_read(s, INVALID_FD, null, null, 0, 0, 0);
            fail();
        } catch (NullPointerException expected) {
        }
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    // null byte array
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        try {
            NativeCrypto.SSL_read(s, INVALID_FD, DUMMY_CB, null, 0, 0, 0);
            fail();
        } catch (NullPointerException expected) {
        }
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    // handshaking not yet performed
    {
        long c = NativeCrypto.SSL_CTX_new();
        long s = NativeCrypto.SSL_new(c);
        try {
            NativeCrypto.SSL_read(s, INVALID_FD, DUMMY_CB, new byte[1], 0, 1, 0);
            fail();
        } catch (SSLException expected) {
        }
        NativeCrypto.SSL_free(s);
        NativeCrypto.SSL_CTX_free(c);
    }
    final ServerSocket listener = new ServerSocket(0);
    // normal case
    {
        Hooks cHooks = new Hooks() {

            @Override
            public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                byte[] in = new byte[256];
                assertEquals(BYTES.length, NativeCrypto.SSL_read(s, fd, callback, in, 0, BYTES.length, 0));
                for (int i = 0; i < BYTES.length; i++) {
                    assertEquals(BYTES[i], in[i]);
                }
                super.afterHandshake(session, s, c, sock, fd, callback);
            }
        };
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

            @Override
            public void afterHandshake(long session, long s, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
                NativeCrypto.SSL_write(s, fd, callback, BYTES, 0, BYTES.length, 0);
                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);
    }
    // timeout case
    try {
        Hooks cHooks = new Hooks() {

            @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, 1);
                fail();
            }
        };
        Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

            @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);
                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);
        fail();
    } catch (ExecutionException expected) {
        assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
    }
}
Also used : SSLHandshakeCallbacks(org.conscrypt.NativeCrypto.SSLHandshakeCallbacks) ServerSocket(java.net.ServerSocket) SSLException(javax.net.ssl.SSLException) FileDescriptor(java.io.FileDescriptor) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 15 with SSLHandshakeCallbacks

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

the class NativeCryptoTest method test_SSL_NpnNegotiateSuccess.

public void test_SSL_NpnNegotiateSuccess() throws Exception {
    final byte[] clientNpnProtocols = new byte[] { 8, 'h', 't', 't', 'p', '/', '1', '.', '1', 3, 'f', 'o', 'o', 6, 's', 'p', 'd', 'y', '/', '2' };
    final byte[] serverNpnProtocols = new byte[] { 6, 's', 'p', 'd', 'y', '/', '2', 3, 'f', 'o', 'o', 3, 'b', 'a', 'r' };
    Hooks cHooks = new Hooks() {

        @Override
        public long beforeHandshake(long context) throws SSLException {
            NativeCrypto.SSL_CTX_enable_npn(context);
            return super.beforeHandshake(context);
        }

        @Override
        public void afterHandshake(long session, long ssl, long context, Socket socket, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[] negotiated = NativeCrypto.SSL_get_npn_negotiated_protocol(ssl);
            assertEquals("spdy/2", new String(negotiated));
            assertTrue("NPN should enable cutthrough on the client", 0 != (NativeCrypto.SSL_get_mode(ssl) & SSL_MODE_HANDSHAKE_CUTTHROUGH));
            super.afterHandshake(session, ssl, context, socket, fd, callback);
        }
    };
    Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates()) {

        @Override
        public long beforeHandshake(long context) throws SSLException {
            NativeCrypto.SSL_CTX_enable_npn(context);
            return super.beforeHandshake(context);
        }

        @Override
        public void afterHandshake(long session, long ssl, long c, Socket sock, FileDescriptor fd, SSLHandshakeCallbacks callback) throws Exception {
            byte[] negotiated = NativeCrypto.SSL_get_npn_negotiated_protocol(ssl);
            assertEquals("spdy/2", new String(negotiated));
            assertEquals("NPN should not enable cutthrough on the server", 0, NativeCrypto.SSL_get_mode(ssl) & SSL_MODE_HANDSHAKE_CUTTHROUGH);
            super.afterHandshake(session, ssl, c, sock, fd, callback);
        }
    };
    ServerSocket listener = new ServerSocket(0);
    Future<TestSSLHandshakeCallbacks> client = handshake(listener, 0, true, cHooks, clientNpnProtocols, null);
    Future<TestSSLHandshakeCallbacks> server = handshake(listener, 0, false, sHooks, serverNpnProtocols, 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