Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Common code for SSL test programs |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H |
| 21 | #define MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H |
| 22 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 23 | #include "mbedtls/build_info.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 24 | |
| 25 | #if defined(MBEDTLS_PLATFORM_C) |
| 26 | #include "mbedtls/platform.h" |
| 27 | #else |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #define mbedtls_calloc calloc |
| 31 | #define mbedtls_free free |
| 32 | #define mbedtls_time time |
| 33 | #define mbedtls_time_t time_t |
| 34 | #define mbedtls_printf printf |
| 35 | #define mbedtls_fprintf fprintf |
| 36 | #define mbedtls_snprintf snprintf |
| 37 | #define mbedtls_exit exit |
| 38 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 39 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 40 | #endif |
| 41 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 42 | #undef HAVE_RNG |
| 43 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 44 | ( defined(MBEDTLS_USE_PSA_CRYPTO) || \ |
| 45 | defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) ) |
| 46 | #define HAVE_RNG |
| 47 | #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) |
| 48 | #define HAVE_RNG |
| 49 | #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \ |
| 50 | ( defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C) ) |
| 51 | #define HAVE_RNG |
| 52 | #endif |
| 53 | |
| 54 | #if !defined(MBEDTLS_NET_C) || \ |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 55 | !defined(MBEDTLS_SSL_TLS_C) |
| 56 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
| 57 | "MBEDTLS_NET_C and/or " \ |
| 58 | "MBEDTLS_SSL_TLS_C not defined." |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 59 | #elif !defined(HAVE_RNG) |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 60 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 61 | "No random generator is available.\n" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 62 | #else |
| 63 | #undef MBEDTLS_SSL_TEST_IMPOSSIBLE |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 64 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 65 | #undef HAVE_RNG |
| 66 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 67 | #include <stdio.h> |
| 68 | #include <stdlib.h> |
| 69 | #include <string.h> |
| 70 | |
| 71 | #include "mbedtls/net_sockets.h" |
| 72 | #include "mbedtls/ssl.h" |
Glenn Strauss | 6eef563 | 2022-01-23 08:37:02 -0500 | [diff] [blame] | 73 | #include "mbedtls/ssl_ciphersuites.h" |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 74 | #include "mbedtls/entropy.h" |
| 75 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 76 | #include "mbedtls/hmac_drbg.h" |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 77 | #include "mbedtls/x509.h" |
| 78 | #include "mbedtls/error.h" |
| 79 | #include "mbedtls/debug.h" |
| 80 | #include "mbedtls/timing.h" |
| 81 | #include "mbedtls/base64.h" |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 82 | #include "test/certs.h" |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 83 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 85 | #include "psa/crypto.h" |
| 86 | #include "mbedtls/psa_util.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 87 | #endif |
| 88 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 89 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 90 | #include "mbedtls/memory_buffer_alloc.h" |
| 91 | #endif |
| 92 | |
| 93 | #include <test/helpers.h> |
| 94 | |
Gilles Peskine | c772b18 | 2021-01-12 15:55:10 +0100 | [diff] [blame] | 95 | #include "../test/query_config.h" |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 96 | |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 97 | typedef struct eap_tls_keys |
| 98 | { |
| 99 | unsigned char master_secret[48]; |
| 100 | unsigned char randbytes[64]; |
| 101 | mbedtls_tls_prf_types tls_prf_type; |
| 102 | } eap_tls_keys; |
| 103 | |
| 104 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) |
| 105 | |
| 106 | /* Supported SRTP mode needs a maximum of : |
| 107 | * - 16 bytes for key (AES-128) |
| 108 | * - 14 bytes SALT |
| 109 | * One for sender, one for receiver context |
| 110 | */ |
| 111 | #define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60 |
| 112 | |
| 113 | typedef struct dtls_srtp_keys |
| 114 | { |
| 115 | unsigned char master_secret[48]; |
| 116 | unsigned char randbytes[64]; |
| 117 | mbedtls_tls_prf_types tls_prf_type; |
| 118 | } dtls_srtp_keys; |
| 119 | |
| 120 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 121 | |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 122 | typedef struct |
| 123 | { |
| 124 | mbedtls_ssl_context *ssl; |
| 125 | mbedtls_net_context *net; |
| 126 | } io_ctx_t; |
| 127 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 128 | void my_debug( void *ctx, int level, |
| 129 | const char *file, int line, |
| 130 | const char *str ); |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 131 | |
Raoul Strackx | 9ed9bc9 | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 132 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 133 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ); |
Raoul Strackx | 9ed9bc9 | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 134 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 135 | |
Jerry Yu | 79c0041 | 2022-03-01 17:03:56 +0800 | [diff] [blame] | 136 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 137 | /* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use |
| 138 | * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator. |
| 139 | * |
| 140 | * The constraints are: |
| 141 | * - Without the entropy module, the PSA RNG is the only option. |
| 142 | * - Without at least one of the DRBG modules, the PSA RNG is the only option. |
| 143 | * - The PSA RNG does not support explicit seeding, so it is incompatible with |
| 144 | * the reproducible mode used by test programs. |
| 145 | * - For good overall test coverage, there should be at least one configuration |
| 146 | * where the test programs use the PSA RNG while the PSA RNG is itself based |
| 147 | * on entropy+DRBG, and at least one configuration where the test programs |
| 148 | * do not use the PSA RNG even though it's there. |
| 149 | * |
| 150 | * A simple choice that meets the constraints is to use the PSA RNG whenever |
| 151 | * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the |
| 152 | * choice to use the PSA RNG in the test programs and the choice to use |
| 153 | * PSA crypto when TLS code needs crypto have to be tied together, but it |
| 154 | * happens to be a good match. It's also a good match from an application |
| 155 | * perspective: either PSA is preferred for TLS (both for crypto and for |
| 156 | * random generation) or it isn't. |
| 157 | */ |
| 158 | #define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG |
| 159 | #endif |
| 160 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 161 | /** A context for random number generation (RNG). |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 162 | */ |
| 163 | typedef struct |
| 164 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 165 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 166 | unsigned char dummy; |
| 167 | #else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 168 | mbedtls_entropy_context entropy; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 169 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 170 | mbedtls_ctr_drbg_context drbg; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 171 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 172 | mbedtls_hmac_drbg_context drbg; |
| 173 | #else |
| 174 | #error "No DRBG available" |
| 175 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 176 | #endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 177 | } rng_context_t; |
| 178 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 179 | /** Initialize the RNG. |
| 180 | * |
| 181 | * This function only initializes the memory used by the RNG context. |
| 182 | * Before using the RNG, it must be seeded with rng_seed(). |
| 183 | */ |
| 184 | void rng_init( rng_context_t *rng ); |
| 185 | |
| 186 | /* Seed the random number generator. |
| 187 | * |
| 188 | * \param rng The RNG context to use. It must have been initialized |
| 189 | * with rng_init(). |
| 190 | * \param reproducible If zero, seed the RNG from entropy. |
| 191 | * If nonzero, use a fixed seed, so that the program |
| 192 | * will produce the same sequence of random numbers |
| 193 | * each time it is invoked. |
| 194 | * \param pers A null-terminated string. Different values for this |
| 195 | * string cause the RNG to emit different output for |
| 196 | * the same seed. |
| 197 | * |
| 198 | * return 0 on success, a negative value on error. |
| 199 | */ |
| 200 | int rng_seed( rng_context_t *rng, int reproducible, const char *pers ); |
| 201 | |
| 202 | /** Deinitialize the RNG. Free any embedded resource. |
| 203 | * |
| 204 | * \param rng The RNG context to deinitialize. It must have been |
| 205 | * initialized with rng_init(). |
| 206 | */ |
| 207 | void rng_free( rng_context_t *rng ); |
| 208 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 209 | /** Generate random data. |
| 210 | * |
| 211 | * This function is suitable for use as the \c f_rng argument to Mbed TLS |
| 212 | * library functions. |
| 213 | * |
Gilles Peskine | da9529f | 2021-01-25 13:42:42 +0100 | [diff] [blame] | 214 | * \param p_rng The random generator context. This must be a pointer to |
| 215 | * a #rng_context_t structure. |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 216 | * \param output The buffer to fill. |
| 217 | * \param output_len The length of the buffer in bytes. |
| 218 | * |
| 219 | * \return \c 0 on success. |
| 220 | * \return An Mbed TLS error code on error. |
| 221 | */ |
| 222 | int rng_get( void *p_rng, unsigned char *output, size_t output_len ); |
| 223 | |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 224 | /** Parse command-line option: key_opaque_algs |
| 225 | * |
| 226 | * |
| 227 | * \param arg String value of key_opaque_algs |
| 228 | * Coma-separated pair of values among the following: |
| 229 | * - "rsa-sign-pkcs1" |
| 230 | * - "rsa-sign-pss" |
| 231 | * - "rsa-decrypt" |
| 232 | * - "ecdsa-sign" |
| 233 | * - "ecdh" |
| 234 | * - "none" (only acceptable for the second value). |
| 235 | * \param alg1 Address of pointer to alg #1 |
| 236 | * \param alg2 Address of pointer to alg #2 |
| 237 | * |
| 238 | * \return \c 0 on success. |
| 239 | * \return \c 1 on parse failure. |
| 240 | */ |
| 241 | int key_opaque_alg_parse( const char *arg, const char **alg1, const char **alg2 ); |
| 242 | |
Przemek Stekiel | 76a41f5 | 2022-05-04 13:55:23 +0200 | [diff] [blame] | 243 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemek Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 244 | /** Parse given opaque key algoritms to obtain psa algs and usage |
| 245 | * that will be passed to mbedtls_pk_wrap_as_opaque(). |
| 246 | * |
| 247 | * |
| 248 | * \param alg1 input string opaque key algorithm #1 |
| 249 | * \param alg1 input string opaque key algorithm #2 |
| 250 | * \param psa_alg1 output PSA algorithm #1 |
| 251 | * \param psa_alg2 output PSA algorithm #2 |
| 252 | * \param usage output key usage |
| 253 | * |
| 254 | * \return \c 0 on success. |
| 255 | * \return \c 1 on parse failure. |
| 256 | */ |
| 257 | int key_opaque_set_alg_usage( const char *alg1, const char *alg2, |
| 258 | psa_algorithm_t *psa_alg1, |
| 259 | psa_algorithm_t *psa_alg2, |
| 260 | psa_key_usage_t *usage ); |
Przemek Stekiel | 76a41f5 | 2022-05-04 13:55:23 +0200 | [diff] [blame] | 261 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemek Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 2146211 | 2021-01-13 23:53:09 +0100 | [diff] [blame] | 263 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 264 | /* The test implementation of the PSA external RNG is insecure. When |
| 265 | * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto |
| 266 | * function that makes use of an RNG, you must call |
| 267 | * mbedtls_test_enable_insecure_external_rng(). */ |
Gilles Peskine | 1af872d | 2021-01-20 20:02:01 +0100 | [diff] [blame] | 268 | #include <test/fake_external_rng_for_test.h> |
Gilles Peskine | 2146211 | 2021-01-13 23:53:09 +0100 | [diff] [blame] | 269 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 270 | |
| 271 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 272 | int ca_callback( void *data, mbedtls_x509_crt const *child, |
| 273 | mbedtls_x509_crt **candidates ); |
| 274 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 275 | |
| 276 | /* |
| 277 | * Test recv/send functions that make sure each try returns |
| 278 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 279 | */ |
| 280 | int delayed_recv( void *ctx, unsigned char *buf, size_t len ); |
| 281 | int delayed_send( void *ctx, const unsigned char *buf, size_t len ); |
| 282 | |
| 283 | /* |
| 284 | * Wait for an event from the underlying transport or the timer |
| 285 | * (Used in event-driven IO mode). |
| 286 | */ |
| 287 | int idle( mbedtls_net_context *fd, |
| 288 | #if defined(MBEDTLS_TIMING_C) |
| 289 | mbedtls_timing_delay_context *timer, |
| 290 | #endif |
| 291 | int idle_reason ); |
| 292 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 293 | #if defined(MBEDTLS_TEST_HOOKS) |
| 294 | /** Initialize whatever test hooks are enabled by the compile-time |
| 295 | * configuration and make sense for the TLS test programs. */ |
| 296 | void test_hooks_init( void ); |
| 297 | |
| 298 | /** Check if any test hooks detected a problem. |
| 299 | * |
Gilles Peskine | 00d0ad4 | 2021-02-15 11:02:51 +0100 | [diff] [blame] | 300 | * If a problem was detected, it's ok for the calling program to keep going, |
| 301 | * but it should ultimately exit with an error status. |
| 302 | * |
| 303 | * \note When implementing a test hook that detects errors on its own |
| 304 | * (as opposed to e.g. leaving the error for a memory sanitizer to |
| 305 | * report), make sure to print a message to standard error either at |
| 306 | * the time the problem is detected or during the execution of this |
| 307 | * function. This function does not indicate what problem was detected, |
| 308 | * so printing a message is the only way to provide feedback in the |
| 309 | * logs of the calling program. |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 310 | * |
| 311 | * \return Nonzero if a problem was detected. |
| 312 | * \c 0 if no problem was detected. |
| 313 | */ |
| 314 | int test_hooks_failure_detected( void ); |
| 315 | |
| 316 | /** Free any resources allocated for the sake of test hooks. |
| 317 | * |
| 318 | * Call this at the end of the program so that resource leak analyzers |
| 319 | * don't complain. |
| 320 | */ |
| 321 | void test_hooks_free( void ); |
| 322 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 323 | #endif /* !MBEDTLS_TEST_HOOKS */ |
| 324 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 325 | #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 326 | #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */ |