Search in sources :

Example 11 with SSLSessionContext

use of javax.net.ssl.SSLSessionContext in project vert.x by eclipse.

the class SSLHelperTest method testOpenSslServerSessionContext.

private void testOpenSslServerSessionContext(boolean testDefault) {
    HttpServerOptions httpServerOptions = new HttpServerOptions().setOpenSslEngineOptions(new OpenSSLEngineOptions());
    if (!testDefault) {
        httpServerOptions.setOpenSslEngineOptions(new OpenSSLEngineOptions().setSessionCacheEnabled(false));
    }
    SSLHelper defaultHelper = new SSLHelper(httpServerOptions, Cert.SERVER_PEM.get(), Trust.SERVER_PEM.get());
    SslContext ctx = defaultHelper.getContext((VertxInternal) vertx);
    assertTrue(ctx instanceof OpenSslServerContext);
    SSLSessionContext sslSessionContext = ctx.sessionContext();
    assertTrue(sslSessionContext instanceof OpenSslServerSessionContext);
    if (sslSessionContext instanceof OpenSslServerSessionContext) {
        assertEquals(testDefault, ((OpenSslServerSessionContext) sslSessionContext).isSessionCacheEnabled());
    }
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) SSLSessionContext(javax.net.ssl.SSLSessionContext) OpenSslServerContext(io.netty.handler.ssl.OpenSslServerContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) OpenSslServerSessionContext(io.netty.handler.ssl.OpenSslServerSessionContext) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) SslContext(io.netty.handler.ssl.SslContext)

Example 12 with SSLSessionContext

use of javax.net.ssl.SSLSessionContext in project netty by netty.

the class JdkSslClientContext method newSSLContext.

private static SSLContext newSSLContext(X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory, long sessionCacheSize, long sessionTimeout) throws SSLException {
    try {
        if (trustCertCollection != null) {
            trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory);
        }
        if (keyCertChain != null) {
            keyManagerFactory = buildKeyManagerFactory(keyCertChain, key, keyPassword, keyManagerFactory);
        }
        SSLContext ctx = SSLContext.getInstance(PROTOCOL);
        ctx.init(keyManagerFactory == null ? null : keyManagerFactory.getKeyManagers(), trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(), null);
        SSLSessionContext sessCtx = ctx.getClientSessionContext();
        if (sessionCacheSize > 0) {
            sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
        }
        if (sessionTimeout > 0) {
            sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
        }
        return ctx;
    } catch (Exception e) {
        if (e instanceof SSLException) {
            throw (SSLException) e;
        }
        throw new SSLException("failed to initialize the client-side SSL context", e);
    }
}
Also used : SSLSessionContext(javax.net.ssl.SSLSessionContext) SSLContext(javax.net.ssl.SSLContext) SSLException(javax.net.ssl.SSLException) SSLException(javax.net.ssl.SSLException)

Example 13 with SSLSessionContext

use of javax.net.ssl.SSLSessionContext in project netty by netty.

the class SocketSslSessionReuseTest method testSslSessionReuse.

