Skip to content

Commit fe14bad

Browse files
authored
Adjust SSL related tests to be more correct and so pass in the next EA release of java11. (netty#8162)
Motivation: In some of our tests we not correctly init the SSLEngine before trying to perform a handshake which can cause an IllegalStateException. While this not happened in previous java releases it does now on Java11 (which is "ok" as its even mentioned in the api docs). Beside this how we selected the ciphersuite to test renegotation was not 100 % safe. Modifications: - Correctly init SSLEngine before using it - Correctly select ciphersuite before testing for renegotation. Result: More correct tests and also pass on next java11 EA release.
1 parent 630c827 commit fe14bad

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ public void testServerHandshakeTimeout() throws Exception {
133133
testHandshakeTimeout(false);
134134
}
135135

136+
private static SSLEngine newServerModeSSLEngine() throws NoSuchAlgorithmException {
137+
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
138+
// Set the mode before we try to do the handshake as otherwise it may throw an IllegalStateException.
139+
// See:
140+
// - https://docs.oracle.com/javase/10/docs/api/javax/net/ssl/SSLEngine.html#beginHandshake()
141+
// - http://mail.openjdk.java.net/pipermail/security-dev/2018-July/017715.html
142+
engine.setUseClientMode(false);
143+
return engine;
144+
}
145+
136146
private static void testHandshakeTimeout(boolean client) throws Exception {
137147
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
138148
engine.setUseClientMode(client);
@@ -155,9 +165,7 @@ private static void testHandshakeTimeout(boolean client) throws Exception {
155165

156166
@Test
157167
public void testTruncatedPacket() throws Exception {
158-
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
159-
engine.setUseClientMode(false);
160-
168+
SSLEngine engine = newServerModeSSLEngine();
161169
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
162170

163171
// Push the first part of a 5-byte handshake message.
@@ -183,9 +191,7 @@ public void testTruncatedPacket() throws Exception {
183191

184192
@Test
185193
public void testNonByteBufWriteIsReleased() throws Exception {
186-
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
187-
engine.setUseClientMode(false);
188-
194+
SSLEngine engine = newServerModeSSLEngine();
189195
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
190196

191197
AbstractReferenceCounted referenceCounted = new AbstractReferenceCounted() {
@@ -210,9 +216,7 @@ protected void deallocate() {
210216

211217
@Test(expected = UnsupportedMessageTypeException.class)
212218
public void testNonByteBufNotPassThrough() throws Exception {
213-
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
214-
engine.setUseClientMode(false);
215-
219+
SSLEngine engine = newServerModeSSLEngine();
216220
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
217221

218222
try {
@@ -224,9 +228,7 @@ public void testNonByteBufNotPassThrough() throws Exception {
224228

225229
@Test
226230
public void testIncompleteWriteDoesNotCompletePromisePrematurely() throws NoSuchAlgorithmException {
227-
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
228-
engine.setUseClientMode(false);
229-
231+
SSLEngine engine = newServerModeSSLEngine();
230232
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
231233

232234
ChannelPromise promise = ch.newPromise();
@@ -398,7 +400,8 @@ public void channelInactive(ChannelHandlerContext ctx) {
398400

399401
@Test
400402
public void testCloseFutureNotified() throws Exception {
401-
SslHandler handler = new SslHandler(SSLContext.getDefault().createSSLEngine());
403+
SSLEngine engine = newServerModeSSLEngine();
404+
SslHandler handler = new SslHandler(engine);
402405
EmbeddedChannel ch = new EmbeddedChannel(handler);
403406

404407
ch.close();
@@ -416,7 +419,7 @@ public void testCloseFutureNotified() throws Exception {
416419

417420
@Test(timeout = 5000)
418421
public void testEventsFired() throws Exception {
419-
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
422+
SSLEngine engine = newServerModeSSLEngine();
420423
final BlockingQueue<SslCompletionEvent> events = new LinkedBlockingQueue<SslCompletionEvent>();
421424
EmbeddedChannel channel = new EmbeddedChannel(new SslHandler(engine), new ChannelInboundHandlerAdapter() {
422425
@Override

testsuite/src/main/java/io/netty/testsuite/transport/socket/SocketSslClientRenegotiateTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ public void initChannel(Channel sch) throws Exception {
162162
Future<Channel> clientHandshakeFuture = clientSslHandler.handshakeFuture();
163163
clientHandshakeFuture.sync();
164164

165-
String renegotiation = clientSslHandler.engine().getSupportedCipherSuites()[0];
165+
String renegotiation = clientSslHandler.engine().getEnabledCipherSuites()[0];
166+
// Use the first previous enabled ciphersuite and try to renegotiate.
166167
clientSslHandler.engine().setEnabledCipherSuites(new String[] { renegotiation });
167168
clientSslHandler.renegotiate().await();
168169
serverChannel.close().awaitUninterruptibly();

0 commit comments

Comments
 (0)