Skip to content

Commit 1ccfd9c

Browse files
committed
dtls.c: exclude Extensions when calculating cookie.
Do not calculate the cookie using the Extensions as these are different between DTLS1.2 and DTLS1.3 https://datatracker.ietf.org/doc/html/rfc6347#section-4.2.1 When responding to a HelloVerifyRequest, the client MUST use the same parameter values (version, random, session_id, cipher_suites, compression_method) as it did in the original ClientHello. The server SHOULD use those values to generate its cookie and verify that they are correct upon cookie receipt. https://www.rfc-editor.org/rfc/rfc9147.html#section-5.3 The ClientHello up to, but not including the Extensions is the same for DTLS1.2 and DTLS1.3 Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
1 parent a83514b commit 1ccfd9c

1 file changed

Lines changed: 32 additions & 23 deletions

File tree

dtls.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ dtls_create_cookie(dtls_context_t *ctx,
465465
uint8 *msg, size_t msglen,
466466
uint8 *cookie, int *clen) {
467467
unsigned char buf[DTLS_HMAC_MAX];
468-
size_t e, fragment_length;
468+
uint8 *start;
469469
int len;
470470

471471
/* create cookie with HMAC-SHA256 over:
@@ -476,43 +476,52 @@ dtls_create_cookie(dtls_context_t *ctx,
476476
* - session id
477477
* - cipher_suites
478478
* - compression method
479+
*
480+
* See RFC6347, 4.2.1. Denial-of-Service Countermeasures, page 17
481+
*
482+
* "When responding to a HelloVerifyRequest, the client MUST use the same
483+
* parameter values (version, random, session_id, cipher_suites,
484+
* compression_method) as it did in the original ClientHello. The
485+
* server SHOULD use those values to generate its cookie and verify that
486+
* they are correct upon cookie receipt."
479487
*/
480488

481489
/* Note that the buffer size must fit with the default hash algorithm. */
482490

483491
dtls_hmac_context_t hmac_context;
484492
dtls_hmac_init(&hmac_context, ctx->cookie_secret, DTLS_COOKIE_SECRET_LENGTH);
485493

486-
dtls_hmac_update(&hmac_context,
487-
(unsigned char *)&session->addr, session->size);
494+
dtls_hmac_update(&hmac_context, (uint8 *)&session->addr, session->size);
488495

489-
/* feed in the beginning of the Client Hello up to and including the
490-
session id */
491-
e = DTLS_CH_LENGTH;
492-
if (e + DTLS_HS_LENGTH + sizeof(uint8_t) > msglen)
496+
if (DTLS_HS_LENGTH + DTLS_CH_LENGTH > msglen)
493497
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
494498

495-
e += dtls_uint8_to_int(msg + DTLS_HS_LENGTH + e) + sizeof(uint8_t);
499+
/* skip DTLS_HS_LENGTH */
500+
msg += DTLS_HS_LENGTH;
501+
msglen -= DTLS_HS_LENGTH;
502+
start = msg;
496503

497-
if (e + DTLS_HS_LENGTH > msglen)
498-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
504+
/* add DTLS_CH_LENGTH by forward msg pointer */
505+
msg += DTLS_CH_LENGTH;
506+
msglen -= DTLS_CH_LENGTH;
499507

500-
dtls_hmac_update(&hmac_context, msg + DTLS_HS_LENGTH, e);
508+
/* add session_id by forward msg pointer */
509+
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
510+
"create_cookie, session_id");
501511

502-
if (e + DTLS_HS_LENGTH + sizeof(uint8_t) > msglen)
503-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
504-
/* skip cookie bytes and length byte */
505-
e += dtls_uint8_to_int(msg + DTLS_HS_LENGTH + e);
506-
e += sizeof(uint8_t);
512+
dtls_hmac_update(&hmac_context, start, msg - start);
507513

508-
/* read fragment length and check for consistency */
509-
fragment_length = dtls_get_fragment_length(DTLS_HANDSHAKE_HEADER(msg));
510-
if ((fragment_length < e) || (e + DTLS_HS_LENGTH) > msglen)
511-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
514+
/* skip cookie. */
515+
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
516+
"create_cookie, cookie");
512517

513-
dtls_hmac_update(&hmac_context,
514-
msg + DTLS_HS_LENGTH + e,
515-
fragment_length - e);
518+
/* add cipher suites and compression by forward msg pointer. */
519+
start = msg;
520+
SKIP_VAR_FIELD(msg, msglen, uint16, DTLS_ALERT_HANDSHAKE_FAILURE,
521+
"create_cookie, cipher-suites");
522+
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
523+
"create_cookie, compression");
524+
dtls_hmac_update(&hmac_context, start, msg - start);
516525

517526
len = dtls_hmac_finalize(&hmac_context, buf);
518527

0 commit comments

Comments
 (0)