GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add new SslHandler.isEncrypted(...) variant that will not produce fal… · netty/netty@b3c24e2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • example/src/main/java/io/netty/example/portunification
    • PortUnificationServerHandler.java
  • handler/src
    • main/java/io/netty/handler/ssl
      • OptionalSslHandler.java
      • SslClientHelloHandler.java
      • SslHandler.java
      • SslUtils.java
    • test/java/io/netty/handler/ssl
      • SslUtilsTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java‎

Copy file name to clipboardExpand all lines: example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
8282

8383
private boolean isSsl(ByteBuf buf) {
8484
if (detectSsl) {
85-
return SslHandler.isEncrypted(buf);
85+
return SslHandler.isEncrypted(buf, false);
8686
}
8787
return false;
8888
}
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/OptionalSslHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/OptionalSslHandler.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> ou
4444
if (in.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) {
4545
return;
4646
}
47-
if (SslHandler.isEncrypted(in)) {
47+
if (SslHandler.isEncrypted(in, false)) {
4848
handleSsl(context);
4949
} else {
5050
handleNonSsl(context);
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslClientHelloHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslClientHelloHandler.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
7979
case SslUtils.SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC:
8080
// fall-through
8181
case SslUtils.SSL_CONTENT_TYPE_ALERT:
82-
final int len = SslUtils.getEncryptedPacketLength(in, readerIndex);
82+
final int len = SslUtils.getEncryptedPacketLength(in, readerIndex, true);
8383

8484
// Not an SSL/TLS packet
8585
if (len == SslUtils.NOT_ENCRYPTED) {
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslHandler.java
+24-2Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,13 +1281,35 @@ private boolean ignoreException(Throwable t) {
12811281
* {@code true} if the {@link ByteBuf} is encrypted, {@code false} otherwise.
12821282
* @throws IllegalArgumentException
12831283
* Is thrown if the given {@link ByteBuf} has not at least 5 bytes to read.
1284+
* @deprecated use {@link #isEncrypted(ByteBuf, boolean)}.
12841285
*/
1286+
@Deprecated
12851287
public static boolean isEncrypted(ByteBuf buffer) {
1288+
return isEncrypted(buffer, false);
1289+
}
1290+
1291+
/**
1292+
* Returns {@code true} if the given {@link ByteBuf} is encrypted. Be aware that this method
1293+
* will not increase the readerIndex of the given {@link ByteBuf}.
1294+
*
1295+
* @param buffer
1296+
* The {@link ByteBuf} to read from. Be aware that it must have at least 5 bytes to read,
1297+
* otherwise it will throw an {@link IllegalArgumentException}.
1298+
* @return encrypted
1299+
* {@code true} if the {@link ByteBuf} is encrypted, {@code false} otherwise.
1300+
* @param probeSSLv2
1301+
* {@code true} if the input {@code buffer} might be SSLv2. If {@code true} is used this
1302+
* methods might produce false-positives in some cases so it's strongly suggested to
1303+
* use {@code false}.
1304+
* @throws IllegalArgumentException
1305+
* Is thrown if the given {@link ByteBuf} has not at least 5 bytes to read.
1306+
*/
1307+
public static boolean isEncrypted(ByteBuf buffer, boolean probeSSLv2) {
12861308
if (buffer.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) {
12871309
throw new IllegalArgumentException(
12881310
"buffer must have at least " + SslUtils.SSL_RECORD_HEADER_LENGTH + " readable bytes");
12891311
}
1290-
return getEncryptedPacketLength(buffer, buffer.readerIndex()) != SslUtils.NOT_ENCRYPTED;
1312+
return getEncryptedPacketLength(buffer, buffer.readerIndex(), probeSSLv2) != SslUtils.NOT_ENCRYPTED;
12911313
}
12921314

12931315
private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws NotSslRecordException {
@@ -1303,7 +1325,7 @@ private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws N
13031325
if (readableBytes < SslUtils.SSL_RECORD_HEADER_LENGTH) {
13041326
return;
13051327
}
1306-
packetLength = getEncryptedPacketLength(in, in.readerIndex());
1328+
packetLength = getEncryptedPacketLength(in, in.readerIndex(), true);
13071329
if (packetLength == SslUtils.NOT_ENCRYPTED) {
13081330
// Not an SSL/TLS packet
13091331
NotSslRecordException e = new NotSslRecordException(
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslUtils.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslUtils.java
+11-8Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,15 @@ static SSLHandshakeException toSSLHandshakeException(Throwable e) {
305305
* Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase
306306
* the readerIndex of the given {@link ByteBuf}.
307307
*
308-
* @param buffer
309-
* The {@link ByteBuf} to read from.
310-
* @return length
311-
* The length of the encrypted packet that is included in the buffer or
312-
* {@link #SslUtils#NOT_ENOUGH_DATA} if not enough data is present in the
313-
* {@link ByteBuf}. This will return {@link SslUtils#NOT_ENCRYPTED} if
314-
* the given {@link ByteBuf} is not encrypted at all.
308+
* @param buffer The {@link ByteBuf} to read from.
309+
* @param offset The offset to start from.
310+
* @param probeSSLv2 {@code true} if the input {@code buffer} might be SSLv2.
311+
* @return The length of the encrypted packet that is included in the buffer or
312+
* {@link #SslUtils#NOT_ENOUGH_DATA} if not enough data is present in the
313+
* {@link ByteBuf}. This will return {@link SslUtils#NOT_ENCRYPTED} if
314+
* the given {@link ByteBuf} is not encrypted at all.
315315
*/
316-
static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
316+
static int getEncryptedPacketLength(ByteBuf buffer, int offset, boolean probeSSLv2) {
317317
int packetLength = 0;
318318

319319
// SSLv3 or TLS - Check ContentType
@@ -328,6 +328,9 @@ static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
328328
break;
329329
default:
330330
// SSLv2 or bad data
331+
if (!probeSSLv2) {
332+
return NOT_ENCRYPTED;
333+
}
331334
tls = false;
332335
}
333336

Collapse file

‎handler/src/test/java/io/netty/handler/ssl/SslUtilsTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/SslUtilsTest.java
+22-5Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import static io.netty.handler.ssl.SslUtils.DTLS_1_0;
3232
import static io.netty.handler.ssl.SslUtils.DTLS_1_2;
3333
import static io.netty.handler.ssl.SslUtils.DTLS_1_3;
34+
import static io.netty.handler.ssl.SslUtils.NOT_ENCRYPTED;
3435
import static io.netty.handler.ssl.SslUtils.NOT_ENOUGH_DATA;
3536
import static io.netty.handler.ssl.SslUtils.getEncryptedPacketLength;
3637
import static org.junit.jupiter.api.Assertions.assertEquals;
3738
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
40+
import static org.junit.jupiter.api.Assertions.assertNull;
3841
import static org.junit.jupiter.api.Assertions.assertTrue;
3942

4043
public class SslUtilsTest {
@@ -59,7 +62,8 @@ public void testPacketLength() throws SSLException, NoSuchAlgorithmException {
5962
ByteBuf bufferBE = Unpooled.buffer().writeBytes(cTOsBE);
6063

6164
// Test that the packet-length for BE and LE is the same
62-
assertEquals(getEncryptedPacketLength(bufferBE, 0), getEncryptedPacketLength(bufferLE, 0));
65+
assertEquals(getEncryptedPacketLength(bufferBE, 0, true),
66+
getEncryptedPacketLength(bufferLE, 0, true));
6367
assertEquals(getEncryptedPacketLength(new ByteBuffer[] { bufferBE.nioBuffer() }, 0),
6468
getEncryptedPacketLength(new ByteBuffer[] { bufferLE.nioBuffer().order(ByteOrder.LITTLE_ENDIAN) }, 0));
6569
}
@@ -89,7 +93,7 @@ public void shouldGetPacketLengthOfGmsslProtocolFromByteBuf() {
8993
.writeShort(SslUtils.GMSSL_PROTOCOL_VERSION)
9094
.writeShort(bodyLength);
9195

92-
int packetLength = getEncryptedPacketLength(buf, 0);
96+
int packetLength = getEncryptedPacketLength(buf, 0, true);
9397
assertEquals(bodyLength + SslUtils.SSL_RECORD_HEADER_LENGTH, packetLength);
9498
buf.release();
9599
}
@@ -118,7 +122,7 @@ public void shouldGetPacketLengthOfDtlsRecordFromByteBuf(int dtlsVersion) {
118122
.writeBytes(new byte[6]) // sequence number
119123
.writeShort(bodyLength);
120124

121-
int packetLength = getEncryptedPacketLength(buf, 0);
125+
int packetLength = getEncryptedPacketLength(buf, 0, true);
122126
// bodyLength + DTLS_RECORD_HEADER_LENGTH = 65 + 13 = 78
123127
assertEquals(78, packetLength);
124128
buf.release();
@@ -142,7 +146,7 @@ public void shouldGetPacketLengthOfFirstDtlsRecordFromByteBuf(int dtlsVersion) {
142146
.writeShort(bodyLength)
143147
.writeBytes(new byte[65]);
144148

145-
int packetLength = getEncryptedPacketLength(buf, 0);
149+
int packetLength = getEncryptedPacketLength(buf, 0, true);
146150
assertEquals(78, packetLength);
147151
buf.release();
148152
}
@@ -158,11 +162,24 @@ public void shouldSupportIncompletePackets(int dtlsVersion) {
158162
.writeByte(0);
159163
// Left off the last byte of the length on purpose
160164

161-
int packetLength = getEncryptedPacketLength(buf, 0);
165+
int packetLength = getEncryptedPacketLength(buf, 0, true);
162166
assertEquals(NOT_ENOUGH_DATA, packetLength);
163167
buf.release();
164168
}
165169

170+
@ParameterizedTest
171+
@ValueSource(booleans = { false, true })
172+
public void probeForSSLv2(boolean probeSSLv2) {
173+
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] { 0x30, (byte) 0x82, 0x0c, 0x48, 0x02, 0x01, 0x01, 0x01 });
174+
int packetLength = getEncryptedPacketLength(buf, 0, probeSSLv2);
175+
if (probeSSLv2) {
176+
assertNotEquals(NOT_ENCRYPTED, packetLength);
177+
} else {
178+
assertEquals(NOT_ENCRYPTED, packetLength);
179+
}
180+
buf.release();
181+
}
182+
166183
@Test
167184
public void testValidHostNameForSni() {
168185
assertFalse(SslUtils.isValidHostNameForSNI("/test.de"), "SNI domain can't start with /");

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.