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 | |
| 46 | #if !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 47 | !defined(MBEDTLS_ENTROPY_C) || \ |
| 48 | !defined(MBEDTLS_NET_C) || \ |
| 49 | !defined(MBEDTLS_SSL_TLS_C) || \ |
| 50 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) |
| 51 | #define MBEDTLS_SSL_TEST_IMPOSSIBLE \ |
| 52 | "MBEDTLS_CTR_DRBG_C and/or " \ |
| 53 | "MBEDTLS_ENTROPY_C and/or " \ |
| 54 | "MBEDTLS_NET_C and/or " \ |
| 55 | "MBEDTLS_SSL_TLS_C not defined, " \ |
| 56 | "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n" |
| 57 | #else |
| 58 | #undef MBEDTLS_SSL_TEST_IMPOSSIBLE |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 59 | |
| 60 | #include <stdio.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
| 63 | |
| 64 | #include "mbedtls/net_sockets.h" |
| 65 | #include "mbedtls/ssl.h" |
| 66 | #include "mbedtls/entropy.h" |
| 67 | #include "mbedtls/ctr_drbg.h" |
| 68 | #include "mbedtls/certs.h" |
| 69 | #include "mbedtls/x509.h" |
| 70 | #include "mbedtls/error.h" |
| 71 | #include "mbedtls/debug.h" |
| 72 | #include "mbedtls/timing.h" |
| 73 | #include "mbedtls/base64.h" |
| 74 | |
| 75 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 76 | #include "psa/crypto.h" |
| 77 | #include "mbedtls/psa_util.h" |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 78 | #endif |
| 79 | |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 80 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 81 | #include "mbedtls/memory_buffer_alloc.h" |
| 82 | #endif |
| 83 | |
| 84 | #include <test/helpers.h> |
| 85 | |
Gilles Peskine | 7f679c7 | 2021-01-05 21:41:27 +0100 | [diff] [blame] | 86 | /** Check whether a given configuration symbol is enabled. |
| 87 | * |
| 88 | * \param config The symbol to query (e.g. "MBEDTLS_RSA_C"). |
| 89 | * \return \c 0 if the symbol was defined at compile time |
| 90 | * (in MBEDTLS_CONFIG_FILE or config.h), |
| 91 | * \c 1 otherwise. |
| 92 | * |
| 93 | * \note This function is defined in `programs/test/query_config.c` |
| 94 | * which is automatically generated by |
| 95 | * `scripts/generate_query_config.pl`. |
| 96 | */ |
| 97 | int query_config( const char *config ); |
| 98 | |
Gilles Peskine | 7c818d6 | 2021-01-05 22:33:13 +0100 | [diff] [blame] | 99 | |
| 100 | |
| 101 | #if defined(MBEDTLS_SSL_EXPORT_KEYS) |
| 102 | |
| 103 | typedef struct eap_tls_keys |
| 104 | { |
| 105 | unsigned char master_secret[48]; |
| 106 | unsigned char randbytes[64]; |
| 107 | mbedtls_tls_prf_types tls_prf_type; |
| 108 | } eap_tls_keys; |
| 109 | |
| 110 | #if defined( MBEDTLS_SSL_DTLS_SRTP ) |
| 111 | |
| 112 | /* Supported SRTP mode needs a maximum of : |
| 113 | * - 16 bytes for key (AES-128) |
| 114 | * - 14 bytes SALT |
| 115 | * One for sender, one for receiver context |
| 116 | */ |
| 117 | #define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60 |
| 118 | |
| 119 | typedef struct dtls_srtp_keys |
| 120 | { |
| 121 | unsigned char master_secret[48]; |
| 122 | unsigned char randbytes[64]; |
| 123 | mbedtls_tls_prf_types tls_prf_type; |
| 124 | } dtls_srtp_keys; |
| 125 | |
| 126 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 127 | |
| 128 | #endif /* MBEDTLS_SSL_EXPORT_KEYS */ |
| 129 | |
| 130 | typedef struct |
| 131 | { |
| 132 | mbedtls_ssl_context *ssl; |
| 133 | mbedtls_net_context *net; |
| 134 | } io_ctx_t; |
| 135 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame^] | 136 | void my_debug( void *ctx, int level, |
| 137 | const char *file, int line, |
| 138 | const char *str ); |
Gilles Peskine | 67638d6 | 2021-01-05 21:36:29 +0100 | [diff] [blame] | 139 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame^] | 140 | mbedtls_time_t dummy_constant_time( mbedtls_time_t* time ); |
| 141 | |
| 142 | int dummy_entropy( void *data, unsigned char *output, size_t len ); |
| 143 | |
| 144 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 145 | int ca_callback( void *data, mbedtls_x509_crt const *child, |
| 146 | mbedtls_x509_crt **candidates ); |
| 147 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 148 | |
| 149 | /* |
| 150 | * Test recv/send functions that make sure each try returns |
| 151 | * WANT_READ/WANT_WRITE at least once before sucesseding |
| 152 | */ |
| 153 | int delayed_recv( void *ctx, unsigned char *buf, size_t len ); |
| 154 | int delayed_send( void *ctx, const unsigned char *buf, size_t len ); |
| 155 | |
| 156 | /* |
| 157 | * Wait for an event from the underlying transport or the timer |
| 158 | * (Used in event-driven IO mode). |
| 159 | */ |
| 160 | int idle( mbedtls_net_context *fd, |
| 161 | #if defined(MBEDTLS_TIMING_C) |
| 162 | mbedtls_timing_delay_context *timer, |
| 163 | #endif |
| 164 | int idle_reason ); |
| 165 | |
| 166 | #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */ |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 167 | #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */ |