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 | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 49 | static int dummy_entropy( void *data, unsigned char *output, size_t len ) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 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 Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 64 | void rng_init( rng_context_t *rng ) |
| 65 | { |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 66 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 67 | mbedtls_ctr_drbg_init( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 68 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 69 | mbedtls_hmac_drbg_init( &rng->drbg ); |
| 70 | #else |
| 71 | #error "No DRBG available" |
| 72 | #endif |
| 73 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 74 | mbedtls_entropy_init( &rng->entropy ); |
| 75 | } |
| 76 | |
| 77 | int rng_seed( rng_context_t *rng, int reproducible, const char *pers ) |
| 78 | { |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 79 | int ( *f_entropy )( void *, unsigned char *, size_t ) = |
| 80 | ( reproducible ? dummy_entropy : mbedtls_entropy_func ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 81 | |
| 82 | if ( reproducible ) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 83 | srand( 1 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 84 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 85 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 86 | int ret = mbedtls_ctr_drbg_seed( &rng->drbg, |
| 87 | f_entropy, &rng->entropy, |
| 88 | (const unsigned char *) pers, |
| 89 | strlen( pers ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 90 | #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 Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 107 | 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 Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 113 | |
| 114 | return( 0 ); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void rng_free( rng_context_t *rng ) |
| 118 | { |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 119 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 120 | mbedtls_ctr_drbg_free( &rng->drbg ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 121 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 122 | mbedtls_hmac_drbg_free( &rng->drbg ); |
| 123 | #else |
| 124 | #error "No DRBG available" |
| 125 | #endif |
| 126 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 127 | mbedtls_entropy_free( &rng->entropy ); |
| 128 | } |
| 129 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 130 | int rng_get( void *p_rng, unsigned char *output, size_t output_len ) |
| 131 | { |
| 132 | rng_context_t *rng = p_rng; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 133 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 134 | return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) ); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame^] | 135 | #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 Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 142 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 143 | int 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 | |
| 184 | exit: |
| 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 | |
| 198 | int 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 | |
| 215 | int 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) |
| 233 | int idle( mbedtls_net_context *fd, |
| 234 | int idle_reason ) |
| 235 | #else |
| 236 | int 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 Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 278 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |