Skip to content

Commit 42b8e64

Browse files
committed
TLS Extensions: add record_size_limit (RFC 8449)
Add the record_size_limit extension for TLS 1.3 and TLS 1.2, enabled with --enable-recordsizelimit (CMake WOLFSSL_RECORDSIZELIMIT, macro HAVE_RECORD_SIZE_LIMIT) and by --enable-all and --enable-tlsx. - wolfSSL_UseRecordSizeLimit() / wolfSSL_CTX_UseRecordSizeLimit() set the largest record payload this end accepts (64 to 16384, 0 to not advertise). A limit of 16384 is advertised by default. - The client offers it in the ClientHello; the server answers in EncryptedExtensions under TLS 1.3 and in the ServerHello under TLS 1.2. The TLS 1.3 wire value includes the content type byte. - Records sent are capped to the peer's limit once encryption is on; received records over our advertised limit are refused with record_overflow, checked on the ciphertext length and again on the decrypted plaintext. Early data a server receives is exempt. - RFC 8449 Sect. 5: a server ignores max_fragment_length when both are in a ClientHello, and a client aborts if a server sends both. The built-in default stands aside when the application asked for max_fragment_length. - EncryptedExtensions, CertificateRequest and NewSessionTicket are fragmented across records when the peer's limit is smaller than the message. SendTls13Certificate() refuses a limit too small for the message's fixed header rather than writing past the fragment. - Record compression and a record size limit refuse each other on one connection, and compression is refused once the default limit has been advertised or received. - The limit is copied to a write-dup object and peer state is reset by wolfSSL_clear(). - An application custom extension cannot be registered for type 28 when the extension is built in. Crypto-only and no-TLS builds turn the option off, in configure and CMake alike; configure overrides an explicit --enable-recordsizelimit there with a warning. test_dtls_frag_ch's MTU is raised as the ClientHello grows by 6 bytes.
1 parent 6f12e1d commit 42b8e64

20 files changed

Lines changed: 2657 additions & 19 deletions

File tree

‎.github/configs/os-check-linux.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
{"name": "all-static", "minutes": 8,
33
"comment": "Static-only build, so configure defines WOLFSSL_TEST_STATIC_BUILD and the tests that call WOLFSSL_LOCAL functions directly are compiled and run.",
44
"configure": ["--enable-all", "--enable-static", "--disable-shared"]},
5+
{"name": "record-size-limit",
6+
"comment": "RFC 8449 on its own. --enable-all covers it alongside everything else; this is the minimal build, which is where the extension's own guards get exercised.",
7+
"configure": ["--enable-recordsizelimit"]},
8+
{"name": "record-size-limit-tls12",
9+
"comment": "RFC 8449 defines the extension for TLS 1.2 too, where the server answers in the ServerHello. Guards the parse dispatch staying outside the WOLFSSL_TLS13 block.",
10+
"configure": ["--enable-recordsizelimit", "--disable-tls13"]},
11+
{"name": "record-size-limit-libz",
12+
"comment": "RFC 8449 alongside TLS record layer compression. Both are built, and each connection uses at most one: wolfSSL_set_compression() and wolfSSL_UseRecordSizeLimit() refuse each other, so this is where that pairing gets exercised.",
13+
"configure": ["--enable-recordsizelimit", "--with-libz"]},
514
{"name": "user-settings-all-compat", "minutes": 9.5,
615
"comment": "user_settings_all.h with the compatibility layer enabled by flipping its \"#if 0\" block, as a build-dir copy.",
716
"user_settings": "examples/configs/user_settings_all.h",

‎CMakeLists.txt‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ if(WOLFSSL_ALL)
374374
WOLFSSL_OCSPSTAPLING WOLFSSL_OCSPSTAPLING_V2 WOLFSSL_OPENSSLALL
375375
WOLFSSL_OPENSSLEXTRA WOLFSSL_POSTAUTH WOLFSSL_QUIC WOLFSSL_SAVECERT
376376
WOLFSSL_SAVESESSION WOLFSSL_SCEP WOLFSSL_SESSION_TICKET WOLFSSL_SNI
377-
WOLFSSL_SRTP WOLFSSL_TLS13)
377+
WOLFSSL_SRTP WOLFSSL_TLS13 WOLFSSL_RECORDSIZELIMIT)
378378
default_option(${_o} "yes")
379379
endforeach()
380380

