@@ -4663,6 +4663,13 @@ int SendTls13ClientHello(WOLFSSL* ssl)
46634663 ssl->session->sessionIDSz = 0;
46644664 ssl->options.tls13MiddleBoxCompat = 0;
46654665 }
4666+ #endif
4667+ #ifdef WOLFSSL_DTLS13
4668+ if (ssl->options.dtls) {
4669+ /* RFC 9147 Section 5: DTLS implementations do not use the
4670+ * TLS 1.3 "compatibility mode" */
4671+ ssl->options.tls13MiddleBoxCompat = 0;
4672+ }
46664673#endif
46674674 GetTls13SessionId(ssl, NULL, &sessIdSz);
46684675 args->length += (word16)sessIdSz;
@@ -5581,18 +5588,6 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55815588
55825589 case TLS_ASYNC_FINALIZE:
55835590 {
5584- #ifdef WOLFSSL_DTLS13
5585- /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
5586- * legacy_session_id_echo. */
5587- if (ssl->options.dtls) {
5588- if (args->sessIdSz != 0) {
5589- WOLFSSL_MSG("DTLS 1.3 ServerHello must have empty session ID");
5590- WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
5591- return INVALID_PARAMETER;
5592- }
5593- }
5594- else
5595- #endif
55965591#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
55975592 if (ssl->options.tls13MiddleBoxCompat) {
55985593 if (args->sessIdSz == 0) {
@@ -5618,16 +5613,25 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
56185613 }
56195614 else
56205615#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
5616+ #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13)
5617+ if (0
56215618#ifdef WOLFSSL_QUIC
5622- if (WOLFSSL_IS_QUIC(ssl)) {
5619+ || WOLFSSL_IS_QUIC(ssl)
5620+ #endif
5621+ #ifdef WOLFSSL_DTLS13
5622+ || ssl->options.dtls
5623+ #endif
5624+ ) {
5625+ /* RFC 9147 Section 5.3 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC
5626+ * ServerHello must have empty legacy_session_id_echo. */
56235627 if (args->sessIdSz != 0) {
56245628 WOLFSSL_MSG("args->sessIdSz != 0");
56255629 WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
56265630 return INVALID_PARAMETER;
56275631 }
56285632 }
56295633 else
5630- #endif /* WOLFSSL_QUIC */
5634+ #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */
56315635 if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 &&
56325636 XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0))
56335637 {
@@ -6595,6 +6599,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65956599 word16 length;
65966600 int keyShareExt = 0;
65976601 int ret;
6602+ byte sessIdSz;
65986603
65996604 ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len);
66006605 if (ret < 0)
@@ -6619,11 +6624,14 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66196624 return ret;
66206625
66216626 /* Reconstruct the HelloRetryMessage for handshake hash. */
6622- length = HRR_BODY_SZ - ID_LEN + HRR_COOKIE_HDR_SZ + cookie->len ;
6627+ sessIdSz = ssl->session->sessionIDSz ;
66236628#ifdef WOLFSSL_DTLS13
6624- if (!ssl->options.dtls)
6629+ /* RFC 9147 Section 5.3: DTLS 1.3 must use empty legacy_session_id. */
6630+ if (ssl->options.dtls)
6631+ sessIdSz = 0;
66256632#endif
6626- length += ssl->session->sessionIDSz;
6633+ length = HRR_BODY_SZ - ID_LEN + sessIdSz +
6634+ HRR_COOKIE_HDR_SZ + cookie->len;
66276635 length += HRR_VERSIONS_SZ;
66286636 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
66296637 if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) {
@@ -6649,17 +6657,10 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66496657 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66506658 hrrIdx += RAN_LEN;
66516659
6652- #ifdef WOLFSSL_DTLS13
6653- if (ssl->options.dtls)
6654- hrr[hrrIdx++] = 0;
6655- else
6656- #endif
6657- {
6658- hrr[hrrIdx++] = ssl->session->sessionIDSz;
6659- if (ssl->session->sessionIDSz > 0) {
6660- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz);
6661- hrrIdx += ssl->session->sessionIDSz;
6662- }
6660+ hrr[hrrIdx++] = sessIdSz;
6661+ if (sessIdSz > 0) {
6662+ XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz);
6663+ hrrIdx += sessIdSz;
66636664 }
66646665
66656666 /* Cipher Suite */
@@ -6670,11 +6671,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66706671 hrr[hrrIdx++] = 0;
66716672
66726673 /* Extensions' length */
6673- length -= HRR_BODY_SZ - ID_LEN;
6674- #ifdef WOLFSSL_DTLS13
6675- if (!ssl->options.dtls)
6676- #endif
6677- length -= ssl->session->sessionIDSz;
6674+ length -= HRR_BODY_SZ - ID_LEN + sessIdSz;
66786675 c16toa(length, hrr + hrrIdx);
66796676 hrrIdx += 2;
66806677
@@ -7103,7 +7100,10 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
71037100 /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
71047101 * legacy_session_id_echo. Don't store the client's value so it
71057102 * won't be echoed in SendTls13ServerHello. */
7106- if (!ssl->options.dtls)
7103+ if (ssl->options.dtls) {
7104+ ssl->session->sessionIDSz = 0;
7105+ }
7106+ else
71077107#endif
71087108 {
71097109 ssl->session->sessionIDSz = sessIdSz;
@@ -7613,13 +7613,8 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76137613 /* Protocol version, server random, session id, cipher suite, compression
76147614 * and extensions.
76157615 */
7616- length = VERSION_SZ + RAN_LEN + ENUM_LEN + SUITE_LEN + COMP_LEN;
7617- #ifdef WOLFSSL_DTLS13
7618- /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7619- * legacy_session_id_echo. */
7620- if (!ssl->options.dtls)
7621- #endif
7622- length += ssl->session->sessionIDSz;
7616+ length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz +
7617+ SUITE_LEN + COMP_LEN;
76237618 ret = TLSX_GetResponseSize(ssl, extMsgType, &length);
76247619 if (ret != 0)
76257620 return ret;
0 commit comments