Skip to content

Commit 53736a6

Browse files
bmleiteacogoluegnes
authored andcommitted
Fix unwrapping loop in case bytebuffer has exactly 1 handshake message
In the scenario where the reading ByteBuffer only has enough bytes to unwrap one handshake message, the flow may enter a loop due to the call to ByteBuffer.clear(). That method does not actually erase any data, instead it sets the position back to 0, the limit to the capacity, and the mark is discarded. Since we are doing another unwrap attempt using the same reading ByteBuffer, the same handshake message will be read. Updating the reading ByteBuffer position instead of trying to clear it will result in a BUFFER_UNDERFLOW, which will then trigger another read from the channel (as expected). (cherry picked from commit 7382a33)
1 parent febcccd commit 53736a6

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

Diff for: src/main/java/com/rabbitmq/client/impl/nio/SslEngineHelper.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ private static SSLEngineResult.HandshakeStatus unwrap(ByteBuffer cipherIn, ByteB
110110
SSLEngineResult unwrapResult;
111111
do {
112112
int positionBeforeUnwrapping = cipherIn.position();
113+
LOGGER.debug("Before unwrapping cipherIn is {}, with {} remaining byte(s)", cipherIn, cipherIn.remaining());
113114
unwrapResult = sslEngine.unwrap(cipherIn, plainIn);
114115
LOGGER.debug("SSL engine result is {} after unwrapping", unwrapResult);
115116
status = unwrapResult.getStatus();
@@ -118,14 +119,7 @@ private static SSLEngineResult.HandshakeStatus unwrap(ByteBuffer cipherIn, ByteB
118119
plainIn.clear();
119120
if (unwrapResult.getHandshakeStatus() == NEED_TASK) {
120121
handshakeStatus = runDelegatedTasks(sslEngine);
121-
int newPosition = positionBeforeUnwrapping + unwrapResult.bytesConsumed();
122-
if (newPosition == cipherIn.limit()) {
123-
LOGGER.debug("Clearing cipherIn because all bytes have been read and unwrapped");
124-
cipherIn.clear();
125-
} else {
126-
LOGGER.debug("Setting cipherIn position to {} (limit is {})", newPosition, cipherIn.limit());
127-
cipherIn.position(positionBeforeUnwrapping + unwrapResult.bytesConsumed());
128-
}
122+
cipherIn.position(positionBeforeUnwrapping + unwrapResult.bytesConsumed());
129123
} else {
130124
handshakeStatus = unwrapResult.getHandshakeStatus();
131125
}

0 commit comments

Comments
 (0)