@@ -857,7 +857,7 @@ if(WOLFSSL_CRYPT_ONLY)
857857
WOLFSSL_PKCALLBACKS WOLFSSL_QUIC
858858
WOLFSSL_RENEGOTIATION_INDICATION WOLFSSL_SECURE_RENEGOTIATION
859859
WOLFSSL_SECURE_RENEGOTIATION_INFO
860-
WOLFSSL_SNI WOLFSSL_SRTP WOLFSSL_TLSX)
860+
WOLFSSL_SNI WOLFSSL_SRTP WOLFSSL_TLSX WOLFSSL_RECORDSIZELIMIT)
861861
force_option(${_o} "no")
862862
endforeach()
863863
message(STATUS "WOLFSSL_CRYPT_ONLY: TLS layer off, including its options")
@@ -3066,6 +3066,8 @@ endif()
30663066

30673067
if(WOLFSSL_TLSX)
30683068
override_cache(WOLFSSL_ENC_THEN_MAC "yes")
3069+
# A pure extension, which is what --enable-tlsx means in configure.ac.
3070+
default_option(WOLFSSL_RECORDSIZELIMIT "yes")
30693071
endif()
30703072

30713073
if(WOLFSSL_SNIFFER)
@@ -4750,6 +4752,17 @@ if(WOLFSSL_LIBZ)
47504752
list(APPEND WOLFSSL_INCLUDE_DIRS ${ZLIB_INCLUDE_DIRS})
47514753
endif()
47524754

4755+
# Record size limit (RFC 8449). Applies to TLS 1.2 as well as TLS 1.3, where
4756+
# the server answers in the ServerHello rather than EncryptedExtensions, so
4757+
# it imposes no TLS 1.3 requirement.
4758+
add_option("WOLFSSL_RECORDSIZELIMIT"
4759+
"Enable RFC 8449 record_size_limit, for TLS 1.2 and TLS 1.3 (default: disabled)"
4760+
"no" "yes;no")
4761+
if(WOLFSSL_RECORDSIZELIMIT)
4762+
list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_TLS_EXTENSIONS")
4763+
list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_RECORD_SIZE_LIMIT")
4764+
endif()
4765+
47534766

47544767
####################################################
47554768
# Maximum key size options (parity with configure.ac)

