Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include <mbedtls/ssl.h> |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 3 | #include <mbedtls/ssl_internal.h> |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 4 | /* END_HEADER */ |
| 5 | |
| 6 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 8 | * END_DEPENDENCIES |
| 9 | */ |
| 10 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 12 | void ssl_dtls_replay( data_t * prevs, data_t * new, int ret ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 13 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 14 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 16 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 18 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 19 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 20 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 21 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 22 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 23 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 24 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 25 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 26 | |
| 27 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 28 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 29 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 30 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 35 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 39 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 40 | } |
| 41 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 42 | |
| 43 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 44 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 45 | { |
| 46 | mbedtls_ssl_context ssl; |
| 47 | mbedtls_ssl_init( &ssl ); |
| 48 | |
| 49 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 50 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 51 | |
| 52 | mbedtls_ssl_free( &ssl ); |
| 53 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 54 | /* END_CASE */ |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 1e94128 | 2020-07-28 11:35:39 +0200 | [diff] [blame] | 56 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 57 | void ssl_cf_hmac( int hash ) |
| 58 | { |
| 59 | /* |
| 60 | * Test the function mbedtls_ssl_cf_hmac() against a reference |
| 61 | * implementation. |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 62 | */ |
| 63 | mbedtls_md_context_t ctx, ref_ctx; |
| 64 | const mbedtls_md_info_t *md_info; |
| 65 | size_t out_len, block_size; |
| 66 | size_t min_in_len, in_len, max_in_len, i; |
| 67 | /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ |
| 68 | unsigned char add_data[13]; |
| 69 | unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; |
| 70 | unsigned char *data = NULL; |
| 71 | unsigned char *out = NULL; |
| 72 | unsigned char rec_num = 0; |
| 73 | |
| 74 | mbedtls_md_init( &ctx ); |
| 75 | mbedtls_md_init( &ref_ctx ); |
| 76 | |
| 77 | md_info = mbedtls_md_info_from_type( hash ); |
| 78 | TEST_ASSERT( md_info != NULL ); |
| 79 | out_len = mbedtls_md_get_size( md_info ); |
| 80 | TEST_ASSERT( out_len != 0 ); |
| 81 | block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; |
| 82 | |
| 83 | /* Use allocated out buffer to catch overwrites */ |
| 84 | out = mbedtls_calloc( 1, out_len ); |
| 85 | TEST_ASSERT( out != NULL ); |
| 86 | |
| 87 | /* Set up contexts with the given hash and a dummy key */ |
| 88 | TEST_ASSERT( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) ); |
| 89 | TEST_ASSERT( 0 == mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); |
| 90 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 91 | TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); |
| 92 | TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); |
| 93 | memset( ref_out, 0, sizeof( ref_out ) ); |
| 94 | |
| 95 | /* |
| 96 | * Test all possible lengths up to a point. The difference between |
| 97 | * max_in_len and min_in_len is at most 255, and make sure they both vary |
| 98 | * by at least one block size. |
| 99 | */ |
| 100 | for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) |
| 101 | { |
| 102 | /* Use allocated in buffer to catch overreads */ |
| 103 | data = mbedtls_calloc( 1, max_in_len ); |
| 104 | TEST_ASSERT( data != NULL || max_in_len == 0 ); |
| 105 | |
| 106 | min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; |
| 107 | for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) |
| 108 | { |
| 109 | /* Set up dummy data and add_data */ |
| 110 | rec_num++; |
| 111 | memset( add_data, rec_num, sizeof( add_data ) ); |
| 112 | for( i = 0; i < in_len; i++ ) |
| 113 | data[i] = ( i & 0xff ) ^ rec_num; |
| 114 | |
| 115 | /* Get the function's result */ |
Manuel Pégourié-Gonnard | 0dab12e | 2020-07-28 11:02:57 +0200 | [diff] [blame] | 116 | TEST_CF_SECRET( &in_len, sizeof( in_len ) ); |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 117 | TEST_ASSERT( 0 == mbedtls_ssl_cf_hmac( &ctx, add_data, sizeof( add_data ), |
| 118 | data, in_len, |
| 119 | min_in_len, max_in_len, |
| 120 | out ) ); |
Manuel Pégourié-Gonnard | 0dab12e | 2020-07-28 11:02:57 +0200 | [diff] [blame] | 121 | TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); |
| 122 | TEST_CF_PUBLIC( out, out_len ); |
Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 123 | |
| 124 | /* Compute the reference result */ |
| 125 | TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, add_data, |
| 126 | sizeof( add_data ) ) ); |
| 127 | TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); |
| 128 | TEST_ASSERT( 0 == mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); |
| 129 | TEST_ASSERT( 0 == mbedtls_md_hmac_reset( &ref_ctx ) ); |
| 130 | |
| 131 | /* Compare */ |
| 132 | TEST_ASSERT( 0 == memcmp( out, ref_out, out_len ) ); |
| 133 | } |
| 134 | |
| 135 | mbedtls_free( data ); |
| 136 | data = NULL; |
| 137 | } |
| 138 | |
| 139 | exit: |
| 140 | mbedtls_md_free( &ref_ctx ); |
| 141 | mbedtls_md_free( &ctx ); |
| 142 | |
| 143 | mbedtls_free( data ); |
| 144 | mbedtls_free( out ); |
| 145 | } |
| 146 | /* END_CASE */ |