@@ -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 ) \
@@ -418,11 +417,11 @@ dtls_get_cookie(uint8 *msg, size_t msglen, uint8 **cookie) {
418417
419418static int
420419dtls_create_cookie (dtls_context_t * ctx ,
421- session_t * session ,
422- uint8 * msg , size_t msglen ,
423- uint8 * cookie , int * clen ) {
420+ session_t * session ,
421+ uint8 * msg , size_t msglen ,
422+ uint8 * cookie , int * clen ) {
424423 unsigned char buf [DTLS_HMAC_MAX ];
425- size_t e , fragment_length ;
424+ uint8 * client_hello_msg ;
426425 int len ;
427426
428427 /* create cookie with HMAC-SHA256 over:
@@ -440,40 +439,33 @@ dtls_create_cookie(dtls_context_t *ctx,
440439 dtls_hmac_context_t hmac_context ;
441440 dtls_hmac_init (& hmac_context , ctx -> cookie_secret , DTLS_COOKIE_SECRET_LENGTH );
442441
443- dtls_hmac_update (& hmac_context ,
444- (unsigned char * )& session -> addr , session -> size );
442+ dtls_hmac_update (& hmac_context , (uint8 * )& session -> addr , session -> size );
445443
446- /* feed in the beginning of the Client Hello up to and including the
447- session id */
448- e = DTLS_CH_LENGTH ;
449- if (e + DTLS_HS_LENGTH + sizeof (uint8_t ) > msglen )
450- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
444+ msg += DTLS_HS_LENGTH ;
445+ msglen -= DTLS_HS_LENGTH ;
451446
452- e += dtls_uint8_to_int ( msg + DTLS_HS_LENGTH + e ) + sizeof ( uint8_t ) ;
447+ client_hello_msg = msg ;
453448
454- if ( e + DTLS_HS_LENGTH > msglen )
455- return dtls_alert_fatal_create ( DTLS_ALERT_HANDSHAKE_FAILURE ) ;
449+ msg += DTLS_CH_LENGTH ;
450+ msglen -= DTLS_CH_LENGTH ;
456451
457- dtls_hmac_update (& hmac_context , msg + DTLS_HS_LENGTH , e );
452+ /* skip the session id to include it */
453+ SKIP_VAR_FIELD (msg , msglen , uint8 , DTLS_ALERT_HANDSHAKE_FAILURE );
458454
459- if (e + DTLS_HS_LENGTH + sizeof (uint8_t ) > msglen )
460- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
461- /* skip cookie bytes and length byte */
462- e += dtls_uint8_to_int (msg + DTLS_HS_LENGTH + e );
463- e += sizeof (uint8_t );
455+ /* feed in the beginning of the Client Hello up to and including the
456+ session id */
457+ dtls_hmac_update (& hmac_context , client_hello_msg , msg - client_hello_msg );
464458
465- /* read fragment length and check for consistency */
466- fragment_length = dtls_get_fragment_length (DTLS_HANDSHAKE_HEADER (msg ));
467- if ((fragment_length < e ) || (e + DTLS_HS_LENGTH ) > msglen )
468- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
459+ /* skip the cookie to exclude it */
460+ SKIP_VAR_FIELD (msg , msglen , uint8 , DTLS_ALERT_HANDSHAKE_FAILURE );
469461
470- dtls_hmac_update (& hmac_context ,
471- msg + DTLS_HS_LENGTH + e ,
472- fragment_length - e );
462+ /* feed the rest of the Client Hello */
463+ dtls_hmac_update (& hmac_context , msg , msglen );
473464
474465 len = dtls_hmac_finalize (& hmac_context , buf );
475466
476467 if (len < * clen ) {
468+ /* fill up with 0s*/
477469 memset (cookie + len , 0 , * clen - len );
478470 * clen = len ;
479471 }
0 commit comments