Skip to content

Commit

Permalink
Allow to have the session tickets automatically managed by the native… (
Browse files Browse the repository at this point in the history
netty#10280)


Motivation:

BoringSSL supports to automatically manage the session tickets to be used and so also rotate them etc. This is often prefered by users as it removed some complexity. We should support to make use of this.

Modifications:

- Allow to have setSessionTickets() called without an argument or an empty array
- Add tests

Result:

Easier usage of session tickets
  • Loading branch information
normanmaurer authored May 14, 2020
1 parent 2183b37 commit 91ca3d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ public void setTicketKeys(byte[] keys) {
}

/**
* Sets the SSL session ticket keys of this context.
* Sets the SSL session ticket keys of this context. Depending on the underlying native library you may omit the
* argument or pass an empty array and so let the native library handle the key generation and rotating for you.
* If this is supported by the underlying native library should be checked in this case. For example
* <a href="https://commondatastorage.googleapis.com/chromium-boringssl-docs/ssl.h.html#Session-tickets/">
* BoringSSL</a> is known to support this.
*/
public void setTicketKeys(OpenSslSessionTicketKey... keys) {
ObjectUtil.checkNotNull(keys, "keys");
Expand All @@ -109,7 +113,9 @@ public void setTicketKeys(OpenSslSessionTicketKey... keys) {
writerLock.lock();
try {
SSLContext.clearOptions(context.ctx, SSL.SSL_OP_NO_TICKET);
SSLContext.setSessionTicketKeys(context.ctx, ticketKeys);
if (ticketKeys.length > 0) {
SSLContext.setSessionTicketKeys(context.ctx, ticketKeys);
}
} finally {
writerLock.unlock();
}
Expand Down
31 changes: 24 additions & 7 deletions handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1118,16 +1118,28 @@ protected void initChannel(Channel ch) {

@Test(timeout = 5000L)
public void testSessionTicketsWithTLSv12() throws Throwable {
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_2);
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_2, true);
}

@Test(timeout = 5000L)
public void testSessionTicketsWithTLSv13() throws Throwable {
assumeTrue(OpenSsl.isTlsv13Supported());
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_3);
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_3, true);
}

private static void testSessionTickets(String protocol) throws Throwable {
@Test(timeout = 5000L)
public void testSessionTicketsWithTLSv12AndNoKey() throws Throwable {
assumeTrue(OpenSsl.isBoringSSL());
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_2, false);
}

@Test(timeout = 5000L)
public void testSessionTicketsWithTLSv13AndNoKey() throws Throwable {
assumeTrue(OpenSsl.isTlsv13Supported());
testSessionTickets(SslUtils.PROTOCOL_TLS_V1_3, false);
}

private static void testSessionTickets(String protocol, boolean withKey) throws Throwable {
assumeTrue(OpenSsl.isAvailable());
final SslContext sslClientCtx = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
Expand All @@ -1141,10 +1153,15 @@ private static void testSessionTickets(String protocol) throws Throwable {
.protocols(protocol)
.build();

OpenSslSessionTicketKey key = new OpenSslSessionTicketKey(new byte[OpenSslSessionTicketKey.NAME_SIZE],
new byte[OpenSslSessionTicketKey.HMAC_KEY_SIZE], new byte[OpenSslSessionTicketKey.AES_KEY_SIZE]);
((OpenSslSessionContext) sslClientCtx.sessionContext()).setTicketKeys(key);
((OpenSslSessionContext) sslServerCtx.sessionContext()).setTicketKeys(key);
if (withKey) {
OpenSslSessionTicketKey key = new OpenSslSessionTicketKey(new byte[OpenSslSessionTicketKey.NAME_SIZE],
new byte[OpenSslSessionTicketKey.HMAC_KEY_SIZE], new byte[OpenSslSessionTicketKey.AES_KEY_SIZE]);
((OpenSslSessionContext) sslClientCtx.sessionContext()).setTicketKeys(key);
((OpenSslSessionContext) sslServerCtx.sessionContext()).setTicketKeys(key);
} else {
((OpenSslSessionContext) sslClientCtx.sessionContext()).setTicketKeys();
((OpenSslSessionContext) sslServerCtx.sessionContext()).setTicketKeys();
}

EventLoopGroup group = new NioEventLoopGroup();
Channel sc = null;
Expand Down

0 comments on commit 91ca3d3

Please sign in to comment.