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