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 — Support DTLS headers when parsing encrypted packet lengths #2 (#13656) · netty/netty@32a025b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • SslHandler.java
      • SslUtils.java
    • test/java/io/netty/handler/ssl
      • SslUtilsTest.java
Expand file treeCollapse file tree
Open diff view settings
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
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import javax.net.ssl.SSLSession;
7474

7575
import static io.netty.buffer.ByteBufUtil.ensureWritableSuccess;
76+
import static io.netty.handler.ssl.SslUtils.NOT_ENOUGH_DATA;
7677
import static io.netty.handler.ssl.SslUtils.getEncryptedPacketLength;
7778
import static io.netty.util.internal.ObjectUtil.checkNotNull;
7879
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
@@ -1319,6 +1320,9 @@ private void decodeJdkCompatible(ChannelHandlerContext ctx, ByteBuf in) throws N
13191320

13201321
throw e;
13211322
}
1323+
if (packetLength == NOT_ENOUGH_DATA) {
1324+
return;
1325+
}
13221326
assert packetLength > 0;
13231327
if (packetLength > readableBytes) {
13241328
// wait until the whole packet can be read
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
+15-7Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ final class SslUtils {
5656
"TLS_AES_128_GCM_SHA256", "TLS_AES_128_CCM_8_SHA256",
5757
"TLS_AES_128_CCM_SHA256")));
5858