public void testSslSessionReuse(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final ReadAndDiscardHandler sh = new ReadAndDiscardHandler(true, true);
    final ReadAndDiscardHandler ch = new ReadAndDiscardHandler(false, true);
    final String[] protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
    sb.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel sch) throws Exception {
            SSLEngine engine = serverCtx.newEngine(sch.alloc());
            engine.setUseClientMode(false);
            engine.setEnabledProtocols(protocols);
            sch.pipeline().addLast(new SslHandler(engine));
            sch.pipeline().addLast(sh);
        }
    });
    final Channel sc = sb.bind().sync().channel();
    cb.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel sch) throws Exception {
            InetSocketAddress serverAddr = (InetSocketAddress) sc.localAddress();
            SSLEngine engine = clientCtx.newEngine(sch.alloc(), serverAddr.getHostString(), serverAddr.getPort());
            engine.setUseClientMode(true);
            engine.setEnabledProtocols(protocols);
            sch.pipeline().addLast(new SslHandler(engine));
            sch.pipeline().addLast(ch);
        }
    });
    try {
        SSLSessionContext clientSessionCtx = ((JdkSslContext) clientCtx).sessionContext();
        ByteBuf msg = Unpooled.wrappedBuffer(new byte[] { 0xa, 0xb, 0xc, 0xd }, 0, 4);
        Channel cc = cb.connect().sync().channel();
        cc.writeAndFlush(msg).sync();
        cc.closeFuture().sync();
        rethrowHandlerExceptions(sh, ch);
        Set<String> sessions = sessionIdSet(clientSessionCtx.getIds());
        msg = Unpooled.wrappedBuffer(new byte[] { 0xa, 0xb, 0xc, 0xd }, 0, 4);
        cc = cb.connect().sync().channel();
        cc.writeAndFlush(msg).sync();
        cc.closeFuture().sync();
        assertEquals("Expected no new sessions", sessions, sessionIdSet(clientSessionCtx.getIds()));
        rethrowHandlerExceptions(sh, ch);
    } finally {
        sc.close().awaitUninterruptibly();
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SSLSessionContext(javax.net.ssl.SSLSessionContext) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SslHandler(io.netty.handler.ssl.SslHandler)

Example 14 with SSLSessionContext

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

the class SSLSessionContextTest method test_SSLSessionContext_getSession.

public void test_SSLSessionContext_getSession() {
    TestSSLContext c = TestSSLContext.create();
    try {
        c.clientContext.getClientSessionContext().getSession(null);
        fail();
    } catch (NullPointerException expected) {
    }
    assertNull(c.clientContext.getClientSessionContext().getSession(new byte[0]));
    assertNull(c.clientContext.getClientSessionContext().getSession(new byte[1]));
    try {
        c.serverContext.getServerSessionContext().getSession(null);
        fail();
    } catch (NullPointerException expected) {
    }
    assertNull(c.serverContext.getServerSessionContext().getSession(new byte[0]));
    assertNull(c.serverContext.getServerSessionContext().getSession(new byte[1]));
    c.close();
    TestSSLSocketPair s = TestSSLSocketPair.create();
    SSLSessionContext client = s.c.clientContext.getClientSessionContext();
    SSLSessionContext server = s.c.serverContext.getServerSessionContext();
    byte[] clientId = (byte[]) client.getIds().nextElement();
    assertNotNull(client.getSession(clientId));
    assertTrue(Arrays.equals(clientId, client.getSession(clientId).getId()));
    if (TestSSLContext.sslServerSocketSupportsSessionTickets()) {
        assertFalse(server.getIds().hasMoreElements());
    } else {
        byte[] serverId = (byte[]) server.getIds().nextElement();
        assertNotNull(server.getSession(serverId));
        assertTrue(Arrays.equals(serverId, server.getSession(serverId).getId()));
    }
    s.close();
}
Also used : SSLSessionContext(javax.net.ssl.SSLSessionContext)

Example 15 with SSLSessionContext

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

the class SSLSessionContextTest method test_sessionTimeout.

/**
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * javax.net.ssl.SSLSessionContex#getSessionTimeout()
     * javax.net.ssl.SSLSessionContex#setSessionTimeout(int seconds)
     */
public final void test_sessionTimeout() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, null, null);
    SSLSessionContext sc = context.getClientSessionContext();
    sc.setSessionTimeout(100);
    assertEquals("100 wasn't returned", 100, sc.getSessionTimeout());
    sc.setSessionTimeout(5000);
    assertEquals("5000 wasn't returned", 5000, sc.getSessionTimeout());
    try {
        sc.setSessionTimeout(-1);
        fail("IllegalArgumentException wasn't thrown");
    } catch (IllegalArgumentException iae) {
    //expected
    }
}
Also used : SSLSessionContext(javax.net.ssl.SSLSessionContext) SSLContext(javax.net.ssl.SSLContext)

Aggregations

SSLSessionContext (javax.net.ssl.SSLSessionContext)18 SSLContext (javax.net.ssl.SSLContext)10 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)3 IOException (java.io.IOException)2 SSLException (javax.net.ssl.SSLException)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 JdkSslContext (io.netty.handler.ssl.JdkSslContext)1 OpenSslServerContext (io.netty.handler.ssl.OpenSslServerContext)1 OpenSslServerSessionContext (io.netty.handler.ssl.OpenSslServerSessionContext)1 SslContext (io.netty.handler.ssl.SslContext)1 SslHandler (io.netty.handler.ssl.SslHandler)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)1 SSLHelper (io.vertx.core.net.impl.SSLHelper)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1