@@ -4647,6 +4647,13 @@ int SendTls13ClientHello(WOLFSSL* ssl)
46474647 ssl->session->sessionIDSz = 0;
46484648 ssl->options.tls13MiddleBoxCompat = 0;
46494649 }
4650+ #endif
4651+ #ifdef WOLFSSL_DTLS13
4652+ if (ssl->options.dtls) {
4653+ /* RFC 9147 Section 5: DTLS implementations do not use the
4654+ * TLS 1.3 "compatibility mode" */
4655+ ssl->options.tls13MiddleBoxCompat = 0;
4656+ }
46504657#endif
46514658 GetTls13SessionId(ssl, NULL, &sessIdSz);
46524659 args->length += (word16)sessIdSz;
@@ -5590,16 +5597,25 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55905597 }
55915598 else
55925599#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
5600+ #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13)
5601+ if (0
55935602#ifdef WOLFSSL_QUIC
5594- if (WOLFSSL_IS_QUIC(ssl)) {
5603+ || WOLFSSL_IS_QUIC(ssl)
5604+ #endif
5605+ #ifdef WOLFSSL_DTLS13
5606+ || ssl->options.dtls
5607+ #endif
5608+ ) {
5609+ /* RFC 9147 Section 5.3 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC
5610+ * ServerHello must have empty legacy_session_id_echo. */
55955611 if (args->sessIdSz != 0) {
55965612 WOLFSSL_MSG("args->sessIdSz != 0");
55975613 WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
55985614 return INVALID_PARAMETER;
55995615 }
56005616 }
56015617 else
5602- #endif /* WOLFSSL_QUIC */
5618+ #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */
56035619 if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 &&
56045620 XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0))
56055621 {
@@ -6562,6 +6578,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65626578 word16 length;
65636579 int keyShareExt = 0;
65646580 int ret;
6581+ byte sessIdSz;
65656582
65666583 ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len);
65676584 if (ret < 0)
@@ -6586,7 +6603,13 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65866603 return ret;
65876604
65886605 /* Reconstruct the HelloRetryMessage for handshake hash. */
6589- length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz +
6606+ sessIdSz = ssl->session->sessionIDSz;
6607+ #ifdef WOLFSSL_DTLS13
6608+ /* RFC 9147 Section 5.3: DTLS 1.3 must use empty legacy_session_id. */
6609+ if (ssl->options.dtls)
6610+ sessIdSz = 0;
6611+ #endif
6612+ length = HRR_BODY_SZ - ID_LEN + sessIdSz +
65906613 HRR_COOKIE_HDR_SZ + cookie->len;
65916614 length += HRR_VERSIONS_SZ;
65926615 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
@@ -6613,10 +6636,10 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66136636 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66146637 hrrIdx += RAN_LEN;
66156638
6616- hrr[hrrIdx++] = ssl->session->sessionIDSz ;
6617- if (ssl->session->sessionIDSz > 0) {
6618- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz );
6619- hrrIdx += ssl->session->sessionIDSz ;
6639+ hrr[hrrIdx++] = sessIdSz ;
6640+ if (sessIdSz > 0) {
6641+ XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz );
6642+ hrrIdx += sessIdSz ;
66206643 }
66216644
66226645 /* Restore the cipher suite from the cookie. */
@@ -6629,7 +6652,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66296652 hrr[hrrIdx++] = 0;
66306653
66316654 /* Extensions' length */
6632- length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz ;
6655+ length -= HRR_BODY_SZ - ID_LEN + sessIdSz ;
66336656 c16toa(length, hrr + hrrIdx);
66346657 hrrIdx += 2;
66356658
@@ -7054,9 +7077,20 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70547077 if (sessIdSz + args->idx > helloSz)
70557078 ERROR_OUT(BUFFER_ERROR, exit_dch);
70567079
7057- ssl->session->sessionIDSz = sessIdSz;
7058- if (sessIdSz > 0)
7059- XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7080+ #ifdef WOLFSSL_DTLS13
7081+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7082+ * legacy_session_id_echo. Don't store the client's value so it
7083+ * won't be echoed in SendTls13ServerHello. */
7084+ if (ssl->options.dtls) {
7085+ ssl->session->sessionIDSz = 0;
7086+ }
7087+ else
7088+ #endif
7089+ {
7090+ ssl->session->sessionIDSz = sessIdSz;
7091+ if (sessIdSz > 0)
7092+ XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7093+ }
70607094 args->idx += sessIdSz;
70617095
70627096#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
@@ -7629,10 +7663,21 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76297663 WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
76307664#endif
76317665
7632- output[idx++] = ssl->session->sessionIDSz;
7633- if (ssl->session->sessionIDSz > 0) {
7634- XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz);
7635- idx += ssl->session->sessionIDSz;
7666+ #ifdef WOLFSSL_DTLS13
7667+ if (ssl->options.dtls) {
7668+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7669+ * legacy_session_id_echo. */
7670+ output[idx++] = 0;
7671+ }
7672+ else
7673+ #endif
7674+ {
7675+ output[idx++] = ssl->session->sessionIDSz;
7676+ if (ssl->session->sessionIDSz > 0) {
7677+ XMEMCPY(output + idx, ssl->session->sessionID,
7678+ ssl->session->sessionIDSz);
7679+ idx += ssl->session->sessionIDSz;
7680+ }
76367681 }
76377682
76387683 /* Chosen cipher suite */
0 commit comments