blob: 487d3ce471563b56b75405e384772b360e80fb31 [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 ) ) \
15 return( 1 ); \
16 } while( 0 )
17
18
19static int build_transforms( mbedtls_ssl_transform *t_in,
20 mbedtls_ssl_transform *t_out,
21 int cipher_type, int hash_id,
22 int etm, int tag_mode, int ver )
23{
24 mbedtls_cipher_info_t const *cipher_info;
25
26 size_t keylen, maclen, ivlen;
27 unsigned char *key0, *key1;
28 unsigned char iv_enc[16], iv_dec[16];
29
30 maclen = 0;
31
32 /* Pick cipher */
33 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
34 CHK( cipher_info != NULL );
35 CHK( cipher_info->iv_size <= 16 );
36 CHK( cipher_info->key_bitlen % 8 == 0 );
37
38 /* Pick keys */
39 keylen = cipher_info->key_bitlen / 8;
40 CHK( ( key0 = malloc( keylen ) ) != NULL );
41 CHK( ( key1 = malloc( keylen ) ) != NULL );
42 memset( key0, 0x1, keylen );
43 memset( key1, 0x2, keylen );
44
45 /* Setup cipher contexts */
46 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
47 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
48 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
49 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
50
51#if defined(MBEDTLS_CIPHER_MODE_CBC)
52 if( cipher_info->mode == MBEDTLS_MODE_CBC )
53 {
54 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
55 MBEDTLS_PADDING_NONE ) == 0 );
56 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
57 MBEDTLS_PADDING_NONE ) == 0 );
58 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
59 MBEDTLS_PADDING_NONE ) == 0 );
60 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
61 MBEDTLS_PADDING_NONE ) == 0 );
62 }
63#endif /* MBEDTLS_CIPHER_MODE_CBC */
64
65 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
66 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
67 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
68 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
69 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
70 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
71 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
72 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
73 free( key0 );
74 free( key1 );
75
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
226 return( 0 );
227}
228
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200229/* END_HEADER */
230
231/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200233 * END_DEPENDENCIES
234 */
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100237void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200238{
Azim Khand30ca132017-06-09 04:32:58 +0100239 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200241 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200242
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200243 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200244 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200245
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200246 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
247 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200248 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
249 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200250 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200251
252 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100253 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200254 {
Azim Khand30ca132017-06-09 04:32:58 +0100255 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200257 }
258
259 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100260 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200264 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200265}
266/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100267
268/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
269void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
270{
271 mbedtls_ssl_context ssl;
272 mbedtls_ssl_init( &ssl );
273
274 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
275 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
276
277 mbedtls_ssl_free( &ssl );
278}
Darryl Green11999bb2018-03-13 15:22:58 +0000279/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000280
281/* BEGIN_CASE */
282void ssl_crypt_record( int cipher_type, int hash_id,
283 int etm, int tag_mode, int ver )
284{
285 /*
286 * Test several record encryptions and decryptions
287 * with plenty of space before and after the data
288 * within the record buffer.
289 */
290
291 int ret;
292 int num_records = 16;
293 mbedtls_ssl_context ssl; /* ONLY for debugging */
294
295 mbedtls_ssl_transform t0, t1;
296 unsigned char *buf;
297 size_t const buflen = 512;
298 mbedtls_record rec, rec_backup;
299
300 mbedtls_ssl_init( &ssl );
301 mbedtls_ssl_transform_init( &t0 );
302 mbedtls_ssl_transform_init( &t1 );
303 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
304 etm, tag_mode, ver ) == 0 );
305
306 TEST_ASSERT( ( buf = malloc( buflen ) ) != NULL );
307
308 while( num_records-- > 0 )
309 {
310 mbedtls_ssl_transform *t_dec, *t_enc;
311 /* Take turns in who's sending and who's receiving. */
312 if( num_records % 3 == 0 )
313 {
314 t_dec = &t0;
315 t_enc = &t1;
316 }
317 else
318 {
319 t_dec = &t1;
320 t_enc = &t0;
321 }
322
323 /*
324 * The record header affects the transformation in two ways:
325 * 1) It determines the AEAD additional data
326 * 2) The record counter sometimes determines the IV.
327 *
328 * Apart from that, the fields don't have influence.
329 * In particular, it is currently not the responsibility
330 * of ssl_encrypt/decrypt_buf to check if the transform
331 * version matches the record version, or that the
332 * type is sensible.
333 */
334
335 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
336 rec.type = 42;
337 rec.ver[0] = num_records;
338 rec.ver[1] = num_records;
339
340 rec.buf = buf;
341 rec.buf_len = buflen;
342 rec.data_offset = 16;
343 /* Make sure to vary the length to exercise different
344 * paddings. */
345 rec.data_len = 1 + num_records;
346
347 memset( rec.buf + rec.data_offset, 42, rec.data_len );
348
349 /* Make a copy for later comparison */
350 rec_backup = rec;
351
352 /* Encrypt record */
353 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
354 rnd_std_rand, NULL );
355 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
356 if( ret != 0 )
357 {
358 continue;
359 }
360
361 /* Decrypt record with t_dec */
362 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
363
364 /* Compare results */
365 TEST_ASSERT( rec.type == rec_backup.type );
366 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
367 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
368 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
369 TEST_ASSERT( rec.data_len == rec_backup.data_len );
370 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
371 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
372 rec_backup.buf + rec_backup.data_offset,
373 rec.data_len ) == 0 );
374 }
375
376 /* Cleanup */
377 mbedtls_ssl_free( &ssl );
378 mbedtls_ssl_transform_free( &t0 );
379 mbedtls_ssl_transform_free( &t1 );
380
381 free( buf );
382}
383/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000384
385/* BEGIN_CASE */
386void ssl_crypt_record_small( int cipher_type, int hash_id,
387 int etm, int tag_mode, int ver )
388{
389 /*
390 * Test pairs of encryption and decryption with an increasing
391 * amount of space in the record buffer - in more detail:
392 * 1) Try to encrypt with 0, 1, 2, ... bytes available
393 * in front of the plaintext, and expect the encryption
394 * to succeed starting from some offset. Always keep
395 * enough space in the end of the buffer.
396 * 2) Try to encrypt with 0, 1, 2, ... bytes available
397 * at the end of the plaintext, and expect the encryption
398 * to succeed starting from some offset. Always keep
399 * enough space at the beginning of the buffer.
400 * 3) Try to encrypt with 0, 1, 2, ... bytes available
401 * both at the front and end of the plaintext,
402 * and expect the encryption to succeed starting from
403 * some offset.
404 *
405 * If encryption succeeds, check that decryption succeeds
406 * and yields the original record.
407 */
408
409 mbedtls_ssl_context ssl; /* ONLY for debugging */
410
411 mbedtls_ssl_transform t0, t1;
412 unsigned char *buf;
413 size_t const buflen = 150;
414 mbedtls_record rec, rec_backup;
415
416 int ret;
417 int mode; /* Mode 1, 2 or 3 as explained above */
418 size_t offset; /* Available space at beginning/end/both */
419 size_t threshold = 64; /* Maximum offset to test against */
420
421 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
422 size_t default_post_padding = 64; /* Post-padding to use in mode 1 */
423
424 int seen_success; /* Indicates if in the current mode we've
425 * already seen a successful test. */
426
427 mbedtls_ssl_init( &ssl );
428 mbedtls_ssl_transform_init( &t0 );
429 mbedtls_ssl_transform_init( &t1 );
430 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
431 etm, tag_mode, ver ) == 0 );
432
433 TEST_ASSERT( ( buf = malloc( buflen ) ) != NULL );
434
435 for( mode=1; mode <= 3; mode++ )
436 {
437 seen_success = 0;
438 for( offset=0; offset <= threshold; offset++ )
439 {
440 mbedtls_ssl_transform *t_dec, *t_enc;
441 /* Take turns in who's sending and who's receiving. */
442 if( offset % 3 == 0 )
443 {
444 t_dec = &t0;
445 t_enc = &t1;
446 }
447 else
448 {
449 t_dec = &t1;
450 t_enc = &t0;
451 }
452
453 memset( rec.ctr, offset, sizeof( rec.ctr ) );
454 rec.type = 42;
455 rec.ver[0] = offset;
456 rec.ver[1] = offset;
457 rec.buf = buf;
458 rec.buf_len = buflen;
459
460 switch( mode )
461 {
462 case 1: /* Space in the beginning */
463 rec.data_offset = offset;
464 rec.data_len = buflen - offset - default_post_padding;
465 break;
466
467 case 2: /* Space in the end */
468 rec.data_offset = default_pre_padding;
469 rec.data_len = buflen - default_pre_padding - offset;
470 break;
471
472 case 3: /* Space in the beginning and end */
473 rec.data_offset = offset;
474 rec.data_len = buflen - 2 * offset;
475 break;
476
477 default:
478 TEST_ASSERT( 0 );
479 break;
480 }
481
482 memset( rec.buf + rec.data_offset, 42, rec.data_len );
483
484 /* Make a copy for later comparison */
485 rec_backup = rec;
486
487 /* Encrypt record */
488 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
489
490 if( ( mode == 1 || mode == 2 ) && seen_success )
491 {
492 TEST_ASSERT( ret == 0 );
493 }
494 else
495 {
496 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
497 if( ret == 0 )
498 seen_success = 1;
499 }
500
501 if( ret != 0 )
502 continue;
503
504 /* Decrypt record with t_dec */
505 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
506
507 /* Compare results */
508 TEST_ASSERT( rec.type == rec_backup.type );
509 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
510 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
511 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
512 TEST_ASSERT( rec.data_len == rec_backup.data_len );
513 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
514 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
515 rec_backup.buf + rec_backup.data_offset,
516 rec.data_len ) == 0 );
517 }
518
519 TEST_ASSERT( seen_success == 1 );
520 }
521
522 /* Cleanup */
523 mbedtls_ssl_free( &ssl );
524 mbedtls_ssl_transform_free( &t0 );
525 mbedtls_ssl_transform_free( &t1 );
526
527 free( buf );
528}
529/* END_CASE */