2 #include <mbedtls/ssl.h>
3 #include <mbedtls/ssl_internal.h>
6 * Helper function setting up inverse record transformations
7 * using given cipher, hash, EtM mode, authentication tag length,
21 #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
22 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
24 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
27 static int build_transforms( mbedtls_ssl_transform *t_in,
28 mbedtls_ssl_transform *t_out,
29 int cipher_type, int hash_id,
30 int etm, int tag_mode, int ver,
34 mbedtls_cipher_info_t const *cipher_info;
37 size_t keylen, maclen, ivlen;
38 unsigned char *key0 = NULL, *key1 = NULL;
39 unsigned char iv_enc[16], iv_dec[16];
41 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
42 unsigned char cid0[ SSL_CID_LEN_MIN ];
43 unsigned char cid1[ SSL_CID_LEN_MIN ];
45 rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
46 rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
50 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
55 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
56 CHK( cipher_info != NULL );
57 CHK( cipher_info->iv_size <= 16 );
58 CHK( cipher_info->key_bitlen % 8 == 0 );
61 keylen = cipher_info->key_bitlen / 8;
62 /* Allocate `keylen + 1` bytes to ensure that we get
63 * a non-NULL pointers from `mbedtls_calloc` even if
64 * `keylen == 0` in the case of the NULL cipher. */
65 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
66 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
67 memset( key0, 0x1, keylen );
68 memset( key1, 0x2, keylen );
70 /* Setup cipher contexts */
71 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
72 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
73 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
74 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
76 #if defined(MBEDTLS_CIPHER_MODE_CBC)
77 if( cipher_info->mode == MBEDTLS_MODE_CBC )
79 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
80 MBEDTLS_PADDING_NONE ) == 0 );
81 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
82 MBEDTLS_PADDING_NONE ) == 0 );
83 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
84 MBEDTLS_PADDING_NONE ) == 0 );
85 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
86 MBEDTLS_PADDING_NONE ) == 0 );
88 #endif /* MBEDTLS_CIPHER_MODE_CBC */
90 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
91 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
92 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
93 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
94 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
95 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
96 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
97 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
99 /* Setup MAC contexts */
100 #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
101 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
102 cipher_info->mode == MBEDTLS_MODE_STREAM )
104 mbedtls_md_info_t const *md_info;
105 unsigned char *md0, *md1;
108 md_info = mbedtls_md_info_from_type( hash_id );
109 CHK( md_info != NULL );
112 maclen = mbedtls_md_get_size( md_info );
113 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
114 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
115 memset( md0, 0x5, maclen );
116 memset( md1, 0x6, maclen );
118 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
119 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
120 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
121 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
123 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
125 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
126 md0, maclen ) == 0 );
127 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
128 md1, maclen ) == 0 );
129 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
130 md1, maclen ) == 0 );
131 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
132 md0, maclen ) == 0 );
134 #if defined(MBEDTLS_SSL_PROTO_SSL3)
137 memcpy( &t_in->mac_enc, md0, maclen );
138 memcpy( &t_in->mac_dec, md1, maclen );
139 memcpy( &t_out->mac_enc, md1, maclen );
140 memcpy( &t_out->mac_dec, md0, maclen );
149 #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
152 /* Pick IV's (regardless of whether they
153 * are being used by the transform). */
154 ivlen = cipher_info->iv_size;
155 memset( iv_enc, 0x3, sizeof( iv_enc ) );
156 memset( iv_dec, 0x4, sizeof( iv_dec ) );
162 #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
163 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
164 t_out->encrypt_then_mac = etm;
165 t_in->encrypt_then_mac = etm;
170 t_out->minor_ver = ver;
171 t_in->minor_ver = ver;
172 t_out->ivlen = ivlen;
175 switch( cipher_info->mode )
177 case MBEDTLS_MODE_GCM:
178 case MBEDTLS_MODE_CCM:
179 t_out->fixed_ivlen = 4;
180 t_in->fixed_ivlen = 4;
185 case 0: /* Full tag */
189 case 1: /* Partial tag */
198 case MBEDTLS_MODE_CHACHAPOLY:
199 t_out->fixed_ivlen = 12;
200 t_in->fixed_ivlen = 12;
205 case 0: /* Full tag */
209 case 1: /* Partial tag */
218 case MBEDTLS_MODE_STREAM:
219 case MBEDTLS_MODE_CBC:
220 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
221 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
226 case 0: /* Full tag */
227 t_out->maclen = maclen;
228 t_in->maclen = maclen;
230 case 1: /* Partial tag */
245 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
246 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
247 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
248 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
250 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
252 memcpy( &t_in->in_cid, cid0, cid0_len );
253 memcpy( &t_in->out_cid, cid1, cid1_len );
254 t_in->in_cid_len = cid0_len;
255 t_in->out_cid_len = cid1_len;
256 memcpy( &t_out->in_cid, cid1, cid1_len );
257 memcpy( &t_out->out_cid, cid0, cid0_len );
258 t_out->in_cid_len = cid1_len;
259 t_out->out_cid_len = cid0_len;
260 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
264 mbedtls_free( key0 );
265 mbedtls_free( key1 );
272 /* BEGIN_DEPENDENCIES
273 * depends_on:MBEDTLS_SSL_TLS_C
277 /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
278 void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
281 mbedtls_ssl_context ssl;
282 mbedtls_ssl_config conf;
284 mbedtls_ssl_init( &ssl );
285 mbedtls_ssl_config_init( &conf );
287 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
288 MBEDTLS_SSL_IS_CLIENT,
289 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
290 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
291 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
293 /* Read previous record numbers */
294 for( len = 0; len < prevs->len; len += 6 )
296 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
297 mbedtls_ssl_dtls_replay_update( &ssl );
300 /* Check new number */
301 memcpy( ssl.in_ctr + 2, new->x, 6 );
302 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
304 mbedtls_ssl_free( &ssl );
305 mbedtls_ssl_config_free( &conf );
309 /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
310 void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
312 mbedtls_ssl_context ssl;
313 mbedtls_ssl_init( &ssl );
315 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
316 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
318 mbedtls_ssl_free( &ssl );
323 void ssl_crypt_record( int cipher_type, int hash_id,
324 int etm, int tag_mode, int ver,
325 int cid0_len, int cid1_len )
328 * Test several record encryptions and decryptions
329 * with plenty of space before and after the data
330 * within the record buffer.
334 int num_records = 16;
335 mbedtls_ssl_context ssl; /* ONLY for debugging */
337 mbedtls_ssl_transform t0, t1;
338 unsigned char *buf = NULL;
339 size_t const buflen = 512;
340 mbedtls_record rec, rec_backup;
342 mbedtls_ssl_init( &ssl );
343 mbedtls_ssl_transform_init( &t0 );
344 mbedtls_ssl_transform_init( &t1 );
345 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
348 (size_t) cid1_len ) == 0 );
350 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
352 while( num_records-- > 0 )
354 mbedtls_ssl_transform *t_dec, *t_enc;
355 /* Take turns in who's sending and who's receiving. */
356 if( num_records % 3 == 0 )
368 * The record header affects the transformation in two ways:
369 * 1) It determines the AEAD additional data
370 * 2) The record counter sometimes determines the IV.
372 * Apart from that, the fields don't have influence.
373 * In particular, it is currently not the responsibility
374 * of ssl_encrypt/decrypt_buf to check if the transform
375 * version matches the record version, or that the
379 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
381 rec.ver[0] = num_records;
382 rec.ver[1] = num_records;
383 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
385 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
388 rec.buf_len = buflen;
389 rec.data_offset = 16;
390 /* Make sure to vary the length to exercise different
392 rec.data_len = 1 + num_records;
394 memset( rec.buf + rec.data_offset, 42, rec.data_len );
396 /* Make a copy for later comparison */
400 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
401 rnd_std_rand, NULL );
402 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
408 /* Decrypt record with t_dec */
409 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
410 TEST_ASSERT( ret == 0 );
412 /* Compare results */
413 TEST_ASSERT( rec.type == rec_backup.type );
414 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
415 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
416 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
417 TEST_ASSERT( rec.data_len == rec_backup.data_len );
418 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
419 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
420 rec_backup.buf + rec_backup.data_offset,
421 rec.data_len ) == 0 );
427 mbedtls_ssl_free( &ssl );
428 mbedtls_ssl_transform_free( &t0 );
429 mbedtls_ssl_transform_free( &t1 );
436 void ssl_crypt_record_small( int cipher_type, int hash_id,
437 int etm, int tag_mode, int ver,
438 int cid0_len, int cid1_len )
441 * Test pairs of encryption and decryption with an increasing
442 * amount of space in the record buffer - in more detail:
443 * 1) Try to encrypt with 0, 1, 2, ... bytes available
444 * in front of the plaintext, and expect the encryption
445 * to succeed starting from some offset. Always keep
446 * enough space in the end of the buffer.
447 * 2) Try to encrypt with 0, 1, 2, ... bytes available
448 * at the end of the plaintext, and expect the encryption
449 * to succeed starting from some offset. Always keep
450 * enough space at the beginning of the buffer.
451 * 3) Try to encrypt with 0, 1, 2, ... bytes available
452 * both at the front and end of the plaintext,
453 * and expect the encryption to succeed starting from
456 * If encryption succeeds, check that decryption succeeds
457 * and yields the original record.
460 mbedtls_ssl_context ssl; /* ONLY for debugging */
462 mbedtls_ssl_transform t0, t1;
463 unsigned char *buf = NULL;
464 size_t const buflen = 256;
465 mbedtls_record rec, rec_backup;
468 int mode; /* Mode 1, 2 or 3 as explained above */
469 size_t offset; /* Available space at beginning/end/both */
470 size_t threshold = 96; /* Maximum offset to test against */
472 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
473 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
475 int seen_success; /* Indicates if in the current mode we've
476 * already seen a successful test. */
478 mbedtls_ssl_init( &ssl );
479 mbedtls_ssl_transform_init( &t0 );
480 mbedtls_ssl_transform_init( &t1 );
481 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
484 (size_t) cid1_len ) == 0 );
486 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
488 for( mode=1; mode <= 3; mode++ )
491 for( offset=0; offset <= threshold; offset++ )
493 mbedtls_ssl_transform *t_dec, *t_enc;
497 memset( rec.ctr, offset, sizeof( rec.ctr ) );
502 rec.buf_len = buflen;
503 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
505 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
509 case 1: /* Space in the beginning */
510 rec.data_offset = offset;
511 rec.data_len = buflen - offset - default_post_padding;
514 case 2: /* Space in the end */
515 rec.data_offset = default_pre_padding;
516 rec.data_len = buflen - default_pre_padding - offset;
519 case 3: /* Space in the beginning and end */
520 rec.data_offset = offset;
521 rec.data_len = buflen - 2 * offset;
529 memset( rec.buf + rec.data_offset, 42, rec.data_len );
531 /* Make a copy for later comparison */
535 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
537 if( ( mode == 1 || mode == 2 ) && seen_success )
539 TEST_ASSERT( ret == 0 );
543 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
551 /* Decrypt record with t_dec */
552 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
554 /* Compare results */
555 TEST_ASSERT( rec.type == rec_backup.type );
556 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
557 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
558 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
559 TEST_ASSERT( rec.data_len == rec_backup.data_len );
560 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
561 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
562 rec_backup.buf + rec_backup.data_offset,
563 rec.data_len ) == 0 );
566 TEST_ASSERT( seen_success == 1 );
572 mbedtls_ssl_free( &ssl );
573 mbedtls_ssl_transform_free( &t0 );
574 mbedtls_ssl_transform_free( &t1 );
581 void ssl_tls_prf( int type, data_t * secret, data_t * random,
582 char *label, data_t *result_hex_str, int exp_ret )
584 unsigned char *output;
586 output = mbedtls_calloc( 1, result_hex_str->len );
590 #if defined(MBEDTLS_USE_PSA_CRYPTO)
591 TEST_ASSERT( psa_crypto_init() == 0 );
594 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
595 label, random->x, random->len,
596 output, result_hex_str->len ) == exp_ret );
600 TEST_ASSERT( hexcmp( output, result_hex_str->x,
601 result_hex_str->len, result_hex_str->len ) == 0 );
605 mbedtls_free( output );