Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 1 | /* |
Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 2 | * 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 Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 6 | * |
| 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 Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 25 | #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
| 26 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 27 | void 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 | |
| 43 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) |
| 44 | { |
| 45 | (void) time; |
| 46 | return 0x5af2a056; |
| 47 | } |
| 48 | |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 49 | #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 50 | static int dummy_entropy( void *data, unsigned char *output, size_t len ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 51 | { |
| 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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 64 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 65 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 66 | void rng_init( rng_context_t *rng ) |
| 67 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 68 | #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 Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 73 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 74 | mbedtls_ctr_drbg_init( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 75 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 76 | mbedtls_hmac_drbg_init( &rng->drbg ); |
| 77 | #else |
| 78 | #error "No DRBG available" |
| 79 | #endif |
| 80 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 81 | mbedtls_entropy_init( &rng->entropy ); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 82 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int rng_seed( rng_context_t *rng, int reproducible, const char *pers ) |
| 86 | { |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 87 | #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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 95 | #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 Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 107 | int ( *f_entropy )( void *, unsigned char *, size_t ) = |
| 108 | ( reproducible ? dummy_entropy : mbedtls_entropy_func ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 109 | |
| 110 | if ( reproducible ) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 111 | srand( 1 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 112 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 113 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 114 | int ret = mbedtls_ctr_drbg_seed( &rng->drbg, |
| 115 | f_entropy, &rng->entropy, |
| 116 | (const unsigned char *) pers, |
| 117 | strlen( pers ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 118 | #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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 131 | #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 132 | #error "No DRBG available" |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 133 | #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 134 | |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 135 | 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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 141 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 142 | |
| 143 | return( 0 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void rng_free( rng_context_t *rng ) |
| 147 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 148 | #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 Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 156 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 157 | mbedtls_ctr_drbg_free( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 158 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 159 | mbedtls_hmac_drbg_free( &rng->drbg ); |
| 160 | #else |
| 161 | #error "No DRBG available" |
| 162 | #endif |
| 163 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 164 | mbedtls_entropy_free( &rng->entropy ); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 165 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 168 | int rng_get( void *p_rng, unsigned char *output, size_t output_len ) |
| 169 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 170 | #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 Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 175 | rng_context_t *rng = p_rng; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 176 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 177 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 178 | return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 179 | #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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame^] | 184 | |
| 185 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 189 | int 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 | |
| 230 | exit: |
| 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 | |
| 244 | int 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 | |
| 261 | int 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) |
| 279 | int idle( mbedtls_net_context *fd, |
| 280 | int idle_reason ) |
| 281 | #else |
| 282 | int 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 Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 324 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |