Skip to content

Commit aed780b

Browse files
committed
Incorporate review comments
1 parent 45fa35f commit aed780b

2 files changed

Lines changed: 58 additions & 43 deletions

File tree

src/dtls.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -295,42 +295,6 @@ static int CheckDtlsCookie(const WOLFSSL* ssl, WolfSSL_CH* ch,
295295
return ret;
296296
}
297297

298-
#if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
299-
static int ParseCookieExt(WOLFSSL* ssl, WolfSSL_CH* ch)
300-
{
301-
int ret = 0;
302-
word16 len;
303-
const byte* cookie = NULL;
304-
305-
if (ch->cookieExt.size < OPAQUE16_LEN + 1)
306-
return BUFFER_E;
307-
ato16(ch->cookieExt.elements, &len);
308-
if (ch->cookieExt.size - OPAQUE16_LEN != len)
309-
return BUFFER_E;
310-
cookie = ch->cookieExt.elements + OPAQUE16_LEN;
311-
ret = TlsCheckCookie(ssl, cookie, len);
312-
if (ret < 0)
313-
return ret;
314-
len = (word16)ret;
315-
316-
/* Cookie Data = Hash Len | Hash | CS | KeyShare Group (optional) */
317-
318-
/* Check if the cookie has a key share group */
319-
if (len > cookie[0] + 4) {
320-
word16 keyShareGroup = 0;
321-
ato16(cookie + 3 + cookie[0], &keyShareGroup);
322-
323-
/* The key share group in the cookie is the group selected by the
324-
* server in the HelloRetryRequest. Hence, the client must use this
325-
* group in the second ClientHello.
326-
*/
327-
ssl->hrr_keyshare_group = keyShareGroup;
328-
}
329-
330-
return 0;
331-
}
332-
#endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE */
333-
334298
static int ParseClientHello(const byte* input, word32 helloSz, WolfSSL_CH* ch,
335299
byte isFirstCHFrag)
336300
{
@@ -1077,10 +1041,6 @@ int DoClientHelloStateless(WOLFSSL* ssl, const byte* input, word32 helloSz,
10771041
e = Dtls13GetEpoch(ssl, ssl->keys.curEpoch64);
10781042
if (e != NULL)
10791043
XMEMSET(e->window, 0xFF, sizeof(e->window));
1080-
1081-
ret = ParseCookieExt(ssl, &ch);
1082-
if (ret != 0)
1083-
return ret;
10841044
}
10851045
else
10861046
#endif

src/tls.c

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)