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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H |
| 9 | #define MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H |
| 10 | |
Yanray Wang | d818c08 | 2022-11-10 16:30:57 +0800 | [diff] [blame] | 11 | #include "mbedtls/version.h" |
| 12 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 13 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 14 | #include "mbedtls/config.h" |
| 15 | #else |
| 16 | #include MBEDTLS_CONFIG_FILE |
| 17 | #endif |
| 18 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 19 | #include "mbedtls/platform.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 20 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 21 | #undef HAVE_RNG |
| 22 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 23 | (defined(MBEDTLS_USE_PSA_CRYPTO) || \ |
| 24 | defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)) |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 25 | #define HAVE_RNG |
| 26 | #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) |
| 27 | #define HAVE_RNG |
| 28 | #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | (defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C)) |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 30 | #define HAVE_RNG |
| 31 | #endif |
| 32 | |
| 33 | #if !defined(MBEDTLS_NET_C) || \ |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 34 | !defined(MBEDTLS_SSL_TLS_C) || \ |
| 35 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 36 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 37 | "MBEDTLS_NET_C and/or " \ |
| 38 | "MBEDTLS_SSL_TLS_C not defined, " \ |
| 39 | "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n" |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 40 | #elif !defined(HAVE_RNG) |
| 41 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
| 42 | "No random generator is available.\n" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 43 | #else |
| 44 | #undef MBEDTLS_SSL_TEST_IMPOSSIBLE |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 45 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 46 | #undef HAVE_RNG |
| 47 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <string.h> |
| 51 | |
| 52 | #include "mbedtls/net_sockets.h" |
| 53 | #include "mbedtls/ssl.h" |
| 54 | #include "mbedtls/entropy.h" |
| 55 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 56 | #include "mbedtls/hmac_drbg.h" |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 57 | #include "mbedtls/certs.h" |
| 58 | #include "mbedtls/x509.h" |
| 59 | #include "mbedtls/error.h" |
| 60 | #include "mbedtls/debug.h" |
| 61 | #include "mbedtls/timing.h" |
| 62 | #include "mbedtls/base64.h" |
| 63 | |
Gilles Peskine | 8133abd | 2021-02-08 21:20:12 +0100 | [diff] [blame] | 64 | #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] | 65 | #include "psa/crypto.h" |
| 66 | #include "mbedtls/psa_util.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 69 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 70 | #include "mbedtls/memory_buffer_alloc.h" |
| 71 | #endif |
| 72 | |
| 73 | #include <test/helpers.h> |
| 74 | |
Gilles Peskine | c772b18 | 2021-01-12 15:55:10 +0100 | [diff] [blame] | 75 | #include "../test/query_config.h" |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 76 | |
| 77 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | typedef struct eap_tls_keys { |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 80 | unsigned char master_secret[48]; |
| 81 | unsigned char randbytes[64]; |
| 82 | mbedtls_tls_prf_types tls_prf_type; |
| 83 | } eap_tls_keys; |
| 84 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 86 | |
| 87 | /* Supported SRTP mode needs a maximum of : |
| 88 | * - 16 bytes for key (AES-128) |
| 89 | * - 14 bytes SALT |
| 90 | * One for sender, one for receiver context |
| 91 | */ |
| 92 | #define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60 |
| 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | typedef struct dtls_srtp_keys { |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 95 | unsigned char master_secret[48]; |
| 96 | unsigned char randbytes[64]; |
| 97 | mbedtls_tls_prf_types tls_prf_type; |
| 98 | } dtls_srtp_keys; |
| 99 | |
| 100 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 101 | |
| 102 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | typedef struct { |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 105 | mbedtls_ssl_context *ssl; |
| 106 | mbedtls_net_context *net; |
| 107 | } io_ctx_t; |
| 108 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | void my_debug(void *ctx, int level, |
| 110 | const char *file, int line, |
| 111 | const char *str); |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 112 | |
Raoul Strackx | 2db000f | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 113 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | mbedtls_time_t dummy_constant_time(mbedtls_time_t *time); |
Raoul Strackx | 2db000f | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 115 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 116 | |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 117 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 118 | /* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use |
| 119 | * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator. |
| 120 | * |
| 121 | * The constraints are: |
| 122 | * - Without the entropy module, the PSA RNG is the only option. |
| 123 | * - Without at least one of the DRBG modules, the PSA RNG is the only option. |
| 124 | * - The PSA RNG does not support explicit seeding, so it is incompatible with |
| 125 | * the reproducible mode used by test programs. |
| 126 | * - For good overall test coverage, there should be at least one configuration |
| 127 | * where the test programs use the PSA RNG while the PSA RNG is itself based |
| 128 | * on entropy+DRBG, and at least one configuration where the test programs |
| 129 | * do not use the PSA RNG even though it's there. |
| 130 | * |
| 131 | * A simple choice that meets the constraints is to use the PSA RNG whenever |
| 132 | * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the |
| 133 | * choice to use the PSA RNG in the test programs and the choice to use |
| 134 | * PSA crypto when TLS code needs crypto have to be tied together, but it |
| 135 | * happens to be a good match. It's also a good match from an application |
| 136 | * perspective: either PSA is preferred for TLS (both for crypto and for |
| 137 | * random generation) or it isn't. |
| 138 | */ |
| 139 | #define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG |
| 140 | #endif |
| 141 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 142 | /** A context for random number generation (RNG). |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 143 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | typedef struct { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 145 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 146 | unsigned char dummy; |
| 147 | #else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 148 | mbedtls_entropy_context entropy; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 149 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 150 | mbedtls_ctr_drbg_context drbg; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 151 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 152 | mbedtls_hmac_drbg_context drbg; |
| 153 | #else |
| 154 | #error "No DRBG available" |
| 155 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 156 | #endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 157 | } rng_context_t; |
| 158 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 159 | /** Initialize the RNG. |
| 160 | * |
| 161 | * This function only initializes the memory used by the RNG context. |
| 162 | * Before using the RNG, it must be seeded with rng_seed(). |
| 163 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | void rng_init(rng_context_t *rng); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 165 | |
| 166 | /* Seed the random number generator. |
| 167 | * |
| 168 | * \param rng The RNG context to use. It must have been initialized |
| 169 | * with rng_init(). |
| 170 | * \param reproducible If zero, seed the RNG from entropy. |
| 171 | * If nonzero, use a fixed seed, so that the program |
| 172 | * will produce the same sequence of random numbers |
| 173 | * each time it is invoked. |
| 174 | * \param pers A null-terminated string. Different values for this |
| 175 | * string cause the RNG to emit different output for |
| 176 | * the same seed. |
| 177 | * |
| 178 | * return 0 on success, a negative value on error. |
| 179 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | int rng_seed(rng_context_t *rng, int reproducible, const char *pers); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 181 | |
| 182 | /** Deinitialize the RNG. Free any embedded resource. |
| 183 | * |
| 184 | * \param rng The RNG context to deinitialize. It must have been |
| 185 | * initialized with rng_init(). |
| 186 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | void rng_free(rng_context_t *rng); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 188 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 189 | /** Generate random data. |
| 190 | * |
| 191 | * This function is suitable for use as the \c f_rng argument to Mbed TLS |
| 192 | * library functions. |
| 193 | * |
Gilles Peskine | da9529f | 2021-01-25 13:42:42 +0100 | [diff] [blame] | 194 | * \param p_rng The random generator context. This must be a pointer to |
| 195 | * a #rng_context_t structure. |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 196 | * \param output The buffer to fill. |
| 197 | * \param output_len The length of the buffer in bytes. |
| 198 | * |
| 199 | * \return \c 0 on success. |
| 200 | * \return An Mbed TLS error code on error. |
| 201 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | int rng_get(void *p_rng, unsigned char *output, size_t output_len); |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 203 | |
Gilles Peskine | 2146211 | 2021-01-13 23:53:09 +0100 | [diff] [blame] | 204 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 205 | /* The test implementation of the PSA external RNG is insecure. When |
| 206 | * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto |
| 207 | * function that makes use of an RNG, you must call |
| 208 | * mbedtls_test_enable_insecure_external_rng(). */ |
Gilles Peskine | 1af872d | 2021-01-20 20:02:01 +0100 | [diff] [blame] | 209 | #include <test/fake_external_rng_for_test.h> |
Gilles Peskine | 2146211 | 2021-01-13 23:53:09 +0100 | [diff] [blame] | 210 | #endif |
| 211 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 212 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | int ca_callback(void *data, mbedtls_x509_crt const *child, |
| 214 | mbedtls_x509_crt **candidates); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 215 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 216 | |
| 217 | /* |
| 218 | * Test recv/send functions that make sure each try returns |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 219 | * WANT_READ/WANT_WRITE at least once before succeeding |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 220 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | int delayed_recv(void *ctx, unsigned char *buf, size_t len); |
| 222 | int delayed_send(void *ctx, const unsigned char *buf, size_t len); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * Wait for an event from the underlying transport or the timer |
| 226 | * (Used in event-driven IO mode). |
| 227 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | int idle(mbedtls_net_context *fd, |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 229 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | mbedtls_timing_delay_context *timer, |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 231 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | int idle_reason); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 233 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 234 | #if defined(MBEDTLS_TEST_HOOKS) |
| 235 | /** Initialize whatever test hooks are enabled by the compile-time |
| 236 | * configuration and make sense for the TLS test programs. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 237 | void test_hooks_init(void); |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 238 | |
| 239 | /** Check if any test hooks detected a problem. |
| 240 | * |
Gilles Peskine | 00d0ad4 | 2021-02-15 11:02:51 +0100 | [diff] [blame] | 241 | * If a problem was detected, it's ok for the calling program to keep going, |
| 242 | * but it should ultimately exit with an error status. |
| 243 | * |
| 244 | * \note When implementing a test hook that detects errors on its own |
| 245 | * (as opposed to e.g. leaving the error for a memory sanitizer to |
| 246 | * report), make sure to print a message to standard error either at |
| 247 | * the time the problem is detected or during the execution of this |
| 248 | * function. This function does not indicate what problem was detected, |
| 249 | * so printing a message is the only way to provide feedback in the |
| 250 | * logs of the calling program. |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 251 | * |
| 252 | * \return Nonzero if a problem was detected. |
| 253 | * \c 0 if no problem was detected. |
| 254 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 255 | int test_hooks_failure_detected(void); |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 256 | |
| 257 | /** Free any resources allocated for the sake of test hooks. |
| 258 | * |
| 259 | * Call this at the end of the program so that resource leak analyzers |
| 260 | * don't complain. |
| 261 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | void test_hooks_free(void); |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 263 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 264 | #endif /* !MBEDTLS_TEST_HOOKS */ |
| 265 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 266 | #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 267 | #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */ |