blob: 5cfa7d2327e6f67b1f2adf079f392b59cb0ea714 [file] [log] [blame]
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01001/*
2 * Common code for SSL test programs
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01006 */
7
8#ifndef MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H
9#define MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H
Ben Taylorf19a9002025-08-08 08:53:31 +010010#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
11
12#include "mbedtls/private/pk_private.h"
Gilles Peskinea3ed34f2021-01-05 21:11:16 +010013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010015
Gilles Peskineab7ce962021-01-05 21:27:53 +010016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010017#include "mbedtls/md.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010018
Gilles Peskine8133abd2021-02-08 21:20:12 +010019#undef HAVE_RNG
Ben Taylor0f214292025-06-06 08:31:48 +010020#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine8133abd2021-02-08 21:20:12 +010021#define HAVE_RNG
22#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
23#define HAVE_RNG
24#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +020025 (defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_512))
Gilles Peskine8133abd2021-02-08 21:20:12 +010026#define HAVE_RNG
27#endif
28
29#if !defined(MBEDTLS_NET_C) || \
Andrzej Kurek03e01462022-01-03 12:53:24 +010030 !defined(MBEDTLS_SSL_TLS_C)
31#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
32 "MBEDTLS_NET_C and/or " \
33 "MBEDTLS_SSL_TLS_C not defined."
Gilles Peskine8133abd2021-02-08 21:20:12 +010034#elif !defined(HAVE_RNG)
Andrzej Kurek03e01462022-01-03 12:53:24 +010035#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
Gilles Peskine8133abd2021-02-08 21:20:12 +010036 "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010037#else
38#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010039
Gilles Peskine8133abd2021-02-08 21:20:12 +010040#undef HAVE_RNG
41
Gilles Peskine67638d62021-01-05 21:36:29 +010042#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include "mbedtls/net_sockets.h"
47#include "mbedtls/ssl.h"
Glenn Strauss6eef5632022-01-23 08:37:02 -050048#include "mbedtls/ssl_ciphersuites.h"
Anton Matkinbc487252025-06-16 13:37:03 +020049#include "mbedtls/private/entropy.h"
50#include "mbedtls/private/ctr_drbg.h"
51#include "mbedtls/private/hmac_drbg.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010052#include "mbedtls/x509.h"
53#include "mbedtls/error.h"
54#include "mbedtls/debug.h"
55#include "mbedtls/timing.h"
56#include "mbedtls/base64.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010057#include "test/certs.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010058
Gilles Peskine67638d62021-01-05 21:36:29 +010059#include "psa/crypto.h"
60#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010061
Gilles Peskine67638d62021-01-05 21:36:29 +010062#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
63#include "mbedtls/memory_buffer_alloc.h"
64#endif
65
66#include <test/helpers.h>
67
Harry Ramseyc19b8e82025-02-11 14:14:00 +000068#include "query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010069
Przemek Stekielda4fba62023-06-02 14:52:28 +020070#define ALPN_LIST_SIZE 10
Przemek Stekiel45255e42023-06-29 13:56:36 +020071#define GROUP_LIST_SIZE 25
Przemek Stekielda4fba62023-06-02 14:52:28 +020072#define SIG_ALG_LIST_SIZE 5
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010075 unsigned char master_secret[48];
76 unsigned char randbytes[64];
77 mbedtls_tls_prf_types tls_prf_type;
78} eap_tls_keys;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +010081
82/* Supported SRTP mode needs a maximum of :
83 * - 16 bytes for key (AES-128)
84 * - 14 bytes SALT
85 * One for sender, one for receiver context
86 */
87#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010090 unsigned char master_secret[48];
91 unsigned char randbytes[64];
92 mbedtls_tls_prf_types tls_prf_type;
93} dtls_srtp_keys;
94
95#endif /* MBEDTLS_SSL_DTLS_SRTP */
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +010098 mbedtls_ssl_context *ssl;
99 mbedtls_net_context *net;
100} io_ctx_t;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void my_debug(void *ctx, int level,
103 const char *file, int line,
104 const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100105
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200106#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100107mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200108#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100109
Gilles Peskine8eb29432021-02-03 20:07:11 +0100110#define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
Gilles Peskine8eb29432021-02-03 20:07:11 +0100111
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100112/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114typedef struct {
Gilles Peskine8eb29432021-02-03 20:07:11 +0100115#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
116 unsigned char dummy;
117#else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100118 mbedtls_entropy_context entropy;
Gilles Peskineba749042021-01-13 20:02:03 +0100119#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100120 mbedtls_ctr_drbg_context drbg;
Gilles Peskineba749042021-01-13 20:02:03 +0100121#elif defined(MBEDTLS_HMAC_DRBG_C)
122 mbedtls_hmac_drbg_context drbg;
123#else
124#error "No DRBG available"
125#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100126#endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100127} rng_context_t;
128
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100129/** Initialize the RNG.
130 *
131 * This function only initializes the memory used by the RNG context.
132 * Before using the RNG, it must be seeded with rng_seed().
133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100135
136/* Seed the random number generator.
137 *
138 * \param rng The RNG context to use. It must have been initialized
139 * with rng_init().
140 * \param reproducible If zero, seed the RNG from entropy.
141 * If nonzero, use a fixed seed, so that the program
142 * will produce the same sequence of random numbers
143 * each time it is invoked.
144 * \param pers A null-terminated string. Different values for this
145 * string cause the RNG to emit different output for
146 * the same seed.
147 *
148 * return 0 on success, a negative value on error.
149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100151
152/** Deinitialize the RNG. Free any embedded resource.
153 *
154 * \param rng The RNG context to deinitialize. It must have been
155 * initialized with rng_init().
156 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100158
Gilles Peskine535fb372021-01-13 18:59:46 +0100159/** Generate random data.
160 *
161 * This function is suitable for use as the \c f_rng argument to Mbed TLS
162 * library functions.
163 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100164 * \param p_rng The random generator context. This must be a pointer to
165 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100166 * \param output The buffer to fill.
167 * \param output_len The length of the buffer in bytes.
168 *
169 * \return \c 0 on success.
170 * \return An Mbed TLS error code on error.
171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100173
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200174/** Parse command-line option: key_opaque_algs
175 *
176 *
177 * \param arg String value of key_opaque_algs
178 * Coma-separated pair of values among the following:
179 * - "rsa-sign-pkcs1"
180 * - "rsa-sign-pss"
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200181 * - "ecdsa-sign"
182 * - "ecdh"
183 * - "none" (only acceptable for the second value).
184 * \param alg1 Address of pointer to alg #1
185 * \param alg2 Address of pointer to alg #2
186 *
187 * \return \c 0 on success.
188 * \return \c 1 on parse failure.
189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2);
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200191
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400192/** Parse given opaque key algorithms to obtain psa algs and usage
Przemek Stekiel01396a12022-05-02 13:41:53 +0200193 * that will be passed to mbedtls_pk_wrap_as_opaque().
194 *
195 *
196 * \param alg1 input string opaque key algorithm #1
Przemek Stekielcb20d202022-05-06 08:42:34 +0200197 * \param alg2 input string opaque key algorithm #2
Przemek Stekiel01396a12022-05-02 13:41:53 +0200198 * \param psa_alg1 output PSA algorithm #1
199 * \param psa_alg2 output PSA algorithm #2
200 * \param usage output key usage
Przemek Stekielcb20d202022-05-06 08:42:34 +0200201 * \param key_type key type used to set default psa algorithm/usage
202 * when alg1 in "none"
Przemek Stekiel01396a12022-05-02 13:41:53 +0200203 *
204 * \return \c 0 on success.
205 * \return \c 1 on parse failure.
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
208 psa_algorithm_t *psa_alg1,
209 psa_algorithm_t *psa_alg2,
210 psa_key_usage_t *usage,
211 mbedtls_pk_type_t key_type);
Valerio Setti7541ebe2024-02-27 10:44:33 +0100212
Valerio Setti90eca2a2024-02-28 10:45:43 +0100213#if defined(MBEDTLS_PK_C)
Valerio Setti7541ebe2024-02-27 10:44:33 +0100214/** Turn a non-opaque PK context into an opaque one with folowing steps:
215 * - extract the key data and attributes from the PK context.
216 * - import the key material into PSA.
217 * - free the provided PK context and re-initilize it as an opaque PK context
218 * wrapping the PSA key imported in the above step.
219 *
Gilles Peskine2e5a7ea2025-02-12 23:11:09 +0100220 * \param[in,out] pk On input, the non-opaque PK context which contains the
221 * key to be wrapped. On output, the re-initialized PK
Valerio Setti7541ebe2024-02-27 10:44:33 +0100222 * context which represents the opaque version of the one
223 * provided as input.
224 * \param[in] psa_alg The primary algorithm that will be associated to the
225 * PSA key.
226 * \param[in] psa_alg2 The enrollment algorithm that will be associated to the
227 * PSA key.
228 * \param[in] psa_usage The PSA key usage policy.
229 * \param[out] key_id The PSA key identifier of the imported key.
230 *
231 * \return \c 0 on sucess.
232 * \return \c -1 on failure.
233 */
234int pk_wrap_as_opaque(mbedtls_pk_context *pk, psa_algorithm_t psa_alg, psa_algorithm_t psa_alg2,
235 psa_key_usage_t psa_usage, mbedtls_svc_key_id_t *key_id);
Valerio Setti90eca2a2024-02-28 10:45:43 +0100236#endif /* MBEDTLS_PK_C */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200237
Ben Taylor4bb98be2025-05-07 14:21:20 +0100238#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine21462112021-01-13 23:53:09 +0100239/* The test implementation of the PSA external RNG is insecure. When
240 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
241 * function that makes use of an RNG, you must call
242 * mbedtls_test_enable_insecure_external_rng(). */
Gilles Peskine1af872d2021-01-20 20:02:01 +0100243#include <test/fake_external_rng_for_test.h>
Gilles Peskine21462112021-01-13 23:53:09 +0100244#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100245
246#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int ca_callback(void *data, mbedtls_x509_crt const *child,
248 mbedtls_x509_crt **candidates);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100249#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
250
251/*
252 * Test recv/send functions that make sure each try returns
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800253 * WANT_READ/WANT_WRITE at least once before succeeding
Gilles Peskine504c1a32021-01-05 23:40:14 +0100254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255int delayed_recv(void *ctx, unsigned char *buf, size_t len);
256int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100257
258/*
259 * Wait for an event from the underlying transport or the timer
260 * (Used in event-driven IO mode).
261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int idle(mbedtls_net_context *fd,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100263#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_timing_delay_context *timer,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100265#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100267
Gilles Peskine53dea742021-02-02 22:55:06 +0100268#if defined(MBEDTLS_TEST_HOOKS)
269/** Initialize whatever test hooks are enabled by the compile-time
270 * configuration and make sense for the TLS test programs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100272
273/** Check if any test hooks detected a problem.
274 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100275 * If a problem was detected, it's ok for the calling program to keep going,
276 * but it should ultimately exit with an error status.
277 *
278 * \note When implementing a test hook that detects errors on its own
279 * (as opposed to e.g. leaving the error for a memory sanitizer to
280 * report), make sure to print a message to standard error either at
281 * the time the problem is detected or during the execution of this
282 * function. This function does not indicate what problem was detected,
283 * so printing a message is the only way to provide feedback in the
284 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100285 *
286 * \return Nonzero if a problem was detected.
287 * \c 0 if no problem was detected.
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100290
291/** Free any resources allocated for the sake of test hooks.
292 *
293 * Call this at the end of the program so that resource leak analyzers
294 * don't complain.
295 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100297
Gilles Peskine53dea742021-02-02 22:55:06 +0100298#endif /* !MBEDTLS_TEST_HOOKS */
299
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200300/* Helper functions for FFDH groups. */
Przemek Stekiel68e75442023-07-06 11:21:39 +0200301int parse_groups(const char *groups, uint16_t *group_list, size_t group_list_len);
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200302
Gilles Peskine504c1a32021-01-05 23:40:14 +0100303#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100304#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */