Skip to content

Commit e54b4fd

Browse files
committed
Move HRR group restore and check out of extension parsing
1 parent 48116ab commit e54b4fd

2 files changed

Lines changed: 36 additions & 73 deletions

File tree

src/tls.c

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7248,55 +7248,6 @@ static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
72487248
return 0;
72497249
}
72507250

7251-
#if defined(WOLFSSL_DTLS13) && defined (WOLFSSL_SEND_HRR_COOKIE)
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-
#else
7271-
#error "No digest available to use with HMAC for cookies."
7272-
#endif /* NO_SHA */
7273-
7274-
extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
7275-
if (extension == NULL)
7276-
return 0;
7277-
7278-
cookie = (Cookie*)extension->data;
7279-
if (cookie == NULL)
7280-
return 0;
7281-
7282-
/* Cookie Data = Hash Len | Hash | CS | KeyShare Group (optional) | MAC */
7283-
7284-
/* Check if the cookie has a key share group */
7285-
if (cookie->data[0] + 4 + macSz < cookie->len) {
7286-
word16 keyShareGroup = 0;
7287-
ato16(cookie->data + 3 + cookie->data[0], &keyShareGroup);
7288-
7289-
/* The key share group in the cookie is the group selected by the
7290-
* server in the HelloRetryRequest. Hence, the client must use this
7291-
* group in the second ClientHello.
7292-
*/
7293-
ssl->hrr_keyshare_group = keyShareGroup;
7294-
}
7295-
7296-
return 0;
7297-
}
7298-
#endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE */
7299-
73007251
/* Parse the Cookie extension.
73017252
* In messages: ClientHello and HelloRetryRequest.
73027253
*
@@ -7340,19 +7291,11 @@ static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
73407291
if (extension == NULL) {
73417292
#ifdef WOLFSSL_DTLS13
73427293
if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) {
7343-
int ret = 0;
73447294
/* Allow a cookie extension with DTLS 1.3 because it is possible
73457295
* that a different SSL instance sent the cookie but we are now
73467296
* receiving it. */
7347-
ret = TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
7348-
&ssl->extensions);
7349-
#if defined(WOLFSSL_SEND_HRR_COOKIE)
7350-
if (ret == 0 && ssl->options.dtlsStateful) {
7351-
/* Try to extract a HRR key share group from the cookie */
7352-
ret = TLSX_Cookie_RestoreHrrGroup(ssl);
7353-
}
7354-
#endif
7355-
return ret;
7297+
return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0,
7298+
&ssl->extensions);
73567299
}
73577300
else
73587301
#endif
@@ -10274,20 +10217,6 @@ int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
1027410217
offset += ret;
1027510218
}
1027610219

10277-
if (ssl->hrr_keyshare_group != 0) {
10278-
/*
10279-
* https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8
10280-
* when sending the new ClientHello, the client MUST
10281-
* replace the original "key_share" extension with one containing only a
10282-
* new KeyShareEntry for the group indicated in the selected_group field
10283-
* of the triggering HelloRetryRequest
10284-
*/
10285-
if (seenGroupsCnt != 1 || seenGroups[0] != ssl->hrr_keyshare_group) {
10286-
WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
10287-
return BAD_KEY_SHARE_DATA;
10288-
}
10289-
}
10290-
1029110220
return 0;
1029210221
}
1029310222

src/tls13.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,17 @@ static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
64366436
cookieData = cookie->data;
64376437
idx = OPAQUE8_LEN;
64386438

6439+
#ifdef WOLFSSL_DTLS13
6440+
/* Restore the HRR key share group from the cookie.
6441+
* Cookie Data = Hash Len (1B) | Hash | CS (2B) | KS Group (2B, optional)
6442+
*/
6443+
if (cookieDataSz == hashSz + 5) {
6444+
word16 keyShareGroup = 0;
6445+
ato16(cookieData + hashSz + 3, &keyShareGroup);
6446+
ssl->hrr_keyshare_group = keyShareGroup;
6447+
}
6448+
#endif /* WOLFSSL_DTLS13 */
6449+
64396450
/* Restart handshake hash with synthetic message hash. */
64406451
AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl);
64416452

@@ -7015,6 +7026,29 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
70157026
}
70167027
#endif
70177028

7029+
#ifdef HAVE_SUPPORTED_CURVES
7030+
if (ssl->hrr_keyshare_group != 0) {
7031+
/*
7032+
* https://datatracker.ietf.org/doc/html/rfc8446#section-4.2.8
7033+
* when sending the new ClientHello, the client MUST
7034+
* replace the original "key_share" extension with one containing only
7035+
* a new KeyShareEntry for the group indicated in the selected_group
7036+
* field of the triggering HelloRetryRequest.
7037+
*/
7038+
TLSX* extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
7039+
if (extension != NULL) {
7040+
KeyShareEntry* kse = (KeyShareEntry*)extension->data;
7041+
/* Exactly one KeyShareEntry with the HRR group must be present. */
7042+
if (kse == NULL || kse->next != NULL ||
7043+
kse->group != ssl->hrr_keyshare_group) {
7044+
ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch);
7045+
}
7046+
}
7047+
else
7048+
ERROR_OUT(BAD_KEY_SHARE_DATA, exit_dch);
7049+
}
7050+
#endif
7051+
70187052
#if defined(HAVE_ECH)
70197053
/* hash clientHelloInner to hsHashesEch independently since it can't include
70207054
* the HRR */

0 commit comments

Comments
 (0)