Skip to content

Commit 2fa9400

Browse files
committed
Fix a bug where SslHandler does not handle SSLv2Hello correctly
Motivation: When a SSLv2Hello message is received, SSLEngine expects the application buffer size to be more than 30KB which is larger than what SslBufferPool can provide. SSLEngine will always return with BUFFER_OVERFLOW status, blocking the SSL session from continuing the handshake. Modifications: When SSLEngine.getSession().getApplicationBufferSize() returns a value larger than what SslBufferPool provides, allocate a temporary heap buffer. Result: SSLv2Hello is handled correctly.
1 parent 129c17a commit 2fa9400

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/main/java/org/jboss/netty/handler/ssl/SslHandler.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,18 @@ private ChannelBuffer unwrap(
12681268
// always contain at least one record in decode(). Therefore, if SSLEngine.unwrap() returns
12691269
// BUFFER_OVERFLOW, it is always resolved by retrying after emptying the application buffer.
12701270
for (;;) {
1271+
final int outAppBufSize = engine.getSession().getApplicationBufferSize();
1272+
final ByteBuffer outAppBuf;
1273+
if (nioOutAppBuf.capacity() < outAppBufSize) {
1274+
// SSLEngine wants a buffer larger than what the pool can provide.
1275+
// Allocate a temporary heap buffer.
1276+
outAppBuf = ByteBuffer.allocate(outAppBufSize);
1277+
} else {
1278+
outAppBuf = nioOutAppBuf;
1279+
}
1280+
12711281
try {
1272-
result = engine.unwrap(nioInNetBuf, nioOutAppBuf);
1282+
result = engine.unwrap(nioInNetBuf, outAppBuf);
12731283
switch (result.getStatus()) {
12741284
case CLOSED:
12751285
// notify about the CLOSED state of the SSLEngine. See #137
@@ -1283,21 +1293,21 @@ private ChannelBuffer unwrap(
12831293

12841294
break;
12851295
} finally {
1286-
nioOutAppBuf.flip();
1296+
outAppBuf.flip();
12871297

12881298
// Sync the offset of the inbound buffer.
12891299
nettyInNetBuf.readerIndex(
12901300
nettyInNetBufStartOffset + nioInNetBuf.position() - nioInNetBufStartOffset);
12911301

12921302
// Copy the unwrapped data into a smaller buffer.
1293-
if (nioOutAppBuf.hasRemaining()) {
1303+
if (outAppBuf.hasRemaining()) {
12941304
if (nettyOutAppBuf == null) {
12951305
ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory();
12961306
nettyOutAppBuf = factory.getBuffer(initialNettyOutAppBufCapacity);
12971307
}
1298-
nettyOutAppBuf.writeBytes(nioOutAppBuf);
1308+
nettyOutAppBuf.writeBytes(outAppBuf);
12991309
}
1300-
nioOutAppBuf.clear();
1310+
outAppBuf.clear();
13011311
}
13021312
}
13031313

0 commit comments

Comments
 (0)