Skip to content

Commit e599c28

Browse files
committed
dtls.c: cleanup dtls_create_cookie.
Use SKIP_VAR_FIELD and GET_VAR_FIELD. Remove fragment checks, already done ahead. Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
1 parent 010071a commit e599c28

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

dtls.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ memarray_t dtlscontext_storage;
8888
#define dtls_get_version(H) dtls_uint16_to_int((H)->version)
8989
#define dtls_get_epoch(H) dtls_uint16_to_int((H)->epoch)
9090
#define dtls_get_sequence_number(H) dtls_uint48_to_ulong((H)->sequence_number)
91-
#define dtls_get_fragment_length(H) dtls_uint24_to_int((H)->fragment_length)
9291

9392
#ifdef DTLS_PEERS_NOHASH
9493
#define FIND_PEER(head,sess,out) \
@@ -456,11 +455,11 @@ dtls_get_cookie(uint8 *msg, size_t msglen, uint8 **cookie) {
456455

457456
static int
458457
dtls_create_cookie(dtls_context_t *ctx,
459-
session_t *session,
460-
uint8 *msg, size_t msglen,
461-
uint8 *cookie, int *clen) {
458+
session_t *session,
459+
uint8 *msg, size_t msglen,
460+
uint8 *cookie, int *clen) {
462461
unsigned char buf[DTLS_HMAC_MAX];
463-
size_t e, fragment_length;
462+
uint8 *client_hello_msg;
464463
int len;
465464

466465
/* create cookie with HMAC-SHA256 over:
@@ -478,40 +477,35 @@ dtls_create_cookie(dtls_context_t *ctx,
478477
dtls_hmac_context_t hmac_context;
479478
dtls_hmac_init(&hmac_context, ctx->cookie_secret, DTLS_COOKIE_SECRET_LENGTH);
480479

481-
dtls_hmac_update(&hmac_context,
482-
(unsigned char *)&session->addr, session->size);
480+
dtls_hmac_update(&hmac_context, (uint8 *)&session->addr, session->size);
483481

484-
/* feed in the beginning of the Client Hello up to and including the
485-
session id */
486-
e = DTLS_CH_LENGTH;
487-
if (e + DTLS_HS_LENGTH + sizeof(uint8_t) > msglen)
488-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
482+
msg += DTLS_HS_LENGTH;
483+
msglen -= DTLS_HS_LENGTH;
489484

490-
e += dtls_uint8_to_int(msg + DTLS_HS_LENGTH + e) + sizeof(uint8_t);
485+
client_hello_msg = msg;
491486

492-
if (e + DTLS_HS_LENGTH > msglen)
493-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
487+
msg += DTLS_CH_LENGTH;
488+
msglen -= DTLS_CH_LENGTH;
494489

495-
dtls_hmac_update(&hmac_context, msg + DTLS_HS_LENGTH, e);
490+
/* skip the session_id to include it */
491+
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
492+
"create_cookie, session_id");
496493

497-
if (e + DTLS_HS_LENGTH + sizeof(uint8_t) > msglen)
498-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
499-
/* skip cookie bytes and length byte */
500-
e += dtls_uint8_to_int(msg + DTLS_HS_LENGTH + e);
501-
e += sizeof(uint8_t);
494+
/* feed in the beginning of the Client Hello up to and including the
495+
session id */
496+
dtls_hmac_update(&hmac_context, client_hello_msg, msg - client_hello_msg);
502497

503-
/* read fragment length and check for consistency */
504-
fragment_length = dtls_get_fragment_length(DTLS_HANDSHAKE_HEADER(msg));
505-
if ((fragment_length < e) || (e + DTLS_HS_LENGTH) > msglen)
506-
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
498+
/* skip the cookie to exclude it */
499+
SKIP_VAR_FIELD(msg, msglen, uint8, DTLS_ALERT_HANDSHAKE_FAILURE,
500+
"create_cookie, cookie");
507501

508-
dtls_hmac_update(&hmac_context,
509-
msg + DTLS_HS_LENGTH + e,
510-
fragment_length - e);
502+
/* feed the rest of the Client Hello */
503+
dtls_hmac_update(&hmac_context, msg, msglen);
511504

512505
len = dtls_hmac_finalize(&hmac_context, buf);
513506

514507
if (len < *clen) {
508+
/* fill up with 0s*/
515509
memset(cookie + len, 0, *clen - len);
516510
*clen = len;
517511
}

0 commit comments

Comments
 (0)