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 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if defined(MBEDTLS_PLATFORM_C) |
| 30 | #include "mbedtls/platform.h" |
| 31 | #else |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #define mbedtls_calloc calloc |
| 35 | #define mbedtls_free free |
| 36 | #define mbedtls_time time |
| 37 | #define mbedtls_time_t time_t |
| 38 | #define mbedtls_printf printf |
| 39 | #define mbedtls_fprintf fprintf |
| 40 | #define mbedtls_snprintf snprintf |
| 41 | #define mbedtls_exit exit |
| 42 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 43 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 44 | #endif |
| 45 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 46 | #if !defined(MBEDTLS_ENTROPY_C) || \ |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 47 | !defined(MBEDTLS_NET_C) || \ |
| 48 | !defined(MBEDTLS_SSL_TLS_C) || \ |
| 49 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 50 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 51 | "MBEDTLS_ENTROPY_C and/or " \ |
| 52 | "MBEDTLS_NET_C and/or " \ |
| 53 | "MBEDTLS_SSL_TLS_C not defined, " \ |
| 54 | "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n" |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 55 | #elif !( defined(MBEDTLS_CTR_DRBG_C) || \ |
| 56 | defined(MBEDTLS_HMAC_DRBG_C) && ( defined(MBEDTLS_SHA256_C) || \ |
| 57 | defined(MBEDTLS_SHA512_C) ) ) |
| 58 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
| 59 | "Neither MBEDTLS_CTR_DRBG_C, nor MBEDTLS_HMAC_DRBG_C and a supported hash defined.\n" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 60 | #else |
| 61 | #undef MBEDTLS_SSL_TEST_IMPOSSIBLE |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 62 | |
| 63 | #include <stdio.h> |
| 64 | #include <stdlib.h> |
| 65 | #include <string.h> |
| 66 | |
| 67 | #include "mbedtls/net_sockets.h" |
| 68 | #include "mbedtls/ssl.h" |
| 69 | #include "mbedtls/entropy.h" |
| 70 | #include "mbedtls/ctr_drbg.h" |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 71 | #include "mbedtls/hmac_drbg.h" |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 72 | #include "mbedtls/certs.h" |
| 73 | #include "mbedtls/x509.h" |
| 74 | #include "mbedtls/error.h" |
| 75 | #include "mbedtls/debug.h" |
| 76 | #include "mbedtls/timing.h" |
| 77 | #include "mbedtls/base64.h" |
| 78 | |
| 79 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 80 | #include "psa/crypto.h" |
| 81 | #include "mbedtls/psa_util.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 82 | #endif |
| 83 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 85 | #include "mbedtls/memory_buffer_alloc.h" |
| 86 | #endif |
| 87 | |
| 88 | #include <test/helpers.h> |
| 89 | |
Gilles Peskine | c772b18 | 2021-01-12 15:55:10 +0100 | [diff] [blame] | 90 | #include "../test/query_config.h" |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 91 | |
| 92 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 93 | |
| 94 | typedef struct eap_tls_keys |
| 95 | { |
| 96 | unsigned char master_secret[48]; |
| 97 | unsigned char randbytes[64]; |
| 98 | mbedtls_tls_prf_types tls_prf_type; |
| 99 | } eap_tls_keys; |
| 100 | |
| 101 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) |
| 102 | |
| 103 | /* Supported SRTP mode needs a maximum of : |
| 104 | * - 16 bytes for key (AES-128) |
| 105 | * - 14 bytes SALT |
| 106 | * One for sender, one for receiver context |
| 107 | */ |
| 108 | #define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60 |
| 109 | |
| 110 | typedef struct dtls_srtp_keys |
| 111 | { |
| 112 | unsigned char master_secret[48]; |
| 113 | unsigned char randbytes[64]; |
| 114 | mbedtls_tls_prf_types tls_prf_type; |
| 115 | } dtls_srtp_keys; |
| 116 | |
| 117 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 118 | |
| 119 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 120 | |
| 121 | typedef struct |
| 122 | { |
| 123 | mbedtls_ssl_context *ssl; |
| 124 | mbedtls_net_context *net; |
| 125 | } io_ctx_t; |
| 126 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 127 | void my_debug( void *ctx, int level, |
| 128 | const char *file, int line, |
| 129 | const char *str ); |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 130 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 131 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ); |
| 132 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 133 | /** A context for random number generation (RNG). |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 134 | */ |
| 135 | typedef struct |
| 136 | { |
| 137 | mbedtls_entropy_context entropy; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 138 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 139 | mbedtls_ctr_drbg_context drbg; |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 140 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 141 | mbedtls_hmac_drbg_context drbg; |
| 142 | #else |
| 143 | #error "No DRBG available" |
| 144 | #endif |
Gilles Peskine | 8a8492b | 2021-01-13 18:17:32 +0100 | [diff] [blame] | 145 | } rng_context_t; |
| 146 | |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 147 | /** Initialize the RNG. |
| 148 | * |
| 149 | * This function only initializes the memory used by the RNG context. |
| 150 | * Before using the RNG, it must be seeded with rng_seed(). |
| 151 | */ |
| 152 | void rng_init( rng_context_t *rng ); |
| 153 | |
| 154 | /* Seed the random number generator. |
| 155 | * |
| 156 | * \param rng The RNG context to use. It must have been initialized |
| 157 | * with rng_init(). |
| 158 | * \param reproducible If zero, seed the RNG from entropy. |
| 159 | * If nonzero, use a fixed seed, so that the program |
| 160 | * will produce the same sequence of random numbers |
| 161 | * each time it is invoked. |
| 162 | * \param pers A null-terminated string. Different values for this |
| 163 | * string cause the RNG to emit different output for |
| 164 | * the same seed. |
| 165 | * |
| 166 | * return 0 on success, a negative value on error. |
| 167 | */ |
| 168 | int rng_seed( rng_context_t *rng, int reproducible, const char *pers ); |
| 169 | |
| 170 | /** Deinitialize the RNG. Free any embedded resource. |
| 171 | * |
| 172 | * \param rng The RNG context to deinitialize. It must have been |
| 173 | * initialized with rng_init(). |
| 174 | */ |
| 175 | void rng_free( rng_context_t *rng ); |
| 176 | |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 177 | /** Generate random data. |
| 178 | * |
| 179 | * This function is suitable for use as the \c f_rng argument to Mbed TLS |
| 180 | * library functions. |
| 181 | * |
| 182 | * \param p_rng The CTR_DRBG context. This must be a pointer to a |
| 183 | * #rng_context_t structure. |
| 184 | * \param output The buffer to fill. |
| 185 | * \param output_len The length of the buffer in bytes. |
| 186 | * |
| 187 | * \return \c 0 on success. |
| 188 | * \return An Mbed TLS error code on error. |
| 189 | */ |
| 190 | int rng_get( void *p_rng, unsigned char *output, size_t output_len ); |
| 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 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 196 | |
| 197 | /* |
| 198 | * Test recv/send functions that make sure each try returns |
| 199 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 200 | */ |
| 201 | int delayed_recv( void *ctx, unsigned char *buf, size_t len ); |
| 202 | int delayed_send( void *ctx, const unsigned char *buf, size_t len ); |
| 203 | |
| 204 | /* |
| 205 | * Wait for an event from the underlying transport or the timer |
| 206 | * (Used in event-driven IO mode). |
| 207 | */ |
| 208 | int idle( mbedtls_net_context *fd, |
| 209 | #if defined(MBEDTLS_TIMING_C) |
| 210 | mbedtls_timing_delay_context *timer, |
| 211 | #endif |
| 212 | int idle_reason ); |
| 213 | |
| 214 | #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 215 | #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */ |