blob: c21b7287e031dbb4f3d08657071aa23e4e9c0f1f [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003#include <mbedtls/ssl_internal.h>
Hanno Beckera18d1322018-01-03 14:27:32 +00004
5/*
6 * Helper function setting up inverse record transformations
7 * using given cipher, hash, EtM mode, authentication tag length,
8 * and version.
9 */
10
11#define CHK( x ) \
12 do \
13 { \
14 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +000015 { \
16 goto cleanup; \
17 } \
Hanno Beckera18d1322018-01-03 14:27:32 +000018 } while( 0 )
19
20
21static int build_transforms( mbedtls_ssl_transform *t_in,
22 mbedtls_ssl_transform *t_out,
23 int cipher_type, int hash_id,
24 int etm, int tag_mode, int ver )
25{
26 mbedtls_cipher_info_t const *cipher_info;
27
28 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +000029 unsigned char *key0 = NULL, *key1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +000030 unsigned char iv_enc[16], iv_dec[16];
31
32 maclen = 0;
33
34 /* Pick cipher */
35 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
36 CHK( cipher_info != NULL );
37 CHK( cipher_info->iv_size <= 16 );
38 CHK( cipher_info->key_bitlen % 8 == 0 );
39
40 /* Pick keys */
41 keylen = cipher_info->key_bitlen / 8;
42 CHK( ( key0 = malloc( keylen ) ) != NULL );
43 CHK( ( key1 = malloc( keylen ) ) != NULL );
44 memset( key0, 0x1, keylen );
45 memset( key1, 0x2, keylen );
46
47 /* Setup cipher contexts */
48 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
49 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
50 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
51 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
52
53#if defined(MBEDTLS_CIPHER_MODE_CBC)
54 if( cipher_info->mode == MBEDTLS_MODE_CBC )
55 {
56 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
57 MBEDTLS_PADDING_NONE ) == 0 );
58 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
59 MBEDTLS_PADDING_NONE ) == 0 );
60 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
61 MBEDTLS_PADDING_NONE ) == 0 );
62 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
63 MBEDTLS_PADDING_NONE ) == 0 );
64 }
65#endif /* MBEDTLS_CIPHER_MODE_CBC */
66
67 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
68 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
69 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
70 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
71 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
72 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
73 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
74 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +000075
76 /* Setup MAC contexts */
77#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
78 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
79 cipher_info->mode == MBEDTLS_MODE_STREAM )
80 {
81 mbedtls_md_info_t const *md_info;
82 unsigned char *md0, *md1;
83
84 /* Pick hash */
85 md_info = mbedtls_md_info_from_type( hash_id );
86 CHK( md_info != NULL );
87
88 /* Pick hash keys */
89 maclen = mbedtls_md_get_size( md_info );
90 CHK( ( md0 = malloc( maclen ) ) != NULL );
91 CHK( ( md1 = malloc( maclen ) ) != NULL );
92 memset( md0, 0x5, maclen );
93 memset( md1, 0x6, maclen );
94
95 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
96 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
97 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
98 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
99
100 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
101 {
102 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
103 md0, maclen ) == 0 );
104 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
105 md1, maclen ) == 0 );
106 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
107 md1, maclen ) == 0 );
108 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
109 md0, maclen ) == 0 );
110 }
111#if defined(MBEDTLS_SSL_PROTO_SSL3)
112 else
113 {
114 memcpy( &t_in->mac_enc, md0, maclen );
115 memcpy( &t_in->mac_dec, md1, maclen );
116 memcpy( &t_out->mac_enc, md1, maclen );
117 memcpy( &t_out->mac_dec, md0, maclen );
118 }
119#endif
120
121 free( md0 );
122 free( md1 );
123 }
124#else
125 ((void) hash_id);
126#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
127
128
129 /* Pick IV's (regardless of whether they
130 * are being used by the transform). */
131 ivlen = cipher_info->iv_size;
132 memset( iv_enc, 0x3, sizeof( iv_enc ) );
133 memset( iv_dec, 0x4, sizeof( iv_dec ) );
134
135 /*
136 * Setup transforms
137 */
138
139#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
140 t_out->encrypt_then_mac = etm;
141 t_in->encrypt_then_mac = etm;
142#else
143 ((void) etm);
144#endif
145
146 t_out->minor_ver = ver;
147 t_in->minor_ver = ver;
148 t_out->ivlen = ivlen;
149 t_in->ivlen = ivlen;
150
151 switch( cipher_info->mode )
152 {
153 case MBEDTLS_MODE_GCM:
154 case MBEDTLS_MODE_CCM:
155 t_out->fixed_ivlen = 4;
156 t_in->fixed_ivlen = 4;
157 t_out->maclen = 0;
158 t_in->maclen = 0;
159 switch( tag_mode )
160 {
161 case 0: /* Full tag */
162 t_out->taglen = 16;
163 t_in->taglen = 16;
164 break;
165 case 1: /* Partial tag */
166 t_out->taglen = 8;
167 t_in->taglen = 8;
168 break;
169 default:
170 return( 1 );
171 }
172 break;
173
174 case MBEDTLS_MODE_CHACHAPOLY:
175 t_out->fixed_ivlen = 12;
176 t_in->fixed_ivlen = 12;
177 t_out->maclen = 0;
178 t_in->maclen = 0;
179 switch( tag_mode )
180 {
181 case 0: /* Full tag */
182 t_out->taglen = 16;
183 t_in->taglen = 16;
184 break;
185 case 1: /* Partial tag */
186 t_out->taglen = 8;
187 t_in->taglen = 8;
188 break;
189 default:
190 return( 1 );
191 }
192 break;
193
194 case MBEDTLS_MODE_STREAM:
195 case MBEDTLS_MODE_CBC:
196 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
197 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
198 t_out->taglen = 0;
199 t_in->taglen = 0;
200 switch( tag_mode )
201 {
202 case 0: /* Full tag */
203 t_out->maclen = maclen;
204 t_in->maclen = maclen;
205 break;
206 case 1: /* Partial tag */
207 t_out->maclen = 10;
208 t_in->maclen = 10;
209 break;
210 default:
211 return( 1 );
212 }
213 break;
214 default:
215 return( 1 );
216 break;
217 }
218
219 /* Setup IV's */
220
221 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
222 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
223 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
224 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
225
Hanno Becker81e16a32019-03-01 11:21:44 +0000226cleanup:
227
228 free( key0 );
229 free( key1 );
230
Hanno Beckera18d1322018-01-03 14:27:32 +0000231 return( 0 );
232}
233
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200234/* END_HEADER */
235
236/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200238 * END_DEPENDENCIES
239 */
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100242void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200243{
Azim Khand30ca132017-06-09 04:32:58 +0100244 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200246 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200247
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200248 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200249 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200250
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200251 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
252 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200253 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
254 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200255 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200256
257 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100258 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200259 {
Azim Khand30ca132017-06-09 04:32:58 +0100260 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200262 }
263
264 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100265 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200269 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200270}
271/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100272
273/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
274void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
275{
276 mbedtls_ssl_context ssl;
277 mbedtls_ssl_init( &ssl );
278
279 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
280 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
281
282 mbedtls_ssl_free( &ssl );
283}
Darryl Green11999bb2018-03-13 15:22:58 +0000284/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000285
286/* BEGIN_CASE */
287void ssl_crypt_record( int cipher_type, int hash_id,
288 int etm, int tag_mode, int ver )
289{
290 /*
291 * Test several record encryptions and decryptions
292 * with plenty of space before and after the data
293 * within the record buffer.
294 */
295
296 int ret;
297 int num_records = 16;
298 mbedtls_ssl_context ssl; /* ONLY for debugging */
299
300 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000301 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +0000302 size_t const buflen = 512;
303 mbedtls_record rec, rec_backup;
304
305 mbedtls_ssl_init( &ssl );
306 mbedtls_ssl_transform_init( &t0 );
307 mbedtls_ssl_transform_init( &t1 );
308 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
309 etm, tag_mode, ver ) == 0 );
310
311 TEST_ASSERT( ( buf = malloc( buflen ) ) != NULL );
312
313 while( num_records-- > 0 )
314 {
315 mbedtls_ssl_transform *t_dec, *t_enc;
316 /* Take turns in who's sending and who's receiving. */
317 if( num_records % 3 == 0 )
318 {
319 t_dec = &t0;
320 t_enc = &t1;
321 }
322 else
323 {
324 t_dec = &t1;
325 t_enc = &t0;
326 }
327
328 /*
329 * The record header affects the transformation in two ways:
330 * 1) It determines the AEAD additional data
331 * 2) The record counter sometimes determines the IV.
332 *
333 * Apart from that, the fields don't have influence.
334 * In particular, it is currently not the responsibility
335 * of ssl_encrypt/decrypt_buf to check if the transform
336 * version matches the record version, or that the
337 * type is sensible.
338 */
339
340 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
341 rec.type = 42;
342 rec.ver[0] = num_records;
343 rec.ver[1] = num_records;
344
345 rec.buf = buf;
346 rec.buf_len = buflen;
347 rec.data_offset = 16;
348 /* Make sure to vary the length to exercise different
349 * paddings. */
350 rec.data_len = 1 + num_records;
351
352 memset( rec.buf + rec.data_offset, 42, rec.data_len );
353
354 /* Make a copy for later comparison */
355 rec_backup = rec;
356
357 /* Encrypt record */
358 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
359 rnd_std_rand, NULL );
360 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
361 if( ret != 0 )
362 {
363 continue;
364 }
365
366 /* Decrypt record with t_dec */
367 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
368
369 /* Compare results */
370 TEST_ASSERT( rec.type == rec_backup.type );
371 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
372 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
373 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
374 TEST_ASSERT( rec.data_len == rec_backup.data_len );
375 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
376 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
377 rec_backup.buf + rec_backup.data_offset,
378 rec.data_len ) == 0 );
379 }
380
Hanno Becker81e16a32019-03-01 11:21:44 +0000381exit:
382
Hanno Beckera18d1322018-01-03 14:27:32 +0000383 /* Cleanup */
384 mbedtls_ssl_free( &ssl );
385 mbedtls_ssl_transform_free( &t0 );
386 mbedtls_ssl_transform_free( &t1 );
387
388 free( buf );
389}
390/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000391
392/* BEGIN_CASE */
393void ssl_crypt_record_small( int cipher_type, int hash_id,
394 int etm, int tag_mode, int ver )
395{
396 /*
397 * Test pairs of encryption and decryption with an increasing
398 * amount of space in the record buffer - in more detail:
399 * 1) Try to encrypt with 0, 1, 2, ... bytes available
400 * in front of the plaintext, and expect the encryption
401 * to succeed starting from some offset. Always keep
402 * enough space in the end of the buffer.
403 * 2) Try to encrypt with 0, 1, 2, ... bytes available
404 * at the end of the plaintext, and expect the encryption
405 * to succeed starting from some offset. Always keep
406 * enough space at the beginning of the buffer.
407 * 3) Try to encrypt with 0, 1, 2, ... bytes available
408 * both at the front and end of the plaintext,
409 * and expect the encryption to succeed starting from
410 * some offset.
411 *
412 * If encryption succeeds, check that decryption succeeds
413 * and yields the original record.
414 */
415
416 mbedtls_ssl_context ssl; /* ONLY for debugging */
417
418 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000419 unsigned char *buf = NULL;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000420 size_t const buflen = 150;
421 mbedtls_record rec, rec_backup;
422
423 int ret;
424 int mode; /* Mode 1, 2 or 3 as explained above */
425 size_t offset; /* Available space at beginning/end/both */
426 size_t threshold = 64; /* Maximum offset to test against */
427
428 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
429 size_t default_post_padding = 64; /* Post-padding to use in mode 1 */
430
431 int seen_success; /* Indicates if in the current mode we've
432 * already seen a successful test. */
433
434 mbedtls_ssl_init( &ssl );
435 mbedtls_ssl_transform_init( &t0 );
436 mbedtls_ssl_transform_init( &t1 );
437 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
438 etm, tag_mode, ver ) == 0 );
439
440 TEST_ASSERT( ( buf = malloc( buflen ) ) != NULL );
441
442 for( mode=1; mode <= 3; mode++ )
443 {
444 seen_success = 0;
445 for( offset=0; offset <= threshold; offset++ )
446 {
447 mbedtls_ssl_transform *t_dec, *t_enc;
448 /* Take turns in who's sending and who's receiving. */
449 if( offset % 3 == 0 )
450 {
451 t_dec = &t0;
452 t_enc = &t1;
453 }
454 else
455 {
456 t_dec = &t1;
457 t_enc = &t0;
458 }
459
460 memset( rec.ctr, offset, sizeof( rec.ctr ) );
461 rec.type = 42;
462 rec.ver[0] = offset;
463 rec.ver[1] = offset;
464 rec.buf = buf;
465 rec.buf_len = buflen;
466
467 switch( mode )
468 {
469 case 1: /* Space in the beginning */
470 rec.data_offset = offset;
471 rec.data_len = buflen - offset - default_post_padding;
472 break;
473
474 case 2: /* Space in the end */
475 rec.data_offset = default_pre_padding;
476 rec.data_len = buflen - default_pre_padding - offset;
477 break;
478
479 case 3: /* Space in the beginning and end */
480 rec.data_offset = offset;
481 rec.data_len = buflen - 2 * offset;
482 break;
483
484 default:
485 TEST_ASSERT( 0 );
486 break;
487 }
488
489 memset( rec.buf + rec.data_offset, 42, rec.data_len );
490
491 /* Make a copy for later comparison */
492 rec_backup = rec;
493
494 /* Encrypt record */
495 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
496
497 if( ( mode == 1 || mode == 2 ) && seen_success )
498 {
499 TEST_ASSERT( ret == 0 );
500 }
501 else
502 {
503 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
504 if( ret == 0 )
505 seen_success = 1;
506 }
507
508 if( ret != 0 )
509 continue;
510
511 /* Decrypt record with t_dec */
512 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
513
514 /* Compare results */
515 TEST_ASSERT( rec.type == rec_backup.type );
516 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
517 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
518 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
519 TEST_ASSERT( rec.data_len == rec_backup.data_len );
520 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
521 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
522 rec_backup.buf + rec_backup.data_offset,
523 rec.data_len ) == 0 );
524 }
525
526 TEST_ASSERT( seen_success == 1 );
527 }
528
Hanno Becker81e16a32019-03-01 11:21:44 +0000529exit:
530
Hanno Beckerb3268da2018-01-05 15:20:24 +0000531 /* Cleanup */
532 mbedtls_ssl_free( &ssl );
533 mbedtls_ssl_transform_free( &t0 );
534 mbedtls_ssl_transform_free( &t1 );
535
536 free( buf );
537}
538/* END_CASE */