blob: e3c95ccf7c69014c35360e7957d15ca9226951d9 [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{
66 mbedtls_ctr_drbg_init( &rng->drbg );
67 mbedtls_entropy_init( &rng->entropy );
68}
69
70int rng_seed( rng_context_t *rng, int reproducible, const char *pers )
71{
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010072 int ( *f_entropy )( void *, unsigned char *, size_t ) =
73 ( reproducible ? dummy_entropy : mbedtls_entropy_func );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010074
75 if ( reproducible )
Gilles Peskinedaa94c42021-01-13 18:38:27 +010076 srand( 1 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010077
Gilles Peskinef1cb75f2021-01-13 18:46:01 +010078 int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
79 f_entropy, &rng->entropy,
80 (const unsigned char *) pers,
81 strlen( pers ) );
82 if( ret != 0 )
83 {
84 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
85 (unsigned int) -ret );
86 return( ret );
87 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +010088
89 return( 0 );
Gilles Peskinedaa94c42021-01-13 18:38:27 +010090}
91
92void rng_free( rng_context_t *rng )
93{
94 mbedtls_ctr_drbg_free( &rng->drbg );
95 mbedtls_entropy_free( &rng->entropy );
96}
97
Gilles Peskine504c1a32021-01-05 23:40:14 +010098#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
99int ca_callback( void *data, mbedtls_x509_crt const *child,
100 mbedtls_x509_crt **candidates )
101{
102 int ret = 0;
103 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
104 mbedtls_x509_crt *first;
105
106 /* This is a test-only implementation of the CA callback
107 * which always returns the entire list of trusted certificates.
108 * Production implementations managing a large number of CAs
109 * should use an efficient presentation and lookup for the
110 * set of trusted certificates (such as a hashtable) and only
111 * return those trusted certificates which satisfy basic
112 * parental checks, such as the matching of child `Issuer`
113 * and parent `Subject` field or matching key identifiers. */
114 ((void) child);
115
116 first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
117 if( first == NULL )
118 {
119 ret = -1;
120 goto exit;
121 }
122 mbedtls_x509_crt_init( first );
123
124 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
125 {
126 ret = -1;
127 goto exit;
128 }
129
130 while( ca->next != NULL )
131 {
132 ca = ca->next;
133 if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
134 {
135 ret = -1;
136 goto exit;
137 }
138 }
139
140exit:
141
142 if( ret != 0 )
143 {
144 mbedtls_x509_crt_free( first );
145 mbedtls_free( first );
146 first = NULL;
147 }
148
149 *candidates = first;
150 return( ret );
151}
152#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
153
154int delayed_recv( void *ctx, unsigned char *buf, size_t len )
155{
156 static int first_try = 1;
157 int ret;
158
159 if( first_try )
160 {
161 first_try = 0;
162 return( MBEDTLS_ERR_SSL_WANT_READ );
163 }
164
165 ret = mbedtls_net_recv( ctx, buf, len );
166 if( ret != MBEDTLS_ERR_SSL_WANT_READ )
167 first_try = 1; /* Next call will be a new operation */
168 return( ret );
169}
170
171int delayed_send( void *ctx, const unsigned char *buf, size_t len )
172{
173 static int first_try = 1;
174 int ret;
175
176 if( first_try )
177 {
178 first_try = 0;
179 return( MBEDTLS_ERR_SSL_WANT_WRITE );
180 }
181
182 ret = mbedtls_net_send( ctx, buf, len );
183 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
184 first_try = 1; /* Next call will be a new operation */
185 return( ret );
186}
187
188#if !defined(MBEDTLS_TIMING_C)
189int idle( mbedtls_net_context *fd,
190 int idle_reason )
191#else
192int idle( mbedtls_net_context *fd,
193 mbedtls_timing_delay_context *timer,
194 int idle_reason )
195#endif
196{
197 int ret;
198 int poll_type = 0;
199
200 if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
201 poll_type = MBEDTLS_NET_POLL_WRITE;
202 else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
203 poll_type = MBEDTLS_NET_POLL_READ;
204#if !defined(MBEDTLS_TIMING_C)
205 else
206 return( 0 );
207#endif
208
209 while( 1 )
210 {
211 /* Check if timer has expired */
212#if defined(MBEDTLS_TIMING_C)
213 if( timer != NULL &&
214 mbedtls_timing_get_delay( timer ) == 2 )
215 {
216 break;
217 }
218#endif /* MBEDTLS_TIMING_C */
219
220 /* Check if underlying transport became available */
221 if( poll_type != 0 )
222 {
223 ret = mbedtls_net_poll( fd, poll_type, 0 );
224 if( ret < 0 )
225 return( ret );
226 if( ret == poll_type )
227 break;
228 }
229 }
230
231 return( 0 );
232}
233
Gilles Peskineab7ce962021-01-05 21:27:53 +0100234#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */