@@ -7248,6 +7248,53 @@ static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
72487248 return 0;
72497249}
72507250
7251+ #ifdef WOLFSSL_DTLS13
7252+ /* Extract the key share group from the cookie and store it in the
7253+ * ssl session for later checks.
7254+ *
7255+ * ssl The SSL/TLS object.
7256+ * returns 0 on success and other values indicate failure.
7257+ */
7258+ static int TLSX_Cookie_RestoreHrrGroup(WOLFSSL* ssl)
7259+ {
7260+ TLSX* extension;
7261+ Cookie* cookie;
7262+ #ifndef NO_SHA256
7263+ byte macSz = WC_SHA256_DIGEST_SIZE;
7264+ #elif defined(WOLFSSL_SHA384)
7265+ byte macSz = WC_SHA384_DIGEST_SIZE;
7266+ #elif defined(WOLFSSL_TLS13_SHA512)
7267+ byte macSz = WC_SHA512_DIGEST_SIZE;
7268+ #elif defined(WOLFSSL_SM3)
7269+ byte macSz = WC_SM3_DIGEST_SIZE;
7270+ #endif /* NO_SHA */
7271+
7272+ extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
7273+ if (extension == NULL)
7274+ return 0;
7275+
7276+ cookie = (Cookie*)extension->data;
7277+ if (cookie == NULL)
7278+ return 0;
7279+
7280+ /* Cookie Data = Hash Len | Hash | CS | KeyShare Group (optional) | MAC */
7281+
7282+ /* Check if the cookie has a key share group */
7283+ if (cookie->data[0] + 4 + macSz < cookie->len) {
7284+ word16 keyShareGroup = 0;
7285+ ato16(cookie->data + 3 + cookie->data[0], &keyShareGroup);
7286+
7287+ /* The key share group in the cookie is the group selected by the
7288+ * server in the HelloRetryRequest. Hence, the client must use this
7289+ * group in the second ClientHello.
7290+ */
7291+ ssl->hrr_keyshare_group = keyShareGroup;
7292+ }
7293+
7294+ return 0;
7295+ }
7296+ #endif /* WOLFSSL_DTLS13 */
7297+
72517298/* Parse the Cookie extension.
72527299 * In messages: ClientHello and HelloRetryRequest.
72537300 *
@@ -7290,12 +7337,20 @@ static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
72907337 extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
72917338 if (extension == NULL) {
72927339#ifdef WOLFSSL_DTLS13
7293- if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
7340+ if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) {
7341+ int ret = 0;
72947342 /* Allow a cookie extension with DTLS 1.3 because it is possible
72957343 * that a different SSL instance sent the cookie but we are now
72967344 * receiving it. */
7297- return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
7298- &ssl->extensions);
7345+ ret = TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
7346+ &ssl->extensions);
7347+
7348+ if (ret == 0 && ssl->options.dtlsStateful) {
7349+ /* Try to extract a HRR key share group from the cookie */
7350+ ret = TLSX_Cookie_RestoreHrrGroup(ssl);
7351+ }
7352+ return ret;
7353+ }
72997354 else
73007355#endif
73017356 {
0 commit comments