@@ -4643,6 +4643,13 @@ int SendTls13ClientHello(WOLFSSL* ssl)
46434643 ssl->session->sessionIDSz = 0;
46444644 ssl->options.tls13MiddleBoxCompat = 0;
46454645 }
4646+ #endif
4647+ #ifdef WOLFSSL_DTLS13
4648+ if (ssl->options.dtls) {
4649+ /* RFC 9147 Section 5: DTLS implementations do not use the
4650+ * TLS 1.3 "compatibility mode" */
4651+ ssl->options.tls13MiddleBoxCompat = 0;
4652+ }
46464653#endif
46474654 GetTls13SessionId(ssl, NULL, &sessIdSz);
46484655 args->length += (word16)sessIdSz;
@@ -5586,16 +5593,25 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55865593 }
55875594 else
55885595#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
5596+ #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13)
5597+ if (0
55895598#ifdef WOLFSSL_QUIC
5590- if (WOLFSSL_IS_QUIC(ssl)) {
5599+ || WOLFSSL_IS_QUIC(ssl)
5600+ #endif
5601+ #ifdef WOLFSSL_DTLS13
5602+ || ssl->options.dtls
5603+ #endif
5604+ ) {
5605+ /* RFC 9147 Section 5.3 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC
5606+ * ServerHello must have empty legacy_session_id_echo. */
55915607 if (args->sessIdSz != 0) {
55925608 WOLFSSL_MSG("args->sessIdSz != 0");
55935609 WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
55945610 return INVALID_PARAMETER;
55955611 }
55965612 }
55975613 else
5598- #endif /* WOLFSSL_QUIC */
5614+ #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */
55995615 if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 &&
56005616 XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0))
56015617 {
@@ -6558,6 +6574,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65586574 word16 length;
65596575 int keyShareExt = 0;
65606576 int ret;
6577+ byte sessIdSz;
65616578
65626579 ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len);
65636580 if (ret < 0)
@@ -6582,7 +6599,13 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65826599 return ret;
65836600
65846601 /* Reconstruct the HelloRetryMessage for handshake hash. */
6585- length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz +
6602+ sessIdSz = ssl->session->sessionIDSz;
6603+ #ifdef WOLFSSL_DTLS13
6604+ /* RFC 9147 Section 5.3: DTLS 1.3 must use empty legacy_session_id. */
6605+ if (ssl->options.dtls)
6606+ sessIdSz = 0;
6607+ #endif
6608+ length = HRR_BODY_SZ - ID_LEN + sessIdSz +
65866609 HRR_COOKIE_HDR_SZ + cookie->len;
65876610 length += HRR_VERSIONS_SZ;
65886611 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
@@ -6609,10 +6632,10 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66096632 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66106633 hrrIdx += RAN_LEN;
66116634
6612- hrr[hrrIdx++] = ssl->session->sessionIDSz ;
6613- if (ssl->session->sessionIDSz > 0) {
6614- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz );
6615- hrrIdx += ssl->session->sessionIDSz ;
6635+ hrr[hrrIdx++] = sessIdSz ;
6636+ if (sessIdSz > 0) {
6637+ XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz );
6638+ hrrIdx += sessIdSz ;
66166639 }
66176640
66186641 /* Restore the cipher suite from the cookie. */
@@ -6625,7 +6648,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66256648 hrr[hrrIdx++] = 0;
66266649
66276650 /* Extensions' length */
6628- length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz ;
6651+ length -= HRR_BODY_SZ - ID_LEN + sessIdSz ;
66296652 c16toa(length, hrr + hrrIdx);
66306653 hrrIdx += 2;
66316654
@@ -7050,9 +7073,20 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70507073 if (sessIdSz + args->idx > helloSz)
70517074 ERROR_OUT(BUFFER_ERROR, exit_dch);
70527075
7053- ssl->session->sessionIDSz = sessIdSz;
7054- if (sessIdSz > 0)
7055- XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7076+ #ifdef WOLFSSL_DTLS13
7077+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7078+ * legacy_session_id_echo. Don't store the client's value so it
7079+ * won't be echoed in SendTls13ServerHello. */
7080+ if (ssl->options.dtls) {
7081+ ssl->session->sessionIDSz = 0;
7082+ }
7083+ else
7084+ #endif
7085+ {
7086+ ssl->session->sessionIDSz = sessIdSz;
7087+ if (sessIdSz > 0)
7088+ XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7089+ }
70567090 args->idx += sessIdSz;
70577091
70587092#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
@@ -7625,10 +7659,21 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76257659 WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
76267660#endif
76277661
7628- output[idx++] = ssl->session->sessionIDSz;
7629- if (ssl->session->sessionIDSz > 0) {
7630- XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz);
7631- idx += ssl->session->sessionIDSz;
7662+ #ifdef WOLFSSL_DTLS13
7663+ if (ssl->options.dtls) {
7664+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7665+ * legacy_session_id_echo. */
7666+ output[idx++] = 0;
7667+ }
7668+ else
7669+ #endif
7670+ {
7671+ output[idx++] = ssl->session->sessionIDSz;
7672+ if (ssl->session->sessionIDSz > 0) {
7673+ XMEMCPY(output + idx, ssl->session->sessionID,
7674+ ssl->session->sessionIDSz);
7675+ idx += ssl->session->sessionIDSz;
7676+ }
76327677 }
76337678
76347679 /* Chosen cipher suite */
0 commit comments