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 | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 25 | #if defined(MBEDTLS_TEST_HOOKS) |
| 26 | #include "test/helpers.h" |
| 27 | #endif |
| 28 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 29 | #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
| 30 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 31 | void my_debug( void *ctx, int level, |
| 32 | const char *file, int line, |
| 33 | const char *str ) |
| 34 | { |
| 35 | const char *p, *basename; |
| 36 | |
| 37 | /* Extract basename from file */ |
| 38 | for( p = basename = file; *p != '\0'; p++ ) |
| 39 | if( *p == '/' || *p == '\\' ) |
| 40 | basename = p + 1; |
| 41 | |
| 42 | mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s", |
| 43 | basename, line, level, str ); |
| 44 | fflush( (FILE *) ctx ); |
| 45 | } |
| 46 | |
| 47 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ) |
| 48 | { |
| 49 | (void) time; |
| 50 | return 0x5af2a056; |
| 51 | } |
| 52 | |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 53 | #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 54 | static int dummy_entropy( void *data, unsigned char *output, size_t len ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 55 | { |
| 56 | size_t i; |
| 57 | int ret; |
| 58 | (void) data; |
| 59 | |
| 60 | ret = mbedtls_entropy_func( data, output, len ); |
| 61 | for( i = 0; i < len; i++ ) |
| 62 | { |
| 63 | //replace result with pseudo random |
| 64 | output[i] = (unsigned char) rand(); |
| 65 | } |
| 66 | return( ret ); |
| 67 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 68 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 69 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 70 | void rng_init( rng_context_t *rng ) |
| 71 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 72 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 73 | (void) rng; |
| 74 | psa_crypto_init( ); |
| 75 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 76 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 77 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 78 | mbedtls_ctr_drbg_init( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 79 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 80 | mbedtls_hmac_drbg_init( &rng->drbg ); |
| 81 | #else |
| 82 | #error "No DRBG available" |
| 83 | #endif |
| 84 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 85 | mbedtls_entropy_init( &rng->entropy ); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 86 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int rng_seed( rng_context_t *rng, int reproducible, const char *pers ) |
| 90 | { |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 91 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 92 | if( reproducible ) |
| 93 | { |
| 94 | mbedtls_fprintf( stderr, |
| 95 | "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n" ); |
| 96 | return( -1 ); |
| 97 | } |
| 98 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 99 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 100 | /* The PSA crypto RNG does its own seeding. */ |
| 101 | (void) rng; |
| 102 | (void) pers; |
| 103 | if( reproducible ) |
| 104 | { |
| 105 | mbedtls_fprintf( stderr, |
| 106 | "The PSA RNG does not support reproducible mode.\n" ); |
| 107 | return( -1 ); |
| 108 | } |
| 109 | return( 0 ); |
| 110 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 111 | int ( *f_entropy )( void *, unsigned char *, size_t ) = |
| 112 | ( reproducible ? dummy_entropy : mbedtls_entropy_func ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 113 | |
| 114 | if ( reproducible ) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 115 | srand( 1 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 118 | int ret = mbedtls_ctr_drbg_seed( &rng->drbg, |
| 119 | f_entropy, &rng->entropy, |
| 120 | (const unsigned char *) pers, |
| 121 | strlen( pers ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 122 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 123 | #if defined(MBEDTLS_SHA256_C) |
| 124 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256; |
| 125 | #elif defined(MBEDTLS_SHA512_C) |
| 126 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512; |
| 127 | #else |
| 128 | #error "No message digest available for HMAC_DRBG" |
| 129 | #endif |
| 130 | int ret = mbedtls_hmac_drbg_seed( &rng->drbg, |
| 131 | mbedtls_md_info_from_type( md_type ), |
| 132 | f_entropy, &rng->entropy, |
| 133 | (const unsigned char *) pers, |
| 134 | strlen( pers ) ); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 135 | #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 136 | #error "No DRBG available" |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 137 | #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 138 | |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 139 | if( ret != 0 ) |
| 140 | { |
| 141 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", |
| 142 | (unsigned int) -ret ); |
| 143 | return( ret ); |
| 144 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 145 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 146 | |
| 147 | return( 0 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void rng_free( rng_context_t *rng ) |
| 151 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 152 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 153 | (void) rng; |
| 154 | /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs. |
| 155 | * This is ok because none of our applications try to do any crypto after |
| 156 | * deinitializing the RNG. */ |
| 157 | mbedtls_psa_crypto_free( ); |
| 158 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 159 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 160 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 161 | mbedtls_ctr_drbg_free( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 162 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 163 | mbedtls_hmac_drbg_free( &rng->drbg ); |
| 164 | #else |
| 165 | #error "No DRBG available" |
| 166 | #endif |
| 167 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 168 | mbedtls_entropy_free( &rng->entropy ); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 169 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 172 | int rng_get( void *p_rng, unsigned char *output, size_t output_len ) |
| 173 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 174 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 175 | (void) p_rng; |
| 176 | return( mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
| 177 | output, output_len ) ); |
| 178 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 179 | rng_context_t *rng = p_rng; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 180 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 181 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 182 | return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 183 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 184 | return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) ); |
| 185 | #else |
| 186 | #error "No DRBG available" |
| 187 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 188 | |
| 189 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 192 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 193 | int ca_callback( void *data, mbedtls_x509_crt const *child, |
| 194 | mbedtls_x509_crt **candidates ) |
| 195 | { |
| 196 | int ret = 0; |
| 197 | mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; |
| 198 | mbedtls_x509_crt *first; |
| 199 | |
| 200 | /* This is a test-only implementation of the CA callback |
| 201 | * which always returns the entire list of trusted certificates. |
| 202 | * Production implementations managing a large number of CAs |
| 203 | * should use an efficient presentation and lookup for the |
| 204 | * set of trusted certificates (such as a hashtable) and only |
| 205 | * return those trusted certificates which satisfy basic |
| 206 | * parental checks, such as the matching of child `Issuer` |
| 207 | * and parent `Subject` field or matching key identifiers. */ |
| 208 | ((void) child); |
| 209 | |
| 210 | first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
| 211 | if( first == NULL ) |
| 212 | { |
| 213 | ret = -1; |
| 214 | goto exit; |
| 215 | } |
| 216 | mbedtls_x509_crt_init( first ); |
| 217 | |
| 218 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 219 | { |
| 220 | ret = -1; |
| 221 | goto exit; |
| 222 | } |
| 223 | |
| 224 | while( ca->next != NULL ) |
| 225 | { |
| 226 | ca = ca->next; |
| 227 | if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 ) |
| 228 | { |
| 229 | ret = -1; |
| 230 | goto exit; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | exit: |
| 235 | |
| 236 | if( ret != 0 ) |
| 237 | { |
| 238 | mbedtls_x509_crt_free( first ); |
| 239 | mbedtls_free( first ); |
| 240 | first = NULL; |
| 241 | } |
| 242 | |
| 243 | *candidates = first; |
| 244 | return( ret ); |
| 245 | } |
| 246 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 247 | |
| 248 | int delayed_recv( void *ctx, unsigned char *buf, size_t len ) |
| 249 | { |
| 250 | static int first_try = 1; |
| 251 | int ret; |
| 252 | |
| 253 | if( first_try ) |
| 254 | { |
| 255 | first_try = 0; |
| 256 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
| 257 | } |
| 258 | |
| 259 | ret = mbedtls_net_recv( ctx, buf, len ); |
| 260 | if( ret != MBEDTLS_ERR_SSL_WANT_READ ) |
| 261 | first_try = 1; /* Next call will be a new operation */ |
| 262 | return( ret ); |
| 263 | } |
| 264 | |
| 265 | int delayed_send( void *ctx, const unsigned char *buf, size_t len ) |
| 266 | { |
| 267 | static int first_try = 1; |
| 268 | int ret; |
| 269 | |
| 270 | if( first_try ) |
| 271 | { |
| 272 | first_try = 0; |
| 273 | return( MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 274 | } |
| 275 | |
| 276 | ret = mbedtls_net_send( ctx, buf, len ); |
| 277 | if( ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 278 | first_try = 1; /* Next call will be a new operation */ |
| 279 | return( ret ); |
| 280 | } |
| 281 | |
| 282 | #if !defined(MBEDTLS_TIMING_C) |
| 283 | int idle( mbedtls_net_context *fd, |
| 284 | int idle_reason ) |
| 285 | #else |
| 286 | int idle( mbedtls_net_context *fd, |
| 287 | mbedtls_timing_delay_context *timer, |
| 288 | int idle_reason ) |
| 289 | #endif |
| 290 | { |
| 291 | int ret; |
| 292 | int poll_type = 0; |
| 293 | |
| 294 | if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 295 | poll_type = MBEDTLS_NET_POLL_WRITE; |
| 296 | else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ ) |
| 297 | poll_type = MBEDTLS_NET_POLL_READ; |
| 298 | #if !defined(MBEDTLS_TIMING_C) |
| 299 | else |
| 300 | return( 0 ); |
| 301 | #endif |
| 302 | |
| 303 | while( 1 ) |
| 304 | { |
| 305 | /* Check if timer has expired */ |
| 306 | #if defined(MBEDTLS_TIMING_C) |
| 307 | if( timer != NULL && |
| 308 | mbedtls_timing_get_delay( timer ) == 2 ) |
| 309 | { |
| 310 | break; |
| 311 | } |
| 312 | #endif /* MBEDTLS_TIMING_C */ |
| 313 | |
| 314 | /* Check if underlying transport became available */ |
| 315 | if( poll_type != 0 ) |
| 316 | { |
| 317 | ret = mbedtls_net_poll( fd, poll_type, 0 ); |
| 318 | if( ret < 0 ) |
| 319 | return( ret ); |
| 320 | if( ret == poll_type ) |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return( 0 ); |
| 326 | } |
| 327 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 328 | #if defined(MBEDTLS_TEST_HOOKS) |
| 329 | |
| 330 | void test_hooks_init( void ) |
| 331 | { |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 332 | mbedtls_test_info_reset( ); |
| 333 | |
| 334 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 335 | mbedtls_test_mutex_usage_init( ); |
| 336 | #endif |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | int test_hooks_failure_detected( void ) |
| 340 | { |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 341 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 342 | /* Errors are reported via mbedtls_test_info. */ |
| 343 | mbedtls_test_mutex_usage_check( ); |
| 344 | #endif |
| 345 | |
| 346 | if( mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS ) |
| 347 | return( 1 ); |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 348 | return( 0 ); |
| 349 | } |
| 350 | |
| 351 | void test_hooks_free( void ) |
| 352 | { |
| 353 | } |
| 354 | |
| 355 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 356 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 357 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |