Skip to content

Commit

Permalink
dtls.c: cleanup dtls_create_cookie.
Browse files Browse the repository at this point in the history
Use SKIP_VAR_FIELD and GET_VAR_FIELD.
Remove fragment checks, already done ahead.

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Jul 24, 2023
1 parent 387a5b8 commit 5a44384
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ memarray_t dtlscontext_storage;
#define dtls_get_version(H) dtls_uint16_to_int((H)->version)
#define dtls_get_epoch(H) dtls_uint16_to_int((H)->epoch)
#define dtls_get_sequence_number(H) dtls_uint48_to_ulong((H)->sequence_number)
#define dtls_get_fragment_length(H) dtls_uint24_to_int((H)->fragment_length)

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

static int
dtls_create_cookie(dtls_context_t *ctx,
session_t *session,
uint8 *msg, size_t msglen,
uint8 *cookie, int *clen) {
session_t *session,
uint8 *msg, size_t msglen,
uint8 *cookie, int *clen) {
unsigned char buf[DTLS_HMAC_MAX];
size_t e, fragment_length;
uint8 *client_hello_msg;
int len;

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

dtls_hmac_update(&hmac_context,
(unsigned char *)&session->addr, session->size);
dtls_hmac_update(&hmac_context, (uint8 *)&session->addr, session->size);

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

e += dtls_uint8_to_int(msg + DTLS_HS_LENGTH + e) + sizeof(uint8_t);
client_hello_msg = msg;

if (e + DTLS_HS_LENGTH > msglen)
return dtls_alert_fatal_create(DTLS_ALERT_HANDSHAKE_FAILURE);
msg += DTLS_CH_LENGTH;
msglen -= DTLS_CH_LENGTH;

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

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

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

dtls_hmac_update(&hmac_context,
msg + DTLS_HS_LENGTH + e,
fragment_length - e);
/* feed the rest of the Client Hello */
dtls_hmac_update(&hmac_context, msg, msglen);

len = dtls_hmac_finalize(&hmac_context, buf);

if (len < *clen) {
/* fill up with 0s*/
memset(cookie + len, 0, *clen - len);
*clen = len;
}
Expand Down

0 comments on commit 5a44384

Please sign in to comment.