| 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. | 
|  | 62 | * | 
|  | 63 | * Note: the dependency is actually on TLS 1.0-1.2 and (AES or ARIA or | 
|  | 64 | * Camellia or DES), but since the test framework doesn't support | 
|  | 65 | * alternation in dependencies, just depend on the most common. | 
|  | 66 | */ | 
|  | 67 | mbedtls_md_context_t ctx, ref_ctx; | 
|  | 68 | const mbedtls_md_info_t *md_info; | 
|  | 69 | size_t out_len, block_size; | 
|  | 70 | size_t min_in_len, in_len, max_in_len, i; | 
|  | 71 | /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ | 
|  | 72 | unsigned char add_data[13]; | 
|  | 73 | unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; | 
|  | 74 | unsigned char *data = NULL; | 
|  | 75 | unsigned char *out = NULL; | 
|  | 76 | unsigned char rec_num = 0; | 
|  | 77 |  | 
|  | 78 | mbedtls_md_init( &ctx ); | 
|  | 79 | mbedtls_md_init( &ref_ctx ); | 
|  | 80 |  | 
|  | 81 | md_info = mbedtls_md_info_from_type( hash ); | 
|  | 82 | TEST_ASSERT( md_info != NULL ); | 
|  | 83 | out_len = mbedtls_md_get_size( md_info ); | 
|  | 84 | TEST_ASSERT( out_len != 0 ); | 
|  | 85 | block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; | 
|  | 86 |  | 
|  | 87 | /* Use allocated out buffer to catch overwrites */ | 
|  | 88 | out = mbedtls_calloc( 1, out_len ); | 
|  | 89 | TEST_ASSERT( out != NULL ); | 
|  | 90 |  | 
|  | 91 | /* Set up contexts with the given hash and a dummy key */ | 
|  | 92 | TEST_ASSERT( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) ); | 
|  | 93 | TEST_ASSERT( 0 == mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); | 
|  | 94 | memset( ref_out, 42, sizeof( ref_out ) ); | 
|  | 95 | TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); | 
|  | 96 | TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); | 
|  | 97 | memset( ref_out, 0, sizeof( ref_out ) ); | 
|  | 98 |  | 
|  | 99 | /* | 
|  | 100 | * Test all possible lengths up to a point. The difference between | 
|  | 101 | * max_in_len and min_in_len is at most 255, and make sure they both vary | 
|  | 102 | * by at least one block size. | 
|  | 103 | */ | 
|  | 104 | for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) | 
|  | 105 | { | 
|  | 106 | /* Use allocated in buffer to catch overreads */ | 
|  | 107 | data = mbedtls_calloc( 1, max_in_len ); | 
|  | 108 | TEST_ASSERT( data != NULL || max_in_len == 0 ); | 
|  | 109 |  | 
|  | 110 | min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; | 
|  | 111 | for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) | 
|  | 112 | { | 
|  | 113 | /* Set up dummy data and add_data */ | 
|  | 114 | rec_num++; | 
|  | 115 | memset( add_data, rec_num, sizeof( add_data ) ); | 
|  | 116 | for( i = 0; i < in_len; i++ ) | 
|  | 117 | data[i] = ( i & 0xff ) ^ rec_num; | 
|  | 118 |  | 
|  | 119 | /* Get the function's result */ | 
| Manuel Pégourié-Gonnard | 0dab12e | 2020-07-28 11:02:57 +0200 | [diff] [blame] | 120 | TEST_CF_SECRET( &in_len, sizeof( in_len ) ); | 
| Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 121 | TEST_ASSERT( 0 == mbedtls_ssl_cf_hmac( &ctx, add_data, sizeof( add_data ), | 
|  | 122 | data, in_len, | 
|  | 123 | min_in_len, max_in_len, | 
|  | 124 | out ) ); | 
| Manuel Pégourié-Gonnard | 0dab12e | 2020-07-28 11:02:57 +0200 | [diff] [blame] | 125 | TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); | 
|  | 126 | TEST_CF_PUBLIC( out, out_len ); | 
| Manuel Pégourié-Gonnard | fde7505 | 2020-07-28 10:19:45 +0200 | [diff] [blame] | 127 |  | 
|  | 128 | /* Compute the reference result */ | 
|  | 129 | TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, add_data, | 
|  | 130 | sizeof( add_data ) ) ); | 
|  | 131 | TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); | 
|  | 132 | TEST_ASSERT( 0 == mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); | 
|  | 133 | TEST_ASSERT( 0 == mbedtls_md_hmac_reset( &ref_ctx ) ); | 
|  | 134 |  | 
|  | 135 | /* Compare */ | 
|  | 136 | TEST_ASSERT( 0 == memcmp( out, ref_out, out_len ) ); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | mbedtls_free( data ); | 
|  | 140 | data = NULL; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | exit: | 
|  | 144 | mbedtls_md_free( &ref_ctx ); | 
|  | 145 | mbedtls_md_free( &ctx ); | 
|  | 146 |  | 
|  | 147 | mbedtls_free( data ); | 
|  | 148 | mbedtls_free( out ); | 
|  | 149 | } | 
|  | 150 | /* END_CASE */ |