@@ -4570,7 +4570,6 @@ int SendTls13ClientHello(WOLFSSL* ssl)
45704570 }
45714571#endif /* WOLFSSL_DTLS */
45724572
4573- #ifdef HAVE_SESSION_TICKET
45744573 if (ssl->options.resuming &&
45754574 (ssl->session->version.major != ssl->version.major ||
45764575 ssl->session->version.minor != ssl->version.minor)) {
@@ -4590,7 +4589,6 @@ int SendTls13ClientHello(WOLFSSL* ssl)
45904589 return VERSION_ERROR;
45914590 }
45924591 }
4593- #endif
45944592
45954593 suites = WOLFSSL_SUITES(ssl);
45964594 if (suites == NULL) {
@@ -4644,6 +4642,13 @@ int SendTls13ClientHello(WOLFSSL* ssl)
46444642 ssl->session->sessionIDSz = 0;
46454643 ssl->options.tls13MiddleBoxCompat = 0;
46464644 }
4645+ #endif
4646+ #ifdef WOLFSSL_DTLS13
4647+ if (ssl->options.dtls) {
4648+ /* RFC 9147 Section 5: DTLS implementations do not use the
4649+ * TLS 1.3 "compatibility mode" */
4650+ ssl->options.tls13MiddleBoxCompat = 0;
4651+ }
46474652#endif
46484653 GetTls13SessionId(ssl, NULL, &sessIdSz);
46494654 args->length += (word16)sessIdSz;
@@ -5587,16 +5592,25 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55875592 }
55885593 else
55895594#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
5595+ #if defined(WOLFSSL_QUIC) || defined(WOLFSSL_DTLS13)
5596+ if (0
55905597#ifdef WOLFSSL_QUIC
5591- if (WOLFSSL_IS_QUIC(ssl)) {
5598+ || WOLFSSL_IS_QUIC(ssl)
5599+ #endif
5600+ #ifdef WOLFSSL_DTLS13
5601+ || ssl->options.dtls
5602+ #endif
5603+ ) {
5604+ /* RFC 9147 Section 5.3 / RFC 9001 Section 8.4: DTLS 1.3 and QUIC
5605+ * ServerHello must have empty legacy_session_id_echo. */
55925606 if (args->sessIdSz != 0) {
55935607 WOLFSSL_MSG("args->sessIdSz != 0");
55945608 WOLFSSL_ERROR_VERBOSE(INVALID_PARAMETER);
55955609 return INVALID_PARAMETER;
55965610 }
55975611 }
55985612 else
5599- #endif /* WOLFSSL_QUIC */
5613+ #endif /* WOLFSSL_QUIC || WOLFSSL_DTLS13 */
56005614 if (args->sessIdSz != ssl->session->sessionIDSz || (args->sessIdSz > 0 &&
56015615 XMEMCMP(ssl->session->sessionID, args->sessId, args->sessIdSz) != 0))
56025616 {
@@ -6559,6 +6573,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65596573 word16 length;
65606574 int keyShareExt = 0;
65616575 int ret;
6576+ byte sessIdSz;
65626577
65636578 ret = TlsCheckCookie(ssl, cookie->data, (byte)cookie->len);
65646579 if (ret < 0)
@@ -6583,7 +6598,13 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
65836598 return ret;
65846599
65856600 /* Reconstruct the HelloRetryMessage for handshake hash. */
6586- length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz +
6601+ sessIdSz = ssl->session->sessionIDSz;
6602+ #ifdef WOLFSSL_DTLS13
6603+ /* RFC 9147 Section 5.3: DTLS 1.3 must use empty legacy_session_id. */
6604+ if (ssl->options.dtls)
6605+ sessIdSz = 0;
6606+ #endif
6607+ length = HRR_BODY_SZ - ID_LEN + sessIdSz +
65876608 HRR_COOKIE_HDR_SZ + cookie->len;
65886609 length += HRR_VERSIONS_SZ;
65896610 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
@@ -6610,10 +6631,10 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66106631 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66116632 hrrIdx += RAN_LEN;
66126633
6613- hrr[hrrIdx++] = ssl->session->sessionIDSz ;
6614- if (ssl->session->sessionIDSz > 0) {
6615- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz );
6616- hrrIdx += ssl->session->sessionIDSz ;
6634+ hrr[hrrIdx++] = sessIdSz ;
6635+ if (sessIdSz > 0) {
6636+ XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, sessIdSz );
6637+ hrrIdx += sessIdSz ;
66176638 }
66186639
66196640 /* Restore the cipher suite from the cookie. */
@@ -6626,7 +6647,7 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66266647 hrr[hrrIdx++] = 0;
66276648
66286649 /* Extensions' length */
6629- length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz ;
6650+ length -= HRR_BODY_SZ - ID_LEN + sessIdSz ;
66306651 c16toa(length, hrr + hrrIdx);
66316652 hrrIdx += 2;
66326653
@@ -7051,9 +7072,20 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70517072 if (sessIdSz + args->idx > helloSz)
70527073 ERROR_OUT(BUFFER_ERROR, exit_dch);
70537074
7054- ssl->session->sessionIDSz = sessIdSz;
7055- if (sessIdSz > 0)
7056- XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7075+ #ifdef WOLFSSL_DTLS13
7076+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7077+ * legacy_session_id_echo. Don't store the client's value so it
7078+ * won't be echoed in SendTls13ServerHello. */
7079+ if (ssl->options.dtls) {
7080+ ssl->session->sessionIDSz = 0;
7081+ }
7082+ else
7083+ #endif
7084+ {
7085+ ssl->session->sessionIDSz = sessIdSz;
7086+ if (sessIdSz > 0)
7087+ XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7088+ }
70577089 args->idx += sessIdSz;
70587090
70597091#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
@@ -7626,10 +7658,21 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76267658 WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
76277659#endif
76287660
7629- output[idx++] = ssl->session->sessionIDSz;
7630- if (ssl->session->sessionIDSz > 0) {
7631- XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz);
7632- idx += ssl->session->sessionIDSz;
7661+ #ifdef WOLFSSL_DTLS13
7662+ if (ssl->options.dtls) {
7663+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7664+ * legacy_session_id_echo. */
7665+ output[idx++] = 0;
7666+ }
7667+ else
7668+ #endif
7669+ {
7670+ output[idx++] = ssl->session->sessionIDSz;
7671+ if (ssl->session->sessionIDSz > 0) {
7672+ XMEMCPY(output + idx, ssl->session->sessionID,
7673+ ssl->session->sessionIDSz);
7674+ idx += ssl->session->sessionIDSz;
7675+ }
76337676 }
76347677
76357678 /* Chosen cipher suite */
0 commit comments