‎cmake/options.h.in‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ extern "C" {
159159
#cmakedefine HAVE_PTHREAD 1
160160
#undef HAVE_REPRODUCIBLE_BUILD
161161
#cmakedefine HAVE_REPRODUCIBLE_BUILD
162+
#undef HAVE_RECORD_SIZE_LIMIT
163+
#cmakedefine HAVE_RECORD_SIZE_LIMIT
162164
#undef HAVE_SESSION_TICKET
163165
#cmakedefine HAVE_SESSION_TICKET
164166
#undef HAVE_SNI

‎configure.ac‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,7 @@ then
14061406
test "$enable_srtp" = "" && enable_srtp=no
14071407
test "$enable_ocsp" = "" && enable_ocsp=no
14081408
test "$enable_tlsx" = "" && enable_tlsx=no
1409+
test "$enable_recordsizelimit" = "" && enable_recordsizelimit=no
14091410
test "$enable_sni" = "" && enable_sni=no
14101411
test "$enable_crl_monitor" = "" && enable_crl_monitor=no
14111412
test "$enable_alpn" = "" && enable_alpn=no
@@ -1583,6 +1584,7 @@ then
15831584
test "$enable_earlydata" = "" && enable_earlydata=yes
15841585
test "$enable_rpk" = "" && enable_rpk=yes
15851586

1587+
test "$enable_recordsizelimit" = "" && enable_recordsizelimit=yes
15861588
if test "$KERNEL_MODE_DEFAULTS" != "yes"
15871589
then
15881590
# Disable QUIC with JNI since incompatible with client-side
@@ -10228,6 +10230,10 @@ then
1022810230
ENABLED_ALPN=yes
1022910231
ENABLED_TRUSTED_CA=yes
1023010232
ENABLED_ENCRYPT_THEN_MAC=yes
10233+
dnl A seed rather than an ENABLED_ var: this block runs before that option
10234+
dnl is declared, and AC_ARG_ENABLE keys off ${enable_X+set}. It is a pure
10235+
dnl extension, which is what --enable-tlsx means.
10236+
test "$enable_recordsizelimit" = "" && enable_recordsizelimit=yes
1023110237
AM_CFLAGS="$AM_CFLAGS -DHAVE_TLS_EXTENSIONS -DHAVE_SNI -DHAVE_MAX_FRAGMENT -DHAVE_TRUNCATED_HMAC -DHAVE_ALPN -DHAVE_TRUSTED_CA"
1023210238
# Check the ECC supported curves prereq
1023310239
AS_IF([test "x$ENABLED_ECC" != "xno" || test "$ENABLED_CURVE25519" != "no" || test "x$ENABLED_CURVE448" = "xyes" || test "x$ENABLED_TLS13" = "xyes"],
@@ -11663,6 +11669,27 @@ AC_ARG_WITH([libz],
1166311669
]
1166411670
)
1166511671

11672+
# Record Size Limit (RFC 8449)
11673+
AC_ARG_ENABLE([recordsizelimit],
11674+
[AS_HELP_STRING([--enable-recordsizelimit],[Enable RFC 8449 record_size_limit, the byte-exact replacement for max_fragment_length. Applies to TLS 1.2 and TLS 1.3. A connection cannot both use it and compress its records, so wolfSSL_set_compression() and wolfSSL_UseRecordSizeLimit() refuse each other (default: disabled)])],
11675+
[ ENABLED_RECORD_SIZE_LIMIT=$enableval ],
11676+
[ ENABLED_RECORD_SIZE_LIMIT=no ]
11677+
)
11678+
dnl RFC 8449 defines the extension for TLS 1.2 as well as TLS 1.3, where the
11679+
dnl server answers in the ServerHello rather than EncryptedExtensions, so no
11680+
dnl TLS 1.3 dependency is imposed here. It does need TLS: the no-TLS defaults
11681+
dnl above only cover an unset option, so an explicit --enable-recordsizelimit
11682+
dnl is overridden here in a crypto-only or no-TLS build, as CMake does.
11683+
if test "$ENABLED_RECORD_SIZE_LIMIT" = "yes" && test "$ENABLED_TLS" = "no"
11684+
then
11685+
AC_MSG_WARN([--enable-recordsizelimit needs TLS; disabled in this no-TLS build])
11686+
ENABLED_RECORD_SIZE_LIMIT=no
11687+
fi
11688+
if test "$ENABLED_RECORD_SIZE_LIMIT" = "yes"
11689+
then
11690+
AM_CFLAGS="$AM_CFLAGS -DHAVE_TLS_EXTENSIONS -DHAVE_RECORD_SIZE_LIMIT"
11691+
fi
11692+
1166611693

1166711694
# PKCS#11
1166811695
AC_ARG_ENABLE([pkcs11],
@@ -15505,6 +15532,7 @@ echo " * Falcon: $ENABLED_FALCON"
1550515532
echo " * Server Name Indication: $ENABLED_SNI"
1550615533
echo " * ALPN: $ENABLED_ALPN"
1550715534
echo " * Maximum Fragment Length: $ENABLED_MAX_FRAGMENT"
15535+
echo " * Record Size Limit: $ENABLED_RECORD_SIZE_LIMIT"
1550815536
echo " * Trusted CA Indication: $ENABLED_TRUSTED_CA"
1550915537
echo " * Truncated HMAC: $ENABLED_TRUNCATED_HMAC"
1551015538
echo " * Supported Elliptic Curves: $ENABLED_SUPPORTED_CURVES"

‎doc/dox_comments/header_files/ssl.h‎

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8770,10 +8770,22 @@ int wolfSSL_negotiate(WOLFSSL* ssl);
87708770
transport is known when the WOLFSSL object is created, this is reported
87718771
rather than dropped.
87728772

8773+
A connection cannot both compress its records and use the RFC 8449
8774+
record_size_limit extension: the limit bounds the plaintext, which deflate
8775+
may expand past it. A limit the application set with
8776+
wolfSSL_UseRecordSizeLimit() or wolfSSL_CTX_UseRecordSizeLimit() is its
8777+
request, so this call is refused; the advertisement that is on by default
8778+
gives way instead, and such a connection offers no limit. It can give way
8779+
only before it is sent or answered, so call this before the handshake
8780+
starts: from a server's SNI callback, for one, it is refused whenever the
8781+
client offered record_size_limit.
8782+
87738783
\return SSL_SUCCESS upon success.
87748784
\return NOT_COMPILED_IN will be returned if compression support wasn’t
87758785
built into the library.
8776-
\return BAD_FUNC_ARG will be returned if ssl is NULL or is a DTLS session.
8786+
\return BAD_FUNC_ARG will be returned if ssl is NULL, is a DTLS session,
8787+
was given a record size limit, or has already advertised, sent or
8788+
received one.
87778789

87788790
\param ssl pointer to the SSL session, created with wolfSSL_new().
87798791

@@ -11171,12 +11183,13 @@ int wolfSSL_GetOutputSize(WOLFSSL* ssl, int inSz);
1117111183

1117211184
/*!
1117311185
\brief Returns the maximum record layer size for plaintext data. This
11174-
will correspond to either the maximum SSL/TLS record size as specified
11175-
by the protocol standard, the maximum TLS fragment size as set by the
11176-
TLS Max Fragment Length extension. This function is helpful when the
11177-
application has called wolfSSL_GetOutputSize() and received a INPUT_SIZE_E
11178-
error. This function must be called after the SSL/TLS handshake has been
11179-
completed.
11186+
will correspond to the smallest of the maximum SSL/TLS record size as
11187+
specified by the protocol standard, the maximum TLS fragment size as set
11188+
by the TLS Max Fragment Length extension, and the limit the peer
11189+
advertised with the RFC 8449 record_size_limit extension. This function is
11190+
helpful when the application has called wolfSSL_GetOutputSize() and
11191+
received a INPUT_SIZE_E error. This function must be called after the
11192+
SSL/TLS handshake has been completed.
1118011193

1118111194
\return size Upon success, the maximum output size will be returned
1118211195
\return BAD_FUNC_ARG will be returned upon invalid function argument,
@@ -11190,6 +11203,8 @@ int wolfSSL_GetOutputSize(WOLFSSL* ssl, int inSz);
1119011203
\endcode
1119111204

1119211205
\sa wolfSSL_GetOutputSize
11206+
\sa wolfSSL_UseRecordSizeLimit
11207+
\sa wolfSSL_UseMaxFragment
1119311208
*/
1119411209
int wolfSSL_GetMaxOutputSize(WOLFSSL* ssl);
1119511210

@@ -14100,9 +14115,105 @@ int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list,
1410014115

1410114116
\sa wolfSSL_new
1410214117
\sa wolfSSL_CTX_UseMaxFragment
14118+
\sa wolfSSL_UseRecordSizeLimit
1410314119
*/
1410414120
int wolfSSL_UseMaxFragment(WOLFSSL* ssl, unsigned char mfl);
1410514121

14122+
/*!
14123+
\brief Sets the largest record payload this end will accept, advertised as
14124+
the RFC 8449 record_size_limit extension. A client sends it in the
14125+
ClientHello; a server that received one answers in EncryptedExtensions
14126+
under TLS 1.3 or in the ServerHello under TLS 1.2. The extension only
14127+
takes effect when both ends send it, so the peer's records are capped only
14128+
once it has answered. It supersedes max_fragment_length, which offers four
14129+
fixed sizes rather than an exact byte count; where a peer sends both, RFC
14130+
8449 Sect. 5 says record_size_limit governs.
14131+
14132+
The count is payload. RFC 8449's field on the wire covers the whole TLS
14133+
1.3 TLSInnerPlaintext, so a request for n goes out as n+1 under TLS 1.3
14134+
and as n under TLS 1.2; the conversion is internal.
14135+
14136+
A limit is advertised by default. Pass WOLFSSL_RECORD_SIZE_LIMIT_OFF to
14137+
stop advertising one. Must be called before the handshake starts, since
14138+
the value is both advertised to the peer and enforced on arrival: on the
14139+
context, or on the object before wolfSSL_connect() or wolfSSL_accept().
14140+
A server callback is too late. A certificate setup callback and an SNI
14141+
callback both run after the ClientHello's extensions have been parsed, so
14142+
either is refused whenever the client sent record_size_limit - and a
14143+
client that sent the extension is the only case in which a server's limit
14144+
would be answered at all.
14145+
14146+
Record layer compression is refused on a connection that was given a
14147+
limit, and a limit is refused on one that compresses: RFC 8449 Sect. 4
14148+
measures the limit against the plaintext, which deflate may expand past
14149+
it. A limit set here is the application's request and wins, so
14150+
wolfSSL_set_compression() then fails; the advertisement that is on by
14151+
default gives way instead, and such a connection offers no limit.
14152+
14153+
The default limit stands aside for an application that asked for
14154+
max_fragment_length with wolfSSL_UseMaxFragment(), so a client that set
14155+
one keeps offering it alone. Calling this function explicitly overrides
14156+
that: the application has then asked for both, and RFC 8449 Sect. 5 says
14157+
record_size_limit governs.
14158+
14159+
\return WOLFSSL_SUCCESS upon success.
14160+
\return BAD_FUNC_ARG for any of four distinct causes: ssl is NULL, limit
14161+
is outside WOLFSSL_RECORD_SIZE_LIMIT_MIN..WOLFSSL_RECORD_SIZE_LIMIT_MAX
14162+
(and not WOLFSSL_RECORD_SIZE_LIMIT_OFF), the handshake has already begun,
14163+
or wolfSSL_set_compression() was called on this object. The last two are
14164+
state errors rather than bad arguments; they share the code so that
14165+
existing callers testing for failure are unaffected.
14166+
14167+
\param ssl pointer to a SSL object, created with wolfSSL_new().
14168+
\param limit largest record payload accepted, from
14169+
WOLFSSL_RECORD_SIZE_LIMIT_MIN (64) to WOLFSSL_RECORD_SIZE_LIMIT_MAX
14170+
(16384), or WOLFSSL_RECORD_SIZE_LIMIT_OFF (0) to advertise nothing.
14171+
Defaults to WOLFSSL_RECORD_SIZE_LIMIT_DEFAULT.
14172+
14173+
_Example_
14174+
\code
14175+
int ret = 0;
14176+
WOLFSSL* ssl = wolfSSL_new(ctx);
14177+
if (ssl == NULL) {
14178+
// ssl creation failed
14179+
}
14180+
ret = wolfSSL_UseRecordSizeLimit(ssl, 1024);
14181+
if (ret != WOLFSSL_SUCCESS) {
14182+
// limit rejected
14183+
}
14184+
\endcode
14185+
14186+
\sa wolfSSL_CTX_UseRecordSizeLimit
14187+
\sa wolfSSL_UseMaxFragment
14188+
\sa wolfSSL_GetMaxOutputSize
14189+
*/
14190+
int wolfSSL_UseRecordSizeLimit(WOLFSSL* ssl, unsigned short limit);
14191+
14192+
/*!
14193+
\brief Sets the record_size_limit inherited by every WOLFSSL object
14194+
created from this context afterwards. See wolfSSL_UseRecordSizeLimit() for
14195+
what the value means and how the extension is negotiated.
14196+
14197+
\return WOLFSSL_SUCCESS upon success.
14198+
\return BAD_FUNC_ARG when ctx is NULL or limit is out of range.
14199+
14200+
\param ctx pointer to a SSL context, created with wolfSSL_CTX_new().
14201+
\param limit largest record payload accepted, from
14202+
WOLFSSL_RECORD_SIZE_LIMIT_MIN (64) to WOLFSSL_RECORD_SIZE_LIMIT_MAX
14203+
(16384), or WOLFSSL_RECORD_SIZE_LIMIT_OFF (0) to advertise nothing.
14204+
14205+
_Example_
14206+
\code
14207+
int ret = wolfSSL_CTX_UseRecordSizeLimit(ctx, 512);
14208+
if (ret != WOLFSSL_SUCCESS) {
14209+
// limit rejected
14210+
}
14211+
\endcode
14212+
14213+
\sa wolfSSL_UseRecordSizeLimit
14214+
*/
14215+
int wolfSSL_CTX_UseRecordSizeLimit(WOLFSSL_CTX* ctx, unsigned short limit);
14216+
1410614217
/*!
1410714218
\brief This function is called on the client side to enable the use
1410814219
of Maximum Fragment Length for SSL objects created from the SSL context

0 commit comments

Comments
 (0)