blob: 56e94310c576b43767ee276db190b5a97401b2b8 [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 Peskinef1cb75f2021-01-13 18:46:01 +010049static int dummy_entropy( void *data, unsigned char *output, size_t len )
Gilles Peskine504c1a32021-01-05 23:40:14 +010050{
51 size_t i;
52 int ret;
53 (void) data;
54
55 ret = mbedtls_entropy_func( data, output, len );
56 for( i = 0; i < len; i++ )
57 {
58 //replace result with pseudo random
59 output[i] = (unsigned char) rand();
60 }
61 return( ret );
62}
63
Gilles Peskinedaa94c42021-01-13 18:38:27 +010064void rng_init( rng_context_t *rng )
65{
Gilles Peskineba749042021-01-13 20:02:03 +010066#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010067 mbedtls_ctr_drbg_init( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +010068#elif defined(MBEDTLS_HMAC_DRBG_C)
69 mbedtls_hmac_drbg_init( &rng->drbg );
70#else
71#error "No DRBG available"
72#endif
73
Gilles Peskinedaa94c42021-01-13 18:38:27 +010074 mbedtls_entropy_init( &rng->entropy );
75}
76
77int rng_seed( rng_context_t *rng, int reproducible, const char *pers )
78{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010079#if defined(MBEDTLS_USE_PSA_CRYPTO)
80 if( reproducible )
81 {
82 mbedtls_fprintf( stderr,
83 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n" );
84 return( -1 );
85 }
86#endif
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010087 int ( *f_entropy )( void *, unsigned char *, size_t ) =
88 ( reproducible ? dummy_entropy : mbedtls_entropy_func );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010089
90 if ( reproducible )
Gilles Peskinedaa94c42021-01-13 18:38:27 +010091 srand( 1 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010092
Gilles Peskineba749042021-01-13 20:02:03 +010093#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010094 int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
95 f_entropy, &rng->entropy,
96 (const unsigned char *) pers,
97 strlen( pers ) );
Gilles Peskineba749042021-01-13 20:02:03 +010098#elif defined(MBEDTLS_HMAC_DRBG_C)
99#if defined(MBEDTLS_SHA256_C)
100 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
101#elif defined(MBEDTLS_SHA512_C)
102 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
103#else
104#error "No message digest available for HMAC_DRBG"
105#endif
106 int ret = mbedtls_hmac_drbg_seed( &rng->drbg,
107 mbedtls_md_info_from_type( md_type ),
108 f_entropy, &rng->entropy,
109 (const unsigned char *) pers,
110 strlen( pers ) );
111#else
112#error "No DRBG available"
113#endif
114
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100115 if( ret != 0 )
116 {
117 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
118 (unsigned int) -ret );
119 return( ret );
120 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100121
122 return( 0 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100123}
124
125void rng_free( rng_context_t *rng )
126{
Gilles Peskineba749042021-01-13 20:02:03 +0100127#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100128 mbedtls_ctr_drbg_free( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +0100129#elif defined(MBEDTLS_HMAC_DRBG_C)
130 mbedtls_hmac_drbg_free( &rng->drbg );
131#else
132#error "No DRBG available"
133#endif
134
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100135 mbedtls_entropy_free( &rng->entropy );
136}
137
Gilles Peskine535fb372021-01-13 18:59:46 +0100138int rng_get( void *p_rng, unsigned char *output, size_t output_len )
139{
140 rng_context_t *rng = p_rng;
Gilles Peskineba749042021-01-13 20:02:03 +0100141#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine535fb372021-01-13 18:59:46 +0100142 return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100143#elif defined(MBEDTLS_HMAC_DRBG_C)
144 return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) );
145#else
146#error "No DRBG available"
147#endif
Gilles Peskine535fb372021-01-13 18:59:46 +0100148}
149
Gilles Peskine504c1a32021-01-05 23:40:14 +0100150#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
151int ca_callback( void *data, mbedtls_x509_crt const *child,
152 mbedtls_x509_crt **candidates )
153{
154 int ret = 0;
155 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
156 mbedtls_x509_crt *first;
157
158 /* This is a test-only implementation of the CA callback
159 * which always returns the entire list of trusted certificates.
160 * Production implementations managing a large number of CAs
161 * should use an efficient presentation and lookup for the
162 * set of trusted certificates (such as a hashtable) and only
163 * return those trusted certificates which satisfy basic
164 * parental checks, such as the matching of child `Issuer`
165 * and parent `Subject` field or matching key identifiers. */
166 ((void) child);
167
168 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
169 if( first == NULL )
170 {
171 ret = -1;
172 goto exit;
173 }
174 mbedtls_x509_crt_init( first );
175
176 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
177 {
178 ret = -1;
179 goto exit;
180 }
181
182 while( ca->next != NULL )
183 {
184 ca = ca->next;
185 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
186 {
187 ret = -1;
188 goto exit;
189 }
190 }
191
192exit:
193
194 if( ret != 0 )
195 {
196 mbedtls_x509_crt_free( first );
197 mbedtls_free( first );
198 first = NULL;
199 }
200
201 *candidates = first;
202 return( ret );
203}
204#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
205
206int delayed_recv( void *ctx, unsigned char *buf, size_t len )
207{
208 static int first_try = 1;
209 int ret;
210
211 if( first_try )
212 {
213 first_try = 0;
214 return( MBEDTLS_ERR_SSL_WANT_READ );
215 }
216
217 ret = mbedtls_net_recv( ctx, buf, len );
218 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
219 first_try = 1; /* Next call will be a new operation */
220 return( ret );
221}
222
223int delayed_send( void *ctx, const unsigned char *buf, size_t len )
224{
225 static int first_try = 1;
226 int ret;
227
228 if( first_try )
229 {
230 first_try = 0;
231 return( MBEDTLS_ERR_SSL_WANT_WRITE );
232 }
233
234 ret = mbedtls_net_send( ctx, buf, len );
235 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
236 first_try = 1; /* Next call will be a new operation */
237 return( ret );
238}
239
240#if !defined(MBEDTLS_TIMING_C)
241int idle( mbedtls_net_context *fd,
242 int idle_reason )
243#else
244int idle( mbedtls_net_context *fd,
245 mbedtls_timing_delay_context *timer,
246 int idle_reason )
247#endif
248{
249 int ret;
250 int poll_type = 0;
251
252 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
253 poll_type = MBEDTLS_NET_POLL_WRITE;
254 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
255 poll_type = MBEDTLS_NET_POLL_READ;
256#if !defined(MBEDTLS_TIMING_C)
257 else
258 return( 0 );
259#endif
260
261 while( 1 )
262 {
263 /* Check if timer has expired */
264#if defined(MBEDTLS_TIMING_C)
265 if( timer != NULL &&
266 mbedtls_timing_get_delay( timer ) == 2 )
267 {
268 break;
269 }
270#endif /* MBEDTLS_TIMING_C */
271
272 /* Check if underlying transport became available */
273 if( poll_type != 0 )
274 {
275 ret = mbedtls_net_poll( fd, poll_type, 0 );
276 if( ret < 0 )
277 return( ret );
278 if( ret == poll_type )
279 break;
280 }
281 }
282
283 return( 0 );
284}
285
Gilles Peskineab7ce962021-01-05 21:27:53 +0100286#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */