@@ -4574,8 +4574,8 @@ int SendTls13ClientHello(WOLFSSL* ssl)
45744574 }
45754575#endif /* WOLFSSL_DTLS */
45764576
4577- #ifdef HAVE_SESSION_TICKET
45784577 if (ssl->options.resuming &&
4578+ ssl->session->version.major != 0 &&
45794579 (ssl->session->version.major != ssl->version.major ||
45804580 ssl->session->version.minor != ssl->version.minor)) {
45814581 #ifndef WOLFSSL_NO_TLS12
@@ -4594,7 +4594,6 @@ int SendTls13ClientHello(WOLFSSL* ssl)
45944594 return VERSION_ERROR;
45954595 }
45964596 }
4597- #endif
45984597
45994598 suites = WOLFSSL_SUITES(ssl);
46004599 if (suites == NULL) {
@@ -4648,6 +4647,13 @@ int SendTls13ClientHello(WOLFSSL* ssl)
46484647 ssl->session->sessionIDSz = 0;
46494648 ssl->options.tls13MiddleBoxCompat = 0;
46504649 }
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+ }
46514657#endif
46524658 GetTls13SessionId(ssl, NULL, &sessIdSz);
46534659 args->length += (word16)sessIdSz;
@@ -5591,16 +5597,25 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55915597 }
55925598 else
55935599#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
5600+ #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13)
5601+ if (0
55945602#ifdef WOLFSSL_QUIC
5595- 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. */
55965611 if (args->sessIdSz != 0) {
55975612 WOLFSSL_MSG("args->sessIdSz != 0");
55985613 WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
55995614 return INVALID_PARAMETER;
56005615 }
56015616 }
56025617 else
5603- #endif /* WOLFSSL_QUIC */
5618+ #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */
56045619 if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 &&
56055620 XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0))
56065621 {
@@ -6563,6 +6578,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65636578 word16 length;
65646579 int keyShareExt = 0;
65656580 int ret;
6581+ byte sessIdSz;
65666582
65676583 ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len);
65686584 if (ret < 0)
@@ -6587,7 +6603,13 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65876603 return ret;
65886604
65896605 /* Reconstruct the HelloRetryMessage for handshake hash. */
6590- 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 +
65916613 HRR_COOKIE_HDR_SZ + cookie->len;
65926614 length += HRR_VERSIONS_SZ;
65936615 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
@@ -6614,10 +6636,10 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66146636 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66156637 hrrIdx += RAN_LEN;
66166638
6617- hrr[hrrIdx++] = ssl->session->sessionIDSz ;
6618- if (ssl->session->sessionIDSz > 0) {
6619- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz );
6620- hrrIdx += ssl->session->sessionIDSz ;
6639+ hrr[hrrIdx++] = sessIdSz ;
6640+ if (sessIdSz > 0) {
6641+ XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz );
6642+ hrrIdx += sessIdSz ;
66216643 }
66226644
66236645 /* Restore the cipher suite from the cookie. */
@@ -6630,7 +6652,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66306652 hrr[hrrIdx++] = 0;
66316653
66326654 /* Extensions' length */
6633- length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz ;
6655+ length -= HRR_BODY_SZ - ID_LEN + sessIdSz ;
66346656 c16toa(length, hrr + hrrIdx);
66356657 hrrIdx += 2;
66366658
@@ -7055,9 +7077,20 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70557077 if (sessIdSz + args->idx > helloSz)
70567078 ERROR_OUT(BUFFER_ERROR, exit_dch);
70577079
7058- ssl->session->sessionIDSz = sessIdSz;
7059- if (sessIdSz > 0)
7060- 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+ }
70617094 args->idx += sessIdSz;
70627095
70637096#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
@@ -7630,10 +7663,21 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76307663 WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
76317664#endif
76327665
7633- output[idx++] = ssl->session->sessionIDSz;
7634- if (ssl->session->sessionIDSz > 0) {
7635- XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz);
7636- 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+ }
76377681 }
76387682
76397683 /* Chosen cipher suite */
0 commit comments