blob: 2eafb2f9470b9004b232d51e7ccddb230dcc2be6 [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>
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02008 * END_DEPENDENCIES
9 */
10
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020012void ssl_dtls_replay( char *prevs, char *new, int ret )
13{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020015 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020016 char *end_prevs = prevs + strlen( prevs ) + 1;
17
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +020018 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020019 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +020020
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020021 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
22 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020023 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
24 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020025 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020026
27 /* Read previous record numbers */
28 for( ; end_prevs - prevs >= 13; prevs += 13 )
29 {
30 prevs[12] = '\0';
31 unhexify( ssl.in_ctr + 2, prevs );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020033 }
34
35 /* Check new number */
36 unhexify( ssl.in_ctr + 2, new );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +020040 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +020041}
42/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +010043
44/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
45void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
46{
47 mbedtls_ssl_context ssl;
48 mbedtls_ssl_init( &ssl );
49
50 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
51 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
52
53 mbedtls_ssl_free( &ssl );
54}
Simon Butcherbb5e1c32018-06-08 11:14:43 +010055/* END_CASE */
56
Manuel Pégourié-Gonnard3ba2bca2020-07-28 10:19:45 +020057/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */
58void ssl_cf_hmac( int hash )
59{
60 /*
61 * Test the function mbedtls_ssl_cf_hmac() against a reference
62 * implementation.
63 *
64 * Note: the dependency is actually on TLS 1.0-1.2 and (AES or ARIA or
65 * Camellia or DES), but since the test framework doesn't support
66 * alternation in dependencies, just depend on the most common.
67 */
68 mbedtls_md_context_t ctx, ref_ctx;
69 const mbedtls_md_info_t *md_info;
70 size_t out_len, block_size;
71 size_t min_in_len, in_len, max_in_len, i;
72 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
73 unsigned char add_data[13];
74 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
75 unsigned char *data = NULL;
76 unsigned char *out = NULL;
77 unsigned char rec_num = 0;
78
79 mbedtls_md_init( &ctx );
80 mbedtls_md_init( &ref_ctx );
81
82 md_info = mbedtls_md_info_from_type( hash );
83 TEST_ASSERT( md_info != NULL );
84 out_len = mbedtls_md_get_size( md_info );
85 TEST_ASSERT( out_len != 0 );
86 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
87
88 /* Use allocated out buffer to catch overwrites */
89 out = mbedtls_calloc( 1, out_len );
90 TEST_ASSERT( out != NULL );
91
92 /* Set up contexts with the given hash and a dummy key */
93 TEST_ASSERT( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) );
94 TEST_ASSERT( 0 == mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
95 memset( ref_out, 42, sizeof( ref_out ) );
96 TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
97 TEST_ASSERT( 0 == mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
98 memset( ref_out, 0, sizeof( ref_out ) );
99
100 /*
101 * Test all possible lengths up to a point. The difference between
102 * max_in_len and min_in_len is at most 255, and make sure they both vary
103 * by at least one block size.
104 */
105 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
106 {
107 /* Use allocated in buffer to catch overreads */
108 data = mbedtls_calloc( 1, max_in_len );
109 TEST_ASSERT( data != NULL || max_in_len == 0 );
110
111 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
112 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
113 {
114 /* Set up dummy data and add_data */
115 rec_num++;
116 memset( add_data, rec_num, sizeof( add_data ) );
117 for( i = 0; i < in_len; i++ )
118 data[i] = ( i & 0xff ) ^ rec_num;
119
120 /* Get the function's result */
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 ) );
125
126 /* Compute the reference result */
127 TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, add_data,
128 sizeof( add_data ) ) );
129 TEST_ASSERT( 0 == mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
130 TEST_ASSERT( 0 == mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
131 TEST_ASSERT( 0 == mbedtls_md_hmac_reset( &ref_ctx ) );
132
133 /* Compare */
134 TEST_ASSERT( 0 == memcmp( out, ref_out, out_len ) );
135 }
136
137 mbedtls_free( data );
138 data = NULL;
139 }
140
141exit:
142 mbedtls_md_free( &ref_ctx );
143 mbedtls_md_free( &ctx );
144
145 mbedtls_free( data );
146 mbedtls_free( out );
147}
148/* END_CASE */