blob: 6636e9e1ad654a4379a9007514835b875e35b7bc [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 Peskineab7ce962021-01-05 21:27:53 +010025#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
26
Gilles Peskine504c1a32021-01-05 23:40:14 +010027void my_debug( void *ctx, int level,
28 const char *file, int line,
29 const char *str )
30{
31 const char *p, *basename;
32
33 /* Extract basename from file */
34 for( p = basename = file; *p != '\0'; p++ )
35 if( *p == '/' || *p == '\\' )
36 basename = p + 1;
37
38 mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
39 basename, line, level, str );
40 fflush( (FILE *) ctx );
41}
42
43mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
44{
45 (void) time;
46 return 0x5af2a056;
47}
48
Gilles Peskine8eb29432021-02-03 20:07:11 +010049#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010050static int dummy_entropy( void *data, unsigned char *output, size_t len )
Gilles Peskine504c1a32021-01-05 23:40:14 +010051{
52 size_t i;
53 int ret;
54 (void) data;
55
56 ret = mbedtls_entropy_func( data, output, len );
57 for( i = 0; i < len; i++ )
58 {
59 //replace result with pseudo random
60 output[i] = (unsigned char) rand();
61 }
62 return( ret );
63}
Gilles Peskine8eb29432021-02-03 20:07:11 +010064#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010065
Gilles Peskinedaa94c42021-01-13 18:38:27 +010066void rng_init( rng_context_t *rng )
67{
Gilles Peskine8eb29432021-02-03 20:07:11 +010068#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
69 (void) rng;
70 psa_crypto_init( );
71#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
72
Gilles Peskineba749042021-01-13 20:02:03 +010073#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010074 mbedtls_ctr_drbg_init( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +010075#elif defined(MBEDTLS_HMAC_DRBG_C)
76 mbedtls_hmac_drbg_init( &rng->drbg );
77#else
78#error "No DRBG available"
79#endif
80
Gilles Peskinedaa94c42021-01-13 18:38:27 +010081 mbedtls_entropy_init( &rng->entropy );
Gilles Peskine8eb29432021-02-03 20:07:11 +010082#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010083}
84
85int rng_seed( rng_context_t *rng, int reproducible, const char *pers )
86{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010087#if defined(MBEDTLS_USE_PSA_CRYPTO)
88 if( reproducible )
89 {
90 mbedtls_fprintf( stderr,
91 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n" );
92 return( -1 );
93 }
94#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +010095#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
96 /* The PSA crypto RNG does its own seeding. */
97 (void) rng;
98 (void) pers;
99 if( reproducible )
100 {
101 mbedtls_fprintf( stderr,
102 "The PSA RNG does not support reproducible mode.\n" );
103 return( -1 );
104 }
105 return( 0 );
106#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100107 int ( *f_entropy )( void *, unsigned char *, size_t ) =
108 ( reproducible ? dummy_entropy : mbedtls_entropy_func );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100109
110 if ( reproducible )
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100111 srand( 1 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100112
Gilles Peskineba749042021-01-13 20:02:03 +0100113#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100114 int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
115 f_entropy, &rng->entropy,
116 (const unsigned char *) pers,
117 strlen( pers ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100118#elif defined(MBEDTLS_HMAC_DRBG_C)
119#if defined(MBEDTLS_SHA256_C)
120 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
121#elif defined(MBEDTLS_SHA512_C)
122 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
123#else
124#error "No message digest available for HMAC_DRBG"
125#endif
126 int ret = mbedtls_hmac_drbg_seed( &rng->drbg,
127 mbedtls_md_info_from_type( md_type ),
128 f_entropy, &rng->entropy,
129 (const unsigned char *) pers,
130 strlen( pers ) );
Gilles Peskine8eb29432021-02-03 20:07:11 +0100131#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100132#error "No DRBG available"
Gilles Peskine8eb29432021-02-03 20:07:11 +0100133#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100134
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100135 if( ret != 0 )
136 {
137 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
138 (unsigned int) -ret );
139 return( ret );
140 }
Gilles Peskine8eb29432021-02-03 20:07:11 +0100141#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100142
143 return( 0 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100144}
145
146void rng_free( rng_context_t *rng )
147{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100148#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
149 (void) rng;
150 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
151 * This is ok because none of our applications try to do any crypto after
152 * deinitializing the RNG. */
153 mbedtls_psa_crypto_free( );
154#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
155
Gilles Peskineba749042021-01-13 20:02:03 +0100156#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100157 mbedtls_ctr_drbg_free( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +0100158#elif defined(MBEDTLS_HMAC_DRBG_C)
159 mbedtls_hmac_drbg_free( &rng->drbg );
160#else
161#error "No DRBG available"
162#endif
163
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100164 mbedtls_entropy_free( &rng->entropy );
Gilles Peskine8eb29432021-02-03 20:07:11 +0100165#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100166}
167
Gilles Peskine535fb372021-01-13 18:59:46 +0100168int rng_get( void *p_rng, unsigned char *output, size_t output_len )
169{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100170#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
171 (void) p_rng;
172 return( mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
173 output, output_len ) );
174#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100175 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100176
Gilles Peskineba749042021-01-13 20:02:03 +0100177#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine535fb372021-01-13 18:59:46 +0100178 return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100179#elif defined(MBEDTLS_HMAC_DRBG_C)
180 return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) );
181#else
182#error "No DRBG available"
183#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100184
185#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100186}
187
Gilles Peskine504c1a32021-01-05 23:40:14 +0100188#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
189int ca_callback( void *data, mbedtls_x509_crt const *child,
190 mbedtls_x509_crt **candidates )
191{
192 int ret = 0;
193 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
194 mbedtls_x509_crt *first;
195
196 /* This is a test-only implementation of the CA callback
197 * which always returns the entire list of trusted certificates.
198 * Production implementations managing a large number of CAs
199 * should use an efficient presentation and lookup for the
200 * set of trusted certificates (such as a hashtable) and only
201 * return those trusted certificates which satisfy basic
202 * parental checks, such as the matching of child `Issuer`
203 * and parent `Subject` field or matching key identifiers. */
204 ((void) child);
205
206 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
207 if( first == NULL )
208 {
209 ret = -1;
210 goto exit;
211 }
212 mbedtls_x509_crt_init( first );
213
214 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
215 {
216 ret = -1;
217 goto exit;
218 }
219
220 while( ca->next != NULL )
221 {
222 ca = ca->next;
223 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
224 {
225 ret = -1;
226 goto exit;
227 }
228 }
229
230exit:
231
232 if( ret != 0 )
233 {
234 mbedtls_x509_crt_free( first );
235 mbedtls_free( first );
236 first = NULL;
237 }
238
239 *candidates = first;
240 return( ret );
241}
242#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
243
244int delayed_recv( void *ctx, unsigned char *buf, size_t len )
245{
246 static int first_try = 1;
247 int ret;
248
249 if( first_try )
250 {
251 first_try = 0;
252 return( MBEDTLS_ERR_SSL_WANT_READ );
253 }
254
255 ret = mbedtls_net_recv( ctx, buf, len );
256 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
257 first_try = 1; /* Next call will be a new operation */
258 return( ret );
259}
260
261int delayed_send( void *ctx, const unsigned char *buf, size_t len )
262{
263 static int first_try = 1;
264 int ret;
265
266 if( first_try )
267 {
268 first_try = 0;
269 return( MBEDTLS_ERR_SSL_WANT_WRITE );
270 }
271
272 ret = mbedtls_net_send( ctx, buf, len );
273 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
274 first_try = 1; /* Next call will be a new operation */
275 return( ret );
276}
277
278#if !defined(MBEDTLS_TIMING_C)
279int idle( mbedtls_net_context *fd,
280 int idle_reason )
281#else
282int idle( mbedtls_net_context *fd,
283 mbedtls_timing_delay_context *timer,
284 int idle_reason )
285#endif
286{
287 int ret;
288 int poll_type = 0;
289
290 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
291 poll_type = MBEDTLS_NET_POLL_WRITE;
292 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
293 poll_type = MBEDTLS_NET_POLL_READ;
294#if !defined(MBEDTLS_TIMING_C)
295 else
296 return( 0 );
297#endif
298
299 while( 1 )
300 {
301 /* Check if timer has expired */
302#if defined(MBEDTLS_TIMING_C)
303 if( timer != NULL &&
304 mbedtls_timing_get_delay( timer ) == 2 )
305 {
306 break;
307 }
308#endif /* MBEDTLS_TIMING_C */
309
310 /* Check if underlying transport became available */
311 if( poll_type != 0 )
312 {
313 ret = mbedtls_net_poll( fd, poll_type, 0 );
314 if( ret < 0 )
315 return( ret );
316 if( ret == poll_type )
317 break;
318 }
319 }
320
321 return( 0 );
322}
323
Gilles Peskineab7ce962021-01-05 21:27:53 +0100324#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */