Skip to content

Commit 8f06d46

Browse files
committed
[fix] finalize buffered TLS EOF handling
Restore buffer modes before validating transport EOF so decrypted data is delivered first and a failed close leaves buffers consistent.
1 parent 1c614d6 commit 8f06d46

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

‎src/main/java/org/jruby/ext/openssl/SSLSocket.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,7 @@ private int read(final ByteBuffer dst, final boolean blocking, final boolean exc
880880
private int readAndUnwrap(final boolean blocking, final boolean exception) throws IOException {
881881
final int bytesRead = socketChannelImpl().read(netReadData);
882882
if ( bytesRead == -1 ) {
883-
if ( netReadData.position() == 0 ||
884-
( status == SSLEngineResult.Status.BUFFER_UNDERFLOW ) ) {
883+
if ( netReadData.position() == 0 ) {
885884
closeInbound();
886885
return -1;
887886
}
@@ -916,16 +915,18 @@ private int readAndUnwrap(final boolean blocking, final boolean exception) throw
916915
status = result.getStatus();
917916
handshakeStatus = result.getHandshakeStatus();
918917

919-
if ( bytesRead == -1 && ! netReadData.hasRemaining() && appReadData.position() == 0 ) {
920-
// Deliver decrypted data first; report an unclean EOF on the next read.
921-
closeInbound();
922-
}
923-
924918
netReadData.compact();
925919
appReadData.flip();
926-
if ( status == SSLEngineResult.Status.CLOSED && ! appReadData.hasRemaining() ) {
920+
if ( status == SSLEngineResult.Status.CLOSED ) {
921+
return appReadData.hasRemaining() ? appReadData.remaining() : -1;
922+
}
923+
if ( bytesRead == -1 && ! appReadData.hasRemaining() &&
924+
( netReadData.position() == 0 || status == SSLEngineResult.Status.BUFFER_UNDERFLOW ) ) {
925+
// close only after buffered plaintext and complete TLS records have been drained
926+
closeInbound();
927927
return -1;
928928
}
929+
929930
if ( ! initialHandshake && (
930931
handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_TASK ||
931932
handshakeStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP ||

0 commit comments

Comments
 (0)