59+
static final short DTLS_1_0 = (short) 0xFEFF;
60+
static final short DTLS_1_2 = (short) 0xFEFD;
61+
static final short DTLS_1_3 = (short) 0xFEFC;
62+
static final short DTLS_RECORD_HEADER_LENGTH = 13;
63+
5964
/**
6065
* GMSSL Protocol Version
6166
*/
@@ -260,17 +265,12 @@ static SSLHandshakeException toSSLHandshakeException(Throwable e) {
260265
* the readerIndex of the given {@link ByteBuf}.
261266
*
262267
* @param buffer
263-
* The {@link ByteBuf} to read from. Be aware that it must have at least
264-
* {@link #SSL_RECORD_HEADER_LENGTH} bytes to read,
265-
* otherwise it will throw an {@link IllegalArgumentException}.
268+
* The {@link ByteBuf} to read from.
266269
* @return length
267270
* The length of the encrypted packet that is included in the buffer or
268271
* {@link #SslUtils#NOT_ENOUGH_DATA} if not enough data is present in the
269272
* {@link ByteBuf}. This will return {@link SslUtils#NOT_ENCRYPTED} if
270273
* the given {@link ByteBuf} is not encrypted at all.
271-
* @throws IllegalArgumentException
272-
* Is thrown if the given {@link ByteBuf} has not at least {@link #SSL_RECORD_HEADER_LENGTH}
273-
* bytes to read.
274274
*/
275275
static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
276276
int packetLength = 0;
@@ -293,13 +293,21 @@ static int getEncryptedPacketLength(ByteBuf buffer, int offset) {
293293
if (tls) {
294294
// SSLv3 or TLS or GMSSLv1.0 or GMSSLv1.1 - Check ProtocolVersion
295295
int majorVersion = buffer.getUnsignedByte(offset + 1);
296-
if (majorVersion == 3 || buffer.getShort(offset + 1) == GMSSL_PROTOCOL_VERSION) {
296+
int version = buffer.getShort(offset + 1);
297+
if (majorVersion == 3 || version == GMSSL_PROTOCOL_VERSION) {
297298
// SSLv3 or TLS or GMSSLv1.0 or GMSSLv1.1
298299
packetLength = unsignedShortBE(buffer, offset + 3) + SSL_RECORD_HEADER_LENGTH;
299300
if (packetLength <= SSL_RECORD_HEADER_LENGTH) {
300301
// Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
301302
tls = false;
302303
}
304+
} else if (version == DTLS_1_0 || version == DTLS_1_2 || version == DTLS_1_3) {
305+
if (buffer.readableBytes() < offset + DTLS_RECORD_HEADER_LENGTH) {
306+
return NOT_ENOUGH_DATA;
307+
}
308+
// length is the last 2 bytes in the 13 byte header.
309+
packetLength = unsignedShortBE(buffer, offset + DTLS_RECORD_HEADER_LENGTH - 2) +
310+
DTLS_RECORD_HEADER_LENGTH;
303311
} else {
304312
// Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
305313
tls = false;
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
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.Unpooled;
2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123

2224
import javax.net.ssl.SSLContext;
2325
import javax.net.ssl.SSLEngine;
@@ -26,6 +28,10 @@
2628
import java.nio.ByteOrder;
2729
import java.security.NoSuchAlgorithmException;
2830

31+
import static io.netty.handler.ssl.SslUtils.DTLS_1_0;
32+
import static io.netty.handler.ssl.SslUtils.DTLS_1_2;
33+
import static io.netty.handler.ssl.SslUtils.DTLS_1_3;
34+
import static io.netty.handler.ssl.SslUtils.NOT_ENOUGH_DATA;
2935
import static io.netty.handler.ssl.SslUtils.getEncryptedPacketLength;
3036
import static org.junit.jupiter.api.Assertions.assertEquals;
3137
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -101,6 +107,62 @@ public void shouldGetPacketLengthOfGmsslProtocolFromByteBuffer() {
101107
buf.release();
102108
}
103109

110+
@ParameterizedTest
111+
@ValueSource(ints = {DTLS_1_0, DTLS_1_2, DTLS_1_3}) // six numbers
112+
public void shouldGetPacketLengthOfDtlsRecordFromByteBuf(int dtlsVersion) {
113+
int bodyLength = 65;
114+
ByteBuf buf = Unpooled.buffer()
115+
.writeByte(SslUtils.SSL_CONTENT_TYPE_HANDSHAKE)
116+
.writeShort(dtlsVersion)
117+
.writeShort(0) // epoch
118+
.writeBytes(new byte[6]) // sequence number
119+
.writeShort(bodyLength);
120+
121+
int packetLength = getEncryptedPacketLength(buf, 0);
122+
// bodyLength + DTLS_RECORD_HEADER_LENGTH = 65 + 13 = 78
123+
assertEquals(78, packetLength);
124+
buf.release();
125+
}
126+
127+
@ParameterizedTest
128+
@ValueSource(ints = {DTLS_1_0, DTLS_1_2, DTLS_1_3}) // six numbers
129+
public void shouldGetPacketLengthOfFirstDtlsRecordFromByteBuf(int dtlsVersion) {
130+
int bodyLength = 65;
131+
ByteBuf buf = Unpooled.buffer()
132+
.writeByte(SslUtils.SSL_CONTENT_TYPE_HANDSHAKE)
133+
.writeShort(dtlsVersion)
134+
.writeShort(0) // epoch
135+
.writeBytes(new byte[6]) // sequence number
136+
.writeShort(bodyLength)
137+
.writeBytes(new byte[65])
138+
.writeByte(SslUtils.SSL_CONTENT_TYPE_HANDSHAKE)
139+
.writeShort(dtlsVersion)
140+
.writeShort(0) // epoch
141+
.writeBytes(new byte[6]) // sequence number
142+
.writeShort(bodyLength)
143+
.writeBytes(new byte[65]);
144+
145+
int packetLength = getEncryptedPacketLength(buf, 0);
146+
assertEquals(78, packetLength);
147+
buf.release();
148+
}
149+
150+
@ParameterizedTest
151+
@ValueSource(ints = {DTLS_1_0, DTLS_1_2, DTLS_1_3}) // six numbers
152+
public void shouldSupportIncompletePackets(int dtlsVersion) {
153+
ByteBuf buf = Unpooled.buffer()
154+
.writeByte(SslUtils.SSL_CONTENT_TYPE_HANDSHAKE)
155+
.writeShort(dtlsVersion)
156+
.writeShort(0) // epoch
157+
.writeBytes(new byte[6]) // sequence number
158+
.writeByte(0);
159+
// Left off the last byte of the length on purpose
160+
161+
int packetLength = getEncryptedPacketLength(buf, 0);
162+
assertEquals(NOT_ENOUGH_DATA, packetLength);
163+
buf.release();
164+
}
165+
104166
@Test
105167
public void testValidHostNameForSni() {
106168
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.