@@ -5581,6 +5581,18 @@ int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
55815581
55825582 case TLS_ASYNC_FINALIZE:
55835583 {
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
55845596#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
55855597 if (ssl->options.tls13MiddleBoxCompat) {
55865598 if (args->sessIdSz == 0) {
@@ -6607,8 +6619,11 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66076619 return ret;
66086620
66096621 /* Reconstruct the HelloRetryMessage for handshake hash. */
6610- length = HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz +
6611- HRR_COOKIE_HDR_SZ + cookie->len;
6622+ length = HRR_BODY_SZ - ID_LEN + HRR_COOKIE_HDR_SZ + cookie->len;
6623+ #ifdef WOLFSSL_DTLS13
6624+ if (!ssl->options.dtls)
6625+ #endif
6626+ length += ssl->session->sessionIDSz;
66126627 length += HRR_VERSIONS_SZ;
66136628 /* HashSz (1 byte) + Hash (HashSz bytes) + CipherSuite (2 bytes) */
66146629 if (cookieDataSz > OPAQUE8_LEN + hashSz + OPAQUE16_LEN) {
@@ -6634,10 +6649,17 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66346649 XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
66356650 hrrIdx += RAN_LEN;
66366651
6637- hrr[hrrIdx++] = ssl->session->sessionIDSz;
6638- if (ssl->session->sessionIDSz > 0) {
6639- XMEMCPY(hrr + hrrIdx, ssl->session->sessionID, ssl->session->sessionIDSz);
6640- hrrIdx += ssl->session->sessionIDSz;
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+ }
66416663 }
66426664
66436665 /* Cipher Suite */
@@ -6648,7 +6670,11 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
66486670 hrr[hrrIdx++] = 0;
66496671
66506672 /* Extensions' length */
6651- length -= HRR_BODY_SZ - ID_LEN + ssl->session->sessionIDSz;
6673+ length -= HRR_BODY_SZ - ID_LEN;
6674+ #ifdef WOLFSSL_DTLS13
6675+ if (!ssl->options.dtls)
6676+ #endif
6677+ length -= ssl->session->sessionIDSz;
66526678 c16toa(length, hrr + hrrIdx);
66536679 hrrIdx += 2;
66546680
@@ -7073,9 +7099,17 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70737099 if (sessIdSz + args->idx > helloSz)
70747100 ERROR_OUT(BUFFER_ERROR, exit_dch);
70757101
7076- ssl->session->sessionIDSz = sessIdSz;
7077- if (sessIdSz > 0)
7078- XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7102+ #ifdef WOLFSSL_DTLS13
7103+ /* RFC 9147 Section 5.3: DTLS 1.3 ServerHello must have empty
7104+ * legacy_session_id_echo. Don't store the client's value so it
7105+ * won't be echoed in SendTls13ServerHello. */
7106+ if (!ssl->options.dtls)
7107+ #endif
7108+ {
7109+ ssl->session->sessionIDSz = sessIdSz;
7110+ if (sessIdSz > 0)
7111+ XMEMCPY(ssl->session->sessionID, input + args->idx, sessIdSz);
7112+ }
70797113 args->idx += sessIdSz;
70807114
70817115#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
@@ -7579,8 +7613,13 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
75797613 /* Protocol version, server random, session id, cipher suite, compression
75807614 * and extensions.
75817615 */
7582- length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session->sessionIDSz +
7583- SUITE_LEN + COMP_LEN;
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;
75847623 ret = TLSX_GetResponseSize(ssl, extMsgType, &length);
75857624 if (ret != 0)
75867625 return ret;
@@ -7624,10 +7663,21 @@ int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
76247663 WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
76257664#endif
76267665
7627- output[idx++] = ssl->session->sessionIDSz;
7628- if (ssl->session->sessionIDSz > 0) {
7629- XMEMCPY(output + idx, ssl->session->sessionID, ssl->session->sessionIDSz);
7630- 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+ }
76317681 }
76327682
76337683 /* Chosen cipher suite */
0 commit comments