• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include <mbedtls/ssl.h>
3#include <mbedtls/ssl_internal.h>
4/* END_HEADER */
5
6/* BEGIN_DEPENDENCIES
7 * depends_on:MBEDTLS_SSL_TLS_C
8 * END_DEPENDENCIES
9 */
10
11/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
12void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
13{
14    uint32_t len = 0;
15    mbedtls_ssl_context ssl;
16    mbedtls_ssl_config conf;
17
18    mbedtls_ssl_init( &ssl );
19    mbedtls_ssl_config_init( &conf );
20
21    TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
22                 MBEDTLS_SSL_IS_CLIENT,
23                 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
24                 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
25    TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
26
27    /* Read previous record numbers */
28    for( len = 0; len < prevs->len; len += 6 )
29    {
30        memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
31        mbedtls_ssl_dtls_replay_update( &ssl );
32    }
33
34    /* Check new number */
35    memcpy( ssl.in_ctr + 2, new->x, 6 );
36    TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
37
38    mbedtls_ssl_free( &ssl );
39    mbedtls_ssl_config_free( &conf );
40}
41/* END_CASE */
42
43/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
44void 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}
54/* END_CASE */
55
56/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
57void ssl_cf_hmac( int hash )
58{
59    /*
60     * Test the function mbedtls_ssl_cf_hmac() against a reference
61     * implementation.
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 */
116            TEST_CF_SECRET( &in_len, sizeof( in_len ) );
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 ) );
121            TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
122            TEST_CF_PUBLIC( out, out_len );
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
139exit:
140    mbedtls_md_free( &ref_ctx );
141    mbedtls_md_free( &ctx );
142
143    mbedtls_free( data );
144    mbedtls_free( out );
145}
146/* END_CASE */
147
148/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
149void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
150{
151    unsigned char *dst = NULL;
152    unsigned char *src = NULL;
153    size_t src_len = offset_max + len;
154    size_t secret;
155
156    dst = mbedtls_calloc( 1, len );
157    TEST_ASSERT( dst != NULL );
158    src = mbedtls_calloc( 1, src_len );
159    TEST_ASSERT( src != NULL );
160
161    /* Fill src in a way that we can detect if we copied the right bytes */
162    rnd_std_rand( NULL, src, src_len );
163
164    for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
165    {
166        TEST_CF_SECRET( &secret, sizeof( secret ) );
167        mbedtls_ssl_cf_memcpy_offset( dst, src, secret,
168                                      offset_min, offset_max, len );
169        TEST_CF_PUBLIC( &secret, sizeof( secret ) );
170        TEST_CF_PUBLIC( dst, len );
171
172        TEST_ASSERT( memcmp( dst, src + secret, len ) == 0 );
173    }
174
175exit:
176    mbedtls_free( dst );
177    mbedtls_free( src );
178}
179/* END_CASE */
180