blob: 1bb9d6162fa21c52ee850996f093d25c33353b79 [file] [log] [blame]
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01001/*
Gilles Peskine0d980b82021-01-05 23:34:27 +01002 * Common code library for SSL test programs.
3 *
4 * In addition to the functions in this file, there is shared source code
5 * that cannot be compiled separately in "ssl_test_common_source.c".
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01006 *
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "ssl_test_lib.h"
24
Gilles Peskinee374b952021-02-03 00:05:19 +010025#if defined(MBEDTLS_TEST_HOOKS)
26#include "test/helpers.h"
27#endif
28
Gilles Peskineab7ce962021-01-05 21:27:53 +010029#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
30
Gilles Peskine504c1a32021-01-05 23:40:14 +010031void my_debug( void *ctx, int level,
32 const char *file, int line,
33 const char *str )
34{
35 const char *p, *basename;
36
37 /* Extract basename from file */
38 for( p = basename = file; *p != '\0'; p++ )
39 if( *p == '/' || *p == '\\' )
40 basename = p + 1;
41
42 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
43 basename, line, level, str );
44 fflush( (FILE *) ctx );
45}
46
47mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
48{
49 (void) time;
50 return 0x5af2a056;
51}
52
Gilles Peskine8eb29432021-02-03 20:07:11 +010053#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010054static int dummy_entropy( void *data, unsigned char *output, size_t len )
Gilles Peskine504c1a32021-01-05 23:40:14 +010055{
56 size_t i;
57 int ret;
58 (void) data;
59
60 ret = mbedtls_entropy_func( data, output, len );
61 for( i = 0; i < len; i++ )
62 {
63 //replace result with pseudo random
64 output[i] = (unsigned char) rand();
65 }
66 return( ret );
67}
Gilles Peskine8eb29432021-02-03 20:07:11 +010068#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010069
Gilles Peskinedaa94c42021-01-13 18:38:27 +010070void rng_init( rng_context_t *rng )
71{
Gilles Peskine8eb29432021-02-03 20:07:11 +010072#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
73 (void) rng;
74 psa_crypto_init( );
75#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
76
Gilles Peskineba749042021-01-13 20:02:03 +010077#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010078 mbedtls_ctr_drbg_init( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +010079#elif defined(MBEDTLS_HMAC_DRBG_C)
80 mbedtls_hmac_drbg_init( &rng->drbg );
81#else
82#error "No DRBG available"
83#endif
84
Gilles Peskinedaa94c42021-01-13 18:38:27 +010085 mbedtls_entropy_init( &rng->entropy );
Gilles Peskine8eb29432021-02-03 20:07:11 +010086#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010087}
88
89int rng_seed( rng_context_t *rng, int reproducible, const char *pers )
90{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010091#if defined(MBEDTLS_USE_PSA_CRYPTO)
92 if( reproducible )
93 {
94 mbedtls_fprintf( stderr,
95 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n" );
96 return( -1 );
97 }
98#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +010099#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
100 /* The PSA crypto RNG does its own seeding. */
101 (void) rng;
102 (void) pers;
103 if( reproducible )
104 {
105 mbedtls_fprintf( stderr,
106 "The PSA RNG does not support reproducible mode.\n" );
107 return( -1 );
108 }
109 return( 0 );
110#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100111 int ( *f_entropy )( void *, unsigned char *, size_t ) =
112 ( reproducible ? dummy_entropy : mbedtls_entropy_func );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100113
114 if ( reproducible )
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100115 srand( 1 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100116
Gilles Peskineba749042021-01-13 20:02:03 +0100117#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100118 int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
119 f_entropy, &rng->entropy,
120 (const unsigned char *) pers,
121 strlen( pers ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100122#elif defined(MBEDTLS_HMAC_DRBG_C)
123#if defined(MBEDTLS_SHA256_C)
124 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
125#elif defined(MBEDTLS_SHA512_C)
126 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
127#else
128#error "No message digest available for HMAC_DRBG"
129#endif
130 int ret = mbedtls_hmac_drbg_seed( &rng->drbg,
131 mbedtls_md_info_from_type( md_type ),
132 f_entropy, &rng->entropy,
133 (const unsigned char *) pers,
134 strlen( pers ) );
Gilles Peskine8eb29432021-02-03 20:07:11 +0100135#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100136#error "No DRBG available"
Gilles Peskine8eb29432021-02-03 20:07:11 +0100137#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100138
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100139 if( ret != 0 )
140 {
141 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
142 (unsigned int) -ret );
143 return( ret );
144 }
Gilles Peskine8eb29432021-02-03 20:07:11 +0100145#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100146
147 return( 0 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100148}
149
150void rng_free( rng_context_t *rng )
151{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100152#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
153 (void) rng;
154 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
155 * This is ok because none of our applications try to do any crypto after
156 * deinitializing the RNG. */
157 mbedtls_psa_crypto_free( );
158#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
159
Gilles Peskineba749042021-01-13 20:02:03 +0100160#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100161 mbedtls_ctr_drbg_free( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +0100162#elif defined(MBEDTLS_HMAC_DRBG_C)
163 mbedtls_hmac_drbg_free( &rng->drbg );
164#else
165#error "No DRBG available"
166#endif
167
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100168 mbedtls_entropy_free( &rng->entropy );
Gilles Peskine8eb29432021-02-03 20:07:11 +0100169#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100170}
171
Gilles Peskine535fb372021-01-13 18:59:46 +0100172int rng_get( void *p_rng, unsigned char *output, size_t output_len )
173{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100174#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
175 (void) p_rng;
176 return( mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
177 output, output_len ) );
178#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100179 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100180
Gilles Peskineba749042021-01-13 20:02:03 +0100181#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine535fb372021-01-13 18:59:46 +0100182 return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100183#elif defined(MBEDTLS_HMAC_DRBG_C)
184 return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) );
185#else
186#error "No DRBG available"
187#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100188
189#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100190}
191
Gilles Peskine504c1a32021-01-05 23:40:14 +0100192#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
193int ca_callback( void *data, mbedtls_x509_crt const *child,
194 mbedtls_x509_crt **candidates )
195{
196 int ret = 0;
197 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
198 mbedtls_x509_crt *first;
199
200 /* This is a test-only implementation of the CA callback
201 * which always returns the entire list of trusted certificates.
202 * Production implementations managing a large number of CAs
203 * should use an efficient presentation and lookup for the
204 * set of trusted certificates (such as a hashtable) and only
205 * return those trusted certificates which satisfy basic
206 * parental checks, such as the matching of child `Issuer`
207 * and parent `Subject` field or matching key identifiers. */
208 ((void) child);
209
210 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
211 if( first == NULL )
212 {
213 ret = -1;
214 goto exit;
215 }
216 mbedtls_x509_crt_init( first );
217
218 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
219 {
220 ret = -1;
221 goto exit;
222 }
223
224 while( ca->next != NULL )
225 {
226 ca = ca->next;
227 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
228 {
229 ret = -1;
230 goto exit;
231 }
232 }
233
234exit:
235
236 if( ret != 0 )
237 {
238 mbedtls_x509_crt_free( first );
239 mbedtls_free( first );
240 first = NULL;
241 }
242
243 *candidates = first;
244 return( ret );
245}
246#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
247
248int delayed_recv( void *ctx, unsigned char *buf, size_t len )
249{
250 static int first_try = 1;
251 int ret;
252
253 if( first_try )
254 {
255 first_try = 0;
256 return( MBEDTLS_ERR_SSL_WANT_READ );
257 }
258
259 ret = mbedtls_net_recv( ctx, buf, len );
260 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
261 first_try = 1; /* Next call will be a new operation */
262 return( ret );
263}
264
265int delayed_send( void *ctx, const unsigned char *buf, size_t len )
266{
267 static int first_try = 1;
268 int ret;
269
270 if( first_try )
271 {
272 first_try = 0;
273 return( MBEDTLS_ERR_SSL_WANT_WRITE );
274 }
275
276 ret = mbedtls_net_send( ctx, buf, len );
277 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
278 first_try = 1; /* Next call will be a new operation */
279 return( ret );
280}
281
282#if !defined(MBEDTLS_TIMING_C)
283int idle( mbedtls_net_context *fd,
284 int idle_reason )
285#else
286int idle( mbedtls_net_context *fd,
287 mbedtls_timing_delay_context *timer,
288 int idle_reason )
289#endif
290{
291 int ret;
292 int poll_type = 0;
293
294 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
295 poll_type = MBEDTLS_NET_POLL_WRITE;
296 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
297 poll_type = MBEDTLS_NET_POLL_READ;
298#if !defined(MBEDTLS_TIMING_C)
299 else
300 return( 0 );
301#endif
302
303 while( 1 )
304 {
305 /* Check if timer has expired */
306#if defined(MBEDTLS_TIMING_C)
307 if( timer != NULL &&
308 mbedtls_timing_get_delay( timer ) == 2 )
309 {
310 break;
311 }
312#endif /* MBEDTLS_TIMING_C */
313
314 /* Check if underlying transport became available */
315 if( poll_type != 0 )
316 {
317 ret = mbedtls_net_poll( fd, poll_type, 0 );
318 if( ret < 0 )
319 return( ret );
320 if( ret == poll_type )
321 break;
322 }
323 }
324
325 return( 0 );
326}
327
Gilles Peskine53dea742021-02-02 22:55:06 +0100328#if defined(MBEDTLS_TEST_HOOKS)
329
330void test_hooks_init( void )
331{
Gilles Peskinee374b952021-02-03 00:05:19 +0100332 mbedtls_test_info_reset( );
333
334#if defined(MBEDTLS_TEST_MUTEX_USAGE)
335 mbedtls_test_mutex_usage_init( );
336#endif
Gilles Peskine53dea742021-02-02 22:55:06 +0100337}
338
339int test_hooks_failure_detected( void )
340{
Gilles Peskinee374b952021-02-03 00:05:19 +0100341#if defined(MBEDTLS_TEST_MUTEX_USAGE)
342 /* Errors are reported via mbedtls_test_info. */
343 mbedtls_test_mutex_usage_check( );
344#endif
345
346 if( mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS )
347 return( 1 );
Gilles Peskine53dea742021-02-02 22:55:06 +0100348 return( 0 );
349}
350
351void test_hooks_free( void )
352{
353}
354
355#endif /* MBEDTLS_TEST_HOOKS */
356
Gilles Peskineab7ce962021-01-05 21:27:53 +0100357#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */