blob: 46cea144c1d8d08565783df87b73d4d49e339226 [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 Peskinef1cb75f2021-01-13 18:46:01 +010079 int ( *f_entropy )( void *, unsigned char *, size_t ) =
80 ( reproducible ? dummy_entropy : mbedtls_entropy_func );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010081
82 if ( reproducible )
Gilles Peskinedaa94c42021-01-13 18:38:27 +010083 srand( 1 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010084
Gilles Peskineba749042021-01-13 20:02:03 +010085#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010086 int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
87 f_entropy, &rng->entropy,
88 (const unsigned char *) pers,
89 strlen( pers ) );
Gilles Peskineba749042021-01-13 20:02:03 +010090#elif defined(MBEDTLS_HMAC_DRBG_C)
91#if defined(MBEDTLS_SHA256_C)
92 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
93#elif defined(MBEDTLS_SHA512_C)
94 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
95#else
96#error "No message digest available for HMAC_DRBG"
97#endif
98 int ret = mbedtls_hmac_drbg_seed( &rng->drbg,
99 mbedtls_md_info_from_type( md_type ),
100 f_entropy, &rng->entropy,
101 (const unsigned char *) pers,
102 strlen( pers ) );
103#else
104#error "No DRBG available"
105#endif
106
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100107 if( ret != 0 )
108 {
109 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
110 (unsigned int) -ret );
111 return( ret );
112 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100113
114 return( 0 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100115}
116
117void rng_free( rng_context_t *rng )
118{
Gilles Peskineba749042021-01-13 20:02:03 +0100119#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100120 mbedtls_ctr_drbg_free( &rng->drbg );
Gilles Peskineba749042021-01-13 20:02:03 +0100121#elif defined(MBEDTLS_HMAC_DRBG_C)
122 mbedtls_hmac_drbg_free( &rng->drbg );
123#else
124#error "No DRBG available"
125#endif
126
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100127 mbedtls_entropy_free( &rng->entropy );
128}
129
Gilles Peskine535fb372021-01-13 18:59:46 +0100130int rng_get( void *p_rng, unsigned char *output, size_t output_len )
131{
132 rng_context_t *rng = p_rng;
Gilles Peskineba749042021-01-13 20:02:03 +0100133#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine535fb372021-01-13 18:59:46 +0100134 return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) );
Gilles Peskineba749042021-01-13 20:02:03 +0100135#elif defined(MBEDTLS_HMAC_DRBG_C)
136 return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) );
137#else
138#error "No DRBG available"
139#endif
Gilles Peskine535fb372021-01-13 18:59:46 +0100140}
141
Gilles Peskine504c1a32021-01-05 23:40:14 +0100142#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
143int ca_callback( void *data, mbedtls_x509_crt const *child,
144 mbedtls_x509_crt **candidates )
145{
146 int ret = 0;
147 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
148 mbedtls_x509_crt *first;
149
150 /* This is a test-only implementation of the CA callback
151 * which always returns the entire list of trusted certificates.
152 * Production implementations managing a large number of CAs
153 * should use an efficient presentation and lookup for the
154 * set of trusted certificates (such as a hashtable) and only
155 * return those trusted certificates which satisfy basic
156 * parental checks, such as the matching of child `Issuer`
157 * and parent `Subject` field or matching key identifiers. */
158 ((void) child);
159
160 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
161 if( first == NULL )
162 {
163 ret = -1;
164 goto exit;
165 }
166 mbedtls_x509_crt_init( first );
167
168 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
169 {
170 ret = -1;
171 goto exit;
172 }
173
174 while( ca->next != NULL )
175 {
176 ca = ca->next;
177 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
178 {
179 ret = -1;
180 goto exit;
181 }
182 }
183
184exit:
185
186 if( ret != 0 )
187 {
188 mbedtls_x509_crt_free( first );
189 mbedtls_free( first );
190 first = NULL;
191 }
192
193 *candidates = first;
194 return( ret );
195}
196#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
197
198int delayed_recv( void *ctx, unsigned char *buf, size_t len )
199{
200 static int first_try = 1;
201 int ret;
202
203 if( first_try )
204 {
205 first_try = 0;
206 return( MBEDTLS_ERR_SSL_WANT_READ );
207 }
208
209 ret = mbedtls_net_recv( ctx, buf, len );
210 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
211 first_try = 1; /* Next call will be a new operation */
212 return( ret );
213}
214
215int delayed_send( void *ctx, const unsigned char *buf, size_t len )
216{
217 static int first_try = 1;
218 int ret;
219
220 if( first_try )
221 {
222 first_try = 0;
223 return( MBEDTLS_ERR_SSL_WANT_WRITE );
224 }
225
226 ret = mbedtls_net_send( ctx, buf, len );
227 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
228 first_try = 1; /* Next call will be a new operation */
229 return( ret );
230}
231
232#if !defined(MBEDTLS_TIMING_C)
233int idle( mbedtls_net_context *fd,
234 int idle_reason )
235#else
236int idle( mbedtls_net_context *fd,
237 mbedtls_timing_delay_context *timer,
238 int idle_reason )
239#endif
240{
241 int ret;
242 int poll_type = 0;
243
244 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
245 poll_type = MBEDTLS_NET_POLL_WRITE;
246 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
247 poll_type = MBEDTLS_NET_POLL_READ;
248#if !defined(MBEDTLS_TIMING_C)
249 else
250 return( 0 );
251#endif
252
253 while( 1 )
254 {
255 /* Check if timer has expired */
256#if defined(MBEDTLS_TIMING_C)
257 if( timer != NULL &&
258 mbedtls_timing_get_delay( timer ) == 2 )
259 {
260 break;
261 }
262#endif /* MBEDTLS_TIMING_C */
263
264 /* Check if underlying transport became available */
265 if( poll_type != 0 )
266 {
267 ret = mbedtls_net_poll( fd, poll_type, 0 );
268 if( ret < 0 )
269 return( ret );
270 if( ret == poll_type )
271 break;
272 }
273 }
274
275 return( 0 );
276}
277
Gilles Peskineab7ce962021-01-05 21:27:53 +0100278#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */