GeezerVison is showing github.com — Add new SslHandler.isEncrypted(...) variant that will not produce fal… · netty/netty@b3c24e2
File treeExpand file treeCollapse file tree Open diff view settings example/src/main/java/io/netty/example/portunification main/java/io/netty/handler/ssl test/java/io/netty/handler/ssl
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -82,7 +82,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 82 | 82 |
| 83 | 83 | private boolean isSsl(ByteBuf buf) { | 84 | 84 | if (detectSsl) { | 85 | | - return SslHandler.isEncrypted(buf); | | 85 | + return SslHandler.isEncrypted(buf, false); | 86 | 86 | } | 87 | 87 | return false; | 88 | 88 | } | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -44,7 +44,7 @@ protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> ou | 44 | 44 | if (in.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) { | 45 | 45 | return; | 46 | 46 | } | 47 | | - if (SslHandler.isEncrypted(in)) { | | 47 | + if (SslHandler.isEncrypted(in, false)) { | 48 | 48 | handleSsl(context); | 49 | 49 | } else { | 50 | 50 | handleNonSsl(context); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -79,7 +79,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 79 | 79 | case SslUtils.SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC: | 80 | 80 | // fall-through | 81 | 81 | case SslUtils.SSL_CONTENT_TYPE_ALERT: | 82 | | - final int len = SslUtils.getEncryptedPacketLength(in, readerIndex); | | 82 | + final int len = SslUtils.getEncryptedPacketLength(in, readerIndex, true); | 83 | 83 |
| 84 | 84 | // Not an SSL/TLS packet | 85 | 85 | if (len == SslUtils.NOT_ENCRYPTED) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -1281,13 +1281,35 @@ private boolean ignoreException(Throwable t) { | 1281 | 1281 | * {@code true} if the {@link ByteBuf} is encrypted, {@code false} otherwise. | 1282 | 1282 | * @throws IllegalArgumentException | 1283 | 1283 | * Is thrown if the given {@link ByteBuf} has not at least 5 bytes to read. | | 1284 | + * @deprecated use {@link #isEncrypted(ByteBuf, boolean)}. | 1284 | 1285 | */ | | 1286 | + @Deprecated | 1285 | 1287 | 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) { | 1286 | 1308 | if (buffer.readableBytes() < SslUtils.SSL_RECORD_HEADER_LENGTH) { | 1287 | 1309 | throw new IllegalArgumentException( | 1288 | 1310 | "buffer must have at least " + SslUtils.SSL_RECORD_HEADER_LENGTH + " readable bytes"); | 1289 | 1311 | } | 1290 | | - return getEncryptedPacketLength(buffer, buffer.readerIndex()) != SslUtils.NOT_ENCRYPTED; | | 1312 | + return getEncryptedPacketLength(buffer, buffer.readerIndex(), probeSSLv2) != SslUtils.NOT_ENCRYPTED; | 1291 | 1313 | } | 1292 | 1314 |
| 1293 | 1315 | private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws NotSslRecordException { | @@ -1303,7 +1325,7 @@ private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws N | 1303 | 1325 | if (readableBytes < SslUtils.SSL_RECORD_HEADER_LENGTH) { | 1304 | 1326 | return; | 1305 | 1327 | } | 1306 | | - packetLength = getEncryptedPacketLength(in, in.readerIndex()); | | 1328 | + packetLength = getEncryptedPacketLength(in, in.readerIndex(), true); | 1307 | 1329 | if (packetLength == SslUtils.NOT_ENCRYPTED) { | 1308 | 1330 | // Not an SSL/TLS packet | 1309 | 1331 | NotSslRecordException e = new NotSslRecordException( | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -305,15 +305,15 @@ static SSLHandshakeException toSSLHandshakeException(Throwable e) { | 305 | 305 | * Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase | 306 | 306 | * the readerIndex of the given {@link ByteBuf}. | 307 | 307 | * | 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. | 315 | 315 | */ | 316 | | - static int getEncryptedPacketLength(ByteBuf buffer, int offset) { | | 316 | + static int getEncryptedPacketLength(ByteBuf buffer, int offset, boolean probeSSLv2) { | 317 | 317 | int packetLength = 0; | 318 | 318 |
| 319 | 319 | // SSLv3 or TLS - Check ContentType | @@ -328,6 +328,9 @@ static int getEncryptedPacketLength(ByteBuf buffer, int offset) { | 328 | 328 | break; | 329 | 329 | default: | 330 | 330 | // SSLv2 or bad data | | 331 | + if (!probeSSLv2) { | | 332 | + return NOT_ENCRYPTED; | | 333 | + } | 331 | 334 | tls = false; | 332 | 335 | } | 333 | 336 |
| |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 31 | 31 | import static io.netty.handler.ssl.SslUtils.DTLS_1_0; | 32 | 32 | import static io.netty.handler.ssl.SslUtils.DTLS_1_2; | 33 | 33 | import static io.netty.handler.ssl.SslUtils.DTLS_1_3; | | 34 | +import static io.netty.handler.ssl.SslUtils.NOT_ENCRYPTED; | 34 | 35 | import static io.netty.handler.ssl.SslUtils.NOT_ENOUGH_DATA; | 35 | 36 | import static io.netty.handler.ssl.SslUtils.getEncryptedPacketLength; | 36 | 37 | import static org.junit.jupiter.api.Assertions.assertEquals; | 37 | 38 | 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; | 38 | 41 | import static org.junit.jupiter.api.Assertions.assertTrue; | 39 | 42 |
| 40 | 43 | public class SslUtilsTest { | @@ -59,7 +62,8 @@ public void testPacketLength() throws SSLException, NoSuchAlgorithmException { | 59 | 62 | ByteBuf bufferBE = Unpooled.buffer().writeBytes(cTOsBE); | 60 | 63 |
| 61 | 64 | // 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)); | 63 | 67 | assertEquals(getEncryptedPacketLength(new ByteBuffer[] { bufferBE.nioBuffer() }, 0), | 64 | 68 | getEncryptedPacketLength(new ByteBuffer[] { bufferLE.nioBuffer().order(ByteOrder.LITTLE_ENDIAN) }, 0)); | 65 | 69 | } | @@ -89,7 +93,7 @@ public void shouldGetPacketLengthOfGmsslProtocolFromByteBuf() { | 89 | 93 | .writeShort(SslUtils.GMSSL_PROTOCOL_VERSION) | 90 | 94 | .writeShort(bodyLength); | 91 | 95 |
| 92 | | - int packetLength = getEncryptedPacketLength(buf, 0); | | 96 | + int packetLength = getEncryptedPacketLength(buf, 0, true); | 93 | 97 | assertEquals(bodyLength + SslUtils.SSL_RECORD_HEADER_LENGTH, packetLength); | 94 | 98 | buf.release(); | 95 | 99 | } | @@ -118,7 +122,7 @@ public void shouldGetPacketLengthOfDtlsRecordFromByteBuf(int dtlsVersion) { | 118 | 122 | .writeBytes(new byte[6]) // sequence number | 119 | 123 | .writeShort(bodyLength); | 120 | 124 |
| 121 | | - int packetLength = getEncryptedPacketLength(buf, 0); | | 125 | + int packetLength = getEncryptedPacketLength(buf, 0, true); | 122 | 126 | // bodyLength + DTLS_RECORD_HEADER_LENGTH = 65 + 13 = 78 | 123 | 127 | assertEquals(78, packetLength); | 124 | 128 | buf.release(); | @@ -142,7 +146,7 @@ public void shouldGetPacketLengthOfFirstDtlsRecordFromByteBuf(int dtlsVersion) { | 142 | 146 | .writeShort(bodyLength) | 143 | 147 | .writeBytes(new byte[65]); | 144 | 148 |
| 145 | | - int packetLength = getEncryptedPacketLength(buf, 0); | | 149 | + int packetLength = getEncryptedPacketLength(buf, 0, true); | 146 | 150 | assertEquals(78, packetLength); | 147 | 151 | buf.release(); | 148 | 152 | } | @@ -158,11 +162,24 @@ public void shouldSupportIncompletePackets(int dtlsVersion) { | 158 | 162 | .writeByte(0); | 159 | 163 | // Left off the last byte of the length on purpose | 160 | 164 |
| 161 | | - int packetLength = getEncryptedPacketLength(buf, 0); | | 165 | + int packetLength = getEncryptedPacketLength(buf, 0, true); | 162 | 166 | assertEquals(NOT_ENOUGH_DATA, packetLength); | 163 | 167 | buf.release(); | 164 | 168 | } | 165 | 169 |
| | 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 | + | 166 | 183 | @Test | 167 | 184 | public void testValidHostNameForSni() { | 168 | 185 | assertFalse(SslUtils.isValidHostNameForSNI("/test.de"), "SNI domain can't start with /"); | |
|