blob: 6602b1ae21ed7052642c067fb68229906f27e60d [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
11#include "mbedtls/private/pk_private.h"
Gilles Peskinea3ed34f2021-01-05 21:11:16 +010012
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010014
Gilles Peskineab7ce962021-01-05 21:27:53 +010015#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010016#include "mbedtls/md.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010017
Gilles Peskine8133abd2021-02-08 21:20:12 +010018#undef HAVE_RNG
Ben Taylor0f214292025-06-06 08:31:48 +010019#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine8133abd2021-02-08 21:20:12 +010020#define HAVE_RNG
21#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
22#define HAVE_RNG
23#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +020024 (defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_512))
Gilles Peskine8133abd2021-02-08 21:20:12 +010025#define HAVE_RNG
26#endif
27
28#if !defined(MBEDTLS_NET_C) || \
Andrzej Kurek03e01462022-01-03 12:53:24 +010029 !defined(MBEDTLS_SSL_TLS_C)
30#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
31 "MBEDTLS_NET_C and/or " \
32 "MBEDTLS_SSL_TLS_C not defined."
Gilles Peskine8133abd2021-02-08 21:20:12 +010033#elif !defined(HAVE_RNG)
Andrzej Kurek03e01462022-01-03 12:53:24 +010034#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
Gilles Peskine8133abd2021-02-08 21:20:12 +010035 "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010036#else
37#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010038
Gilles Peskine8133abd2021-02-08 21:20:12 +010039#undef HAVE_RNG
40
Gilles Peskine67638d62021-01-05 21:36:29 +010041#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include "mbedtls/net_sockets.h"
46#include "mbedtls/ssl.h"
Glenn Strauss6eef5632022-01-23 08:37:02 -050047#include "mbedtls/ssl_ciphersuites.h"
Anton Matkinbc487252025-06-16 13:37:03 +020048#include "mbedtls/private/entropy.h"
49#include "mbedtls/private/ctr_drbg.h"
50#include "mbedtls/private/hmac_drbg.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010051#include "mbedtls/x509.h"
52#include "mbedtls/error.h"
53#include "mbedtls/debug.h"
54#include "mbedtls/timing.h"
55#include "mbedtls/base64.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010056#include "test/certs.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010057
Gilles Peskine67638d62021-01-05 21:36:29 +010058#include "psa/crypto.h"
59#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010060
Gilles Peskine67638d62021-01-05 21:36:29 +010061#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
62#include "mbedtls/memory_buffer_alloc.h"
63#endif
64
65#include <test/helpers.h>
66
Harry Ramseyc19b8e82025-02-11 14:14:00 +000067#include "query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010068
Przemek Stekielda4fba62023-06-02 14:52:28 +020069#define ALPN_LIST_SIZE 10
Przemek Stekiel45255e42023-06-29 13:56:36 +020070#define GROUP_LIST_SIZE 25
Przemek Stekielda4fba62023-06-02 14:52:28 +020071#define SIG_ALG_LIST_SIZE 5
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010074 unsigned char master_secret[48];
75 unsigned char randbytes[64];
76 mbedtls_tls_prf_types tls_prf_type;
77} eap_tls_keys;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +010080
81/* Supported SRTP mode needs a maximum of :
82 * - 16 bytes for key (AES-128)
83 * - 14 bytes SALT
84 * One for sender, one for receiver context
85 */
86#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010089 unsigned char master_secret[48];
90 unsigned char randbytes[64];
91 mbedtls_tls_prf_types tls_prf_type;
92} dtls_srtp_keys;
93
94#endif /* MBEDTLS_SSL_DTLS_SRTP */
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +010097 mbedtls_ssl_context *ssl;
98 mbedtls_net_context *net;
99} io_ctx_t;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void my_debug(void *ctx, int level,
102 const char *file, int line,
103 const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100104
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200105#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100106mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200107#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100108
Gilles Peskine8eb29432021-02-03 20:07:11 +0100109#define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
Gilles Peskine8eb29432021-02-03 20:07:11 +0100110
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100111/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113typedef struct {
Gilles Peskine8eb29432021-02-03 20:07:11 +0100114#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
115 unsigned char dummy;
116#else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100117 mbedtls_entropy_context entropy;
Gilles Peskineba749042021-01-13 20:02:03 +0100118#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100119 mbedtls_ctr_drbg_context drbg;
Gilles Peskineba749042021-01-13 20:02:03 +0100120#elif defined(MBEDTLS_HMAC_DRBG_C)
121 mbedtls_hmac_drbg_context drbg;
122#else
123#error "No DRBG available"
124#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100125#endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100126} rng_context_t;
127
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100128/** Initialize the RNG.
129 *
130 * This function only initializes the memory used by the RNG context.
131 * Before using the RNG, it must be seeded with rng_seed().
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100134
135/* Seed the random number generator.
136 *
137 * \param rng The RNG context to use. It must have been initialized
138 * with rng_init().
139 * \param reproducible If zero, seed the RNG from entropy.
140 * If nonzero, use a fixed seed, so that the program
141 * will produce the same sequence of random numbers
142 * each time it is invoked.
143 * \param pers A null-terminated string. Different values for this
144 * string cause the RNG to emit different output for
145 * the same seed.
146 *
147 * return 0 on success, a negative value on error.
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100150
151/** Deinitialize the RNG. Free any embedded resource.
152 *
153 * \param rng The RNG context to deinitialize. It must have been
154 * initialized with rng_init().
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100157
Gilles Peskine535fb372021-01-13 18:59:46 +0100158/** Generate random data.
159 *
160 * This function is suitable for use as the \c f_rng argument to Mbed TLS
161 * library functions.
162 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100163 * \param p_rng The random generator context. This must be a pointer to
164 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100165 * \param output The buffer to fill.
166 * \param output_len The length of the buffer in bytes.
167 *
168 * \return \c 0 on success.
169 * \return An Mbed TLS error code on error.
170 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100172
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200173/** Parse command-line option: key_opaque_algs
174 *
175 *
176 * \param arg String value of key_opaque_algs
177 * Coma-separated pair of values among the following:
178 * - "rsa-sign-pkcs1"
179 * - "rsa-sign-pss"
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200180 * - "ecdsa-sign"
181 * - "ecdh"
182 * - "none" (only acceptable for the second value).
183 * \param alg1 Address of pointer to alg #1
184 * \param alg2 Address of pointer to alg #2
185 *
186 * \return \c 0 on success.
187 * \return \c 1 on parse failure.
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2);
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200190
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400191/** Parse given opaque key algorithms to obtain psa algs and usage
Przemek Stekiel01396a12022-05-02 13:41:53 +0200192 * that will be passed to mbedtls_pk_wrap_as_opaque().
193 *
194 *
195 * \param alg1 input string opaque key algorithm #1
Przemek Stekielcb20d202022-05-06 08:42:34 +0200196 * \param alg2 input string opaque key algorithm #2
Przemek Stekiel01396a12022-05-02 13:41:53 +0200197 * \param psa_alg1 output PSA algorithm #1
198 * \param psa_alg2 output PSA algorithm #2
199 * \param usage output key usage
Przemek Stekielcb20d202022-05-06 08:42:34 +0200200 * \param key_type key type used to set default psa algorithm/usage
201 * when alg1 in "none"
Przemek Stekiel01396a12022-05-02 13:41:53 +0200202 *
203 * \return \c 0 on success.
204 * \return \c 1 on parse failure.
205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
207 psa_algorithm_t *psa_alg1,
208 psa_algorithm_t *psa_alg2,
209 psa_key_usage_t *usage,
210 mbedtls_pk_type_t key_type);
Valerio Setti7541ebe2024-02-27 10:44:33 +0100211
Valerio Setti90eca2a2024-02-28 10:45:43 +0100212#if defined(MBEDTLS_PK_C)
Valerio Setti7541ebe2024-02-27 10:44:33 +0100213/** Turn a non-opaque PK context into an opaque one with folowing steps:
214 * - extract the key data and attributes from the PK context.
215 * - import the key material into PSA.
216 * - free the provided PK context and re-initilize it as an opaque PK context
217 * wrapping the PSA key imported in the above step.
218 *
Gilles Peskine2e5a7ea2025-02-12 23:11:09 +0100219 * \param[in,out] pk On input, the non-opaque PK context which contains the
220 * key to be wrapped. On output, the re-initialized PK
Valerio Setti7541ebe2024-02-27 10:44:33 +0100221 * context which represents the opaque version of the one
222 * provided as input.
223 * \param[in] psa_alg The primary algorithm that will be associated to the
224 * PSA key.
225 * \param[in] psa_alg2 The enrollment algorithm that will be associated to the
226 * PSA key.
227 * \param[in] psa_usage The PSA key usage policy.
228 * \param[out] key_id The PSA key identifier of the imported key.
229 *
230 * \return \c 0 on sucess.
231 * \return \c -1 on failure.
232 */
233int pk_wrap_as_opaque(mbedtls_pk_context *pk, psa_algorithm_t psa_alg, psa_algorithm_t psa_alg2,
234 psa_key_usage_t psa_usage, mbedtls_svc_key_id_t *key_id);
Valerio Setti90eca2a2024-02-28 10:45:43 +0100235#endif /* MBEDTLS_PK_C */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200236
Ben Taylor4bb98be2025-05-07 14:21:20 +0100237#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine21462112021-01-13 23:53:09 +0100238/* The test implementation of the PSA external RNG is insecure. When
239 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
240 * function that makes use of an RNG, you must call
241 * mbedtls_test_enable_insecure_external_rng(). */
Gilles Peskine1af872d2021-01-20 20:02:01 +0100242#include <test/fake_external_rng_for_test.h>
Gilles Peskine21462112021-01-13 23:53:09 +0100243#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100244
245#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int ca_callback(void *data, mbedtls_x509_crt const *child,
247 mbedtls_x509_crt **candidates);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100248#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
249
250/*
251 * Test recv/send functions that make sure each try returns
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800252 * WANT_READ/WANT_WRITE at least once before succeeding
Gilles Peskine504c1a32021-01-05 23:40:14 +0100253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int delayed_recv(void *ctx, unsigned char *buf, size_t len);
255int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100256
257/*
258 * Wait for an event from the underlying transport or the timer
259 * (Used in event-driven IO mode).
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int idle(mbedtls_net_context *fd,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100262#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_timing_delay_context *timer,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100264#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100266
Gilles Peskine53dea742021-02-02 22:55:06 +0100267#if defined(MBEDTLS_TEST_HOOKS)
268/** Initialize whatever test hooks are enabled by the compile-time
269 * configuration and make sense for the TLS test programs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100271
272/** Check if any test hooks detected a problem.
273 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100274 * If a problem was detected, it's ok for the calling program to keep going,
275 * but it should ultimately exit with an error status.
276 *
277 * \note When implementing a test hook that detects errors on its own
278 * (as opposed to e.g. leaving the error for a memory sanitizer to
279 * report), make sure to print a message to standard error either at
280 * the time the problem is detected or during the execution of this
281 * function. This function does not indicate what problem was detected,
282 * so printing a message is the only way to provide feedback in the
283 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100284 *
285 * \return Nonzero if a problem was detected.
286 * \c 0 if no problem was detected.
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100289
290/** Free any resources allocated for the sake of test hooks.
291 *
292 * Call this at the end of the program so that resource leak analyzers
293 * don't complain.
294 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100296
Gilles Peskine53dea742021-02-02 22:55:06 +0100297#endif /* !MBEDTLS_TEST_HOOKS */
298
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200299/* Helper functions for FFDH groups. */
Przemek Stekiel68e75442023-07-06 11:21:39 +0200300int parse_groups(const char *groups, uint16_t *group_list, size_t group_list_len);
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200301
Gilles Peskine504c1a32021-01-05 23:40:14 +0100302#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100303#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */