blob: fbb0efff84968ab4a0b8ba1b773dfc9287a60fbd [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
10
Bence Szépkútic662b362021-05-27 11:25:03 +020011#include "mbedtls/build_info.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010012
Gilles Peskineab7ce962021-01-05 21:27:53 +010013#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010014#include "mbedtls/md.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010015
Gilles Peskine8133abd2021-02-08 21:20:12 +010016#undef HAVE_RNG
Ben Taylor0f214292025-06-06 08:31:48 +010017#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine8133abd2021-02-08 21:20:12 +010018#define HAVE_RNG
19#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
20#define HAVE_RNG
21#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +020022 (defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_512))
Gilles Peskine8133abd2021-02-08 21:20:12 +010023#define HAVE_RNG
24#endif
25
26#if !defined(MBEDTLS_NET_C) || \
Andrzej Kurek03e01462022-01-03 12:53:24 +010027 !defined(MBEDTLS_SSL_TLS_C)
28#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
29 "MBEDTLS_NET_C and/or " \
30 "MBEDTLS_SSL_TLS_C not defined."
Gilles Peskine8133abd2021-02-08 21:20:12 +010031#elif !defined(HAVE_RNG)
Andrzej Kurek03e01462022-01-03 12:53:24 +010032#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
Gilles Peskine8133abd2021-02-08 21:20:12 +010033 "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010034#else
35#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010036
Gilles Peskine8133abd2021-02-08 21:20:12 +010037#undef HAVE_RNG
38
Gilles Peskine67638d62021-01-05 21:36:29 +010039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42
43#include "mbedtls/net_sockets.h"
44#include "mbedtls/ssl.h"
Glenn Strauss6eef5632022-01-23 08:37:02 -050045#include "mbedtls/ssl_ciphersuites.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010046#include "mbedtls/entropy.h"
47#include "mbedtls/ctr_drbg.h"
Gilles Peskineba749042021-01-13 20:02:03 +010048#include "mbedtls/hmac_drbg.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010049#include "mbedtls/x509.h"
50#include "mbedtls/error.h"
51#include "mbedtls/debug.h"
52#include "mbedtls/timing.h"
53#include "mbedtls/base64.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010054#include "test/certs.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010055
Gilles Peskine67638d62021-01-05 21:36:29 +010056#include "psa/crypto.h"
57#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010058
Gilles Peskine67638d62021-01-05 21:36:29 +010059#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
60#include "mbedtls/memory_buffer_alloc.h"
61#endif
62
63#include <test/helpers.h>
64
Harry Ramseyc19b8e82025-02-11 14:14:00 +000065#include "query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010066
Przemek Stekielda4fba62023-06-02 14:52:28 +020067#define ALPN_LIST_SIZE 10
Przemek Stekiel45255e42023-06-29 13:56:36 +020068#define GROUP_LIST_SIZE 25
Przemek Stekielda4fba62023-06-02 14:52:28 +020069#define SIG_ALG_LIST_SIZE 5
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010072 unsigned char master_secret[48];
73 unsigned char randbytes[64];
74 mbedtls_tls_prf_types tls_prf_type;
75} eap_tls_keys;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +010078
79/* Supported SRTP mode needs a maximum of :
80 * - 16 bytes for key (AES-128)
81 * - 14 bytes SALT
82 * One for sender, one for receiver context
83 */
84#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010087 unsigned char master_secret[48];
88 unsigned char randbytes[64];
89 mbedtls_tls_prf_types tls_prf_type;
90} dtls_srtp_keys;
91
92#endif /* MBEDTLS_SSL_DTLS_SRTP */
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +010095 mbedtls_ssl_context *ssl;
96 mbedtls_net_context *net;
97} io_ctx_t;
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099void my_debug(void *ctx, int level,
100 const char *file, int line,
101 const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100102
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200103#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200105#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100106
Ben Taylor4bb98be2025-05-07 14:21:20 +0100107#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine8eb29432021-02-03 20:07:11 +0100108/* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use
109 * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator.
110 *
111 * The constraints are:
112 * - Without the entropy module, the PSA RNG is the only option.
113 * - Without at least one of the DRBG modules, the PSA RNG is the only option.
114 * - The PSA RNG does not support explicit seeding, so it is incompatible with
115 * the reproducible mode used by test programs.
116 * - For good overall test coverage, there should be at least one configuration
117 * where the test programs use the PSA RNG while the PSA RNG is itself based
118 * on entropy+DRBG, and at least one configuration where the test programs
119 * do not use the PSA RNG even though it's there.
Gilles Peskine8eb29432021-02-03 20:07:11 +0100120 */
121#define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
122#endif
123
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100124/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126typedef struct {
Gilles Peskine8eb29432021-02-03 20:07:11 +0100127#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
128 unsigned char dummy;
129#else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100130 mbedtls_entropy_context entropy;
Gilles Peskineba749042021-01-13 20:02:03 +0100131#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100132 mbedtls_ctr_drbg_context drbg;
Gilles Peskineba749042021-01-13 20:02:03 +0100133#elif defined(MBEDTLS_HMAC_DRBG_C)
134 mbedtls_hmac_drbg_context drbg;
135#else
136#error "No DRBG available"
137#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100138#endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100139} rng_context_t;
140
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100141/** Initialize the RNG.
142 *
143 * This function only initializes the memory used by the RNG context.
144 * Before using the RNG, it must be seeded with rng_seed().
145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100147
148/* Seed the random number generator.
149 *
150 * \param rng The RNG context to use. It must have been initialized
151 * with rng_init().
152 * \param reproducible If zero, seed the RNG from entropy.
153 * If nonzero, use a fixed seed, so that the program
154 * will produce the same sequence of random numbers
155 * each time it is invoked.
156 * \param pers A null-terminated string. Different values for this
157 * string cause the RNG to emit different output for
158 * the same seed.
159 *
160 * return 0 on success, a negative value on error.
161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100163
164/** Deinitialize the RNG. Free any embedded resource.
165 *
166 * \param rng The RNG context to deinitialize. It must have been
167 * initialized with rng_init().
168 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100170
Gilles Peskine535fb372021-01-13 18:59:46 +0100171/** Generate random data.
172 *
173 * This function is suitable for use as the \c f_rng argument to Mbed TLS
174 * library functions.
175 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100176 * \param p_rng The random generator context. This must be a pointer to
177 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100178 * \param output The buffer to fill.
179 * \param output_len The length of the buffer in bytes.
180 *
181 * \return \c 0 on success.
182 * \return An Mbed TLS error code on error.
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100185
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200186/** Parse command-line option: key_opaque_algs
187 *
188 *
189 * \param arg String value of key_opaque_algs
190 * Coma-separated pair of values among the following:
191 * - "rsa-sign-pkcs1"
192 * - "rsa-sign-pss"
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200193 * - "ecdsa-sign"
194 * - "ecdh"
195 * - "none" (only acceptable for the second value).
196 * \param alg1 Address of pointer to alg #1
197 * \param alg2 Address of pointer to alg #2
198 *
199 * \return \c 0 on success.
200 * \return \c 1 on parse failure.
201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2);
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200203
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400204/** Parse given opaque key algorithms to obtain psa algs and usage
Przemek Stekiel01396a12022-05-02 13:41:53 +0200205 * that will be passed to mbedtls_pk_wrap_as_opaque().
206 *
207 *
208 * \param alg1 input string opaque key algorithm #1
Przemek Stekielcb20d202022-05-06 08:42:34 +0200209 * \param alg2 input string opaque key algorithm #2
Przemek Stekiel01396a12022-05-02 13:41:53 +0200210 * \param psa_alg1 output PSA algorithm #1
211 * \param psa_alg2 output PSA algorithm #2
212 * \param usage output key usage
Przemek Stekielcb20d202022-05-06 08:42:34 +0200213 * \param key_type key type used to set default psa algorithm/usage
214 * when alg1 in "none"
Przemek Stekiel01396a12022-05-02 13:41:53 +0200215 *
216 * \return \c 0 on success.
217 * \return \c 1 on parse failure.
218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
220 psa_algorithm_t *psa_alg1,
221 psa_algorithm_t *psa_alg2,
222 psa_key_usage_t *usage,
223 mbedtls_pk_type_t key_type);
Valerio Setti7541ebe2024-02-27 10:44:33 +0100224
Valerio Setti90eca2a2024-02-28 10:45:43 +0100225#if defined(MBEDTLS_PK_C)
Valerio Setti7541ebe2024-02-27 10:44:33 +0100226/** Turn a non-opaque PK context into an opaque one with folowing steps:
227 * - extract the key data and attributes from the PK context.
228 * - import the key material into PSA.
229 * - free the provided PK context and re-initilize it as an opaque PK context
230 * wrapping the PSA key imported in the above step.
231 *
Gilles Peskine2e5a7ea2025-02-12 23:11:09 +0100232 * \param[in,out] pk On input, the non-opaque PK context which contains the
233 * key to be wrapped. On output, the re-initialized PK
Valerio Setti7541ebe2024-02-27 10:44:33 +0100234 * context which represents the opaque version of the one
235 * provided as input.
236 * \param[in] psa_alg The primary algorithm that will be associated to the
237 * PSA key.
238 * \param[in] psa_alg2 The enrollment algorithm that will be associated to the
239 * PSA key.
240 * \param[in] psa_usage The PSA key usage policy.
241 * \param[out] key_id The PSA key identifier of the imported key.
242 *
243 * \return \c 0 on sucess.
244 * \return \c -1 on failure.
245 */
246int pk_wrap_as_opaque(mbedtls_pk_context *pk, psa_algorithm_t psa_alg, psa_algorithm_t psa_alg2,
247 psa_key_usage_t psa_usage, mbedtls_svc_key_id_t *key_id);
Valerio Setti90eca2a2024-02-28 10:45:43 +0100248#endif /* MBEDTLS_PK_C */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200249
Ben Taylor4bb98be2025-05-07 14:21:20 +0100250#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine21462112021-01-13 23:53:09 +0100251/* The test implementation of the PSA external RNG is insecure. When
252 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
253 * function that makes use of an RNG, you must call
254 * mbedtls_test_enable_insecure_external_rng(). */
Gilles Peskine1af872d2021-01-20 20:02:01 +0100255#include <test/fake_external_rng_for_test.h>
Gilles Peskine21462112021-01-13 23:53:09 +0100256#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100257
258#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int ca_callback(void *data, mbedtls_x509_crt const *child,
260 mbedtls_x509_crt **candidates);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100261#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
262
263/*
264 * Test recv/send functions that make sure each try returns
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800265 * WANT_READ/WANT_WRITE at least once before succeeding
Gilles Peskine504c1a32021-01-05 23:40:14 +0100266 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267int delayed_recv(void *ctx, unsigned char *buf, size_t len);
268int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100269
270/*
271 * Wait for an event from the underlying transport or the timer
272 * (Used in event-driven IO mode).
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int idle(mbedtls_net_context *fd,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100275#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 mbedtls_timing_delay_context *timer,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100277#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100279
Gilles Peskine53dea742021-02-02 22:55:06 +0100280#if defined(MBEDTLS_TEST_HOOKS)
281/** Initialize whatever test hooks are enabled by the compile-time
282 * configuration and make sense for the TLS test programs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100284
285/** Check if any test hooks detected a problem.
286 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100287 * If a problem was detected, it's ok for the calling program to keep going,
288 * but it should ultimately exit with an error status.
289 *
290 * \note When implementing a test hook that detects errors on its own
291 * (as opposed to e.g. leaving the error for a memory sanitizer to
292 * report), make sure to print a message to standard error either at
293 * the time the problem is detected or during the execution of this
294 * function. This function does not indicate what problem was detected,
295 * so printing a message is the only way to provide feedback in the
296 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100297 *
298 * \return Nonzero if a problem was detected.
299 * \c 0 if no problem was detected.
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100302
303/** Free any resources allocated for the sake of test hooks.
304 *
305 * Call this at the end of the program so that resource leak analyzers
306 * don't complain.
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100309
Gilles Peskine53dea742021-02-02 22:55:06 +0100310#endif /* !MBEDTLS_TEST_HOOKS */
311
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200312/* Helper functions for FFDH groups. */
Przemek Stekiel68e75442023-07-06 11:21:39 +0200313int parse_groups(const char *groups, uint16_t *group_list, size_t group_list_len);
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200314
Gilles Peskine504c1a32021-01-05 23:40:14 +0100315#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100316#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */