@@ -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 ) \
@@ -441,11 +440,11 @@ dtls_get_cookie(uint8 *msg, size_t msglen, uint8 **cookie) {
441440
442441static int
443442dtls_create_cookie (dtls_context_t * ctx ,
444- session_t * session ,
445- uint8 * msg , size_t msglen ,
446- uint8 * cookie , int * clen ) {
443+ session_t * session ,
444+ uint8 * msg , size_t msglen ,
445+ uint8 * cookie , int * clen ) {
447446 unsigned char buf [DTLS_HMAC_MAX ];
448- size_t e , fragment_length ;
447+ uint8 * client_hello_msg ;
449448 int len ;
450449
451450 /* create cookie with HMAC-SHA256 over:
@@ -463,40 +462,35 @@ dtls_create_cookie(dtls_context_t *ctx,
463462 dtls_hmac_context_t hmac_context ;
464463 dtls_hmac_init (& hmac_context , ctx -> cookie_secret , DTLS_COOKIE_SECRET_LENGTH );
465464
466- dtls_hmac_update (& hmac_context ,
467- (unsigned char * )& session -> addr , session -> size );
465+ dtls_hmac_update (& hmac_context , (uint8 * )& session -> addr , session -> size );
468466
469- /* feed in the beginning of the Client Hello up to and including the
470- session id */
471- e = DTLS_CH_LENGTH ;
472- if (e + DTLS_HS_LENGTH + sizeof (uint8_t ) > msglen )
473- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
467+ msg += DTLS_HS_LENGTH ;
468+ msglen -= DTLS_HS_LENGTH ;
474469
475- e += dtls_uint8_to_int ( msg + DTLS_HS_LENGTH + e ) + sizeof ( uint8_t ) ;
470+ client_hello_msg = msg ;
476471
477- if ( e + DTLS_HS_LENGTH > msglen )
478- return dtls_alert_fatal_create ( DTLS_ALERT_HANDSHAKE_FAILURE ) ;
472+ msg += DTLS_CH_LENGTH ;
473+ msglen -= DTLS_CH_LENGTH ;
479474
480- dtls_hmac_update (& hmac_context , msg + DTLS_HS_LENGTH , e );
475+ /* skip the session_id to include it */
476+ SKIP_VAR_FIELD (msg , msglen , uint8 , DTLS_ALERT_HANDSHAKE_FAILURE ,
477+ "create_cookie, session_id" );
481478
482- if (e + DTLS_HS_LENGTH + sizeof (uint8_t ) > msglen )
483- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
484- /* skip cookie bytes and length byte */
485- e += dtls_uint8_to_int (msg + DTLS_HS_LENGTH + e );
486- e += sizeof (uint8_t );
479+ /* feed in the beginning of the Client Hello up to and including the
480+ session id */
481+ dtls_hmac_update (& hmac_context , client_hello_msg , msg - client_hello_msg );
487482
488- /* read fragment length and check for consistency */
489- fragment_length = dtls_get_fragment_length (DTLS_HANDSHAKE_HEADER (msg ));
490- if ((fragment_length < e ) || (e + DTLS_HS_LENGTH ) > msglen )
491- return dtls_alert_fatal_create (DTLS_ALERT_HANDSHAKE_FAILURE );
483+ /* skip the cookie to exclude it */
484+ SKIP_VAR_FIELD (msg , msglen , uint8 , DTLS_ALERT_HANDSHAKE_FAILURE ,
485+ "create_cookie, cookie" );
492486
493- dtls_hmac_update (& hmac_context ,
494- msg + DTLS_HS_LENGTH + e ,
495- fragment_length - e );
487+ /* feed the rest of the Client Hello */
488+ dtls_hmac_update (& hmac_context , msg , msglen );
496489
497490 len = dtls_hmac_finalize (& hmac_context , buf );
498491
499492 if (len < * clen ) {
493+ /* fill up with 0s*/
500494 memset (cookie + len , 0 , * clen - len );
501495 * clen = len ;
502496 }
0 commit comments