blob: ef0dba71898296a0bf305cb47165e81b00a39265 [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
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
Bence Szépkútic662b362021-05-27 11:25:03 +020023#include "mbedtls/build_info.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010024
Gilles Peskineab7ce962021-01-05 21:27:53 +010025#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +010026#include "mbedtls/md.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010027
Gilles Peskine8133abd2021-02-08 21:20:12 +010028#undef HAVE_RNG
29#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010030 (defined(MBEDTLS_USE_PSA_CRYPTO) || \
31 defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG))
Gilles Peskine8133abd2021-02-08 21:20:12 +010032#define HAVE_RNG
33#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
34#define HAVE_RNG
35#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010036 (defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA512))
Gilles Peskine8133abd2021-02-08 21:20:12 +010037#define HAVE_RNG
38#endif
39
40#if !defined(MBEDTLS_NET_C) || \
Andrzej Kurek03e01462022-01-03 12:53:24 +010041 !defined(MBEDTLS_SSL_TLS_C)
42#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
43 "MBEDTLS_NET_C and/or " \
44 "MBEDTLS_SSL_TLS_C not defined."
Gilles Peskine8133abd2021-02-08 21:20:12 +010045#elif !defined(HAVE_RNG)
Andrzej Kurek03e01462022-01-03 12:53:24 +010046#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
Gilles Peskine8133abd2021-02-08 21:20:12 +010047 "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010048#else
49#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010050
Gilles Peskine8133abd2021-02-08 21:20:12 +010051#undef HAVE_RNG
52
Gilles Peskine67638d62021-01-05 21:36:29 +010053#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56
57#include "mbedtls/net_sockets.h"
58#include "mbedtls/ssl.h"
Glenn Strauss6eef5632022-01-23 08:37:02 -050059#include "mbedtls/ssl_ciphersuites.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010060#include "mbedtls/entropy.h"
61#include "mbedtls/ctr_drbg.h"
Gilles Peskineba749042021-01-13 20:02:03 +010062#include "mbedtls/hmac_drbg.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010063#include "mbedtls/x509.h"
64#include "mbedtls/error.h"
65#include "mbedtls/debug.h"
66#include "mbedtls/timing.h"
67#include "mbedtls/base64.h"
Mateusz Starzyk1aec6462021-02-08 15:34:42 +010068#include "test/certs.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010069
Gilles Peskine8133abd2021-02-08 21:20:12 +010070#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine67638d62021-01-05 21:36:29 +010071#include "psa/crypto.h"
72#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010073#endif
74
Gilles Peskine67638d62021-01-05 21:36:29 +010075#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
76#include "mbedtls/memory_buffer_alloc.h"
77#endif
78
79#include <test/helpers.h>
80
Gilles Peskinec772b182021-01-12 15:55:10 +010081#include "../test/query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010082
Przemek Stekielda4fba62023-06-02 14:52:28 +020083#define ALPN_LIST_SIZE 10
Przemek Stekiel45255e42023-06-29 13:56:36 +020084#define GROUP_LIST_SIZE 25
Przemek Stekielda4fba62023-06-02 14:52:28 +020085#define SIG_ALG_LIST_SIZE 5
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010088 unsigned char master_secret[48];
89 unsigned char randbytes[64];
90 mbedtls_tls_prf_types tls_prf_type;
91} eap_tls_keys;
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +010094
95/* Supported SRTP mode needs a maximum of :
96 * - 16 bytes for key (AES-128)
97 * - 14 bytes SALT
98 * One for sender, one for receiver context
99 */
100#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100103 unsigned char master_secret[48];
104 unsigned char randbytes[64];
105 mbedtls_tls_prf_types tls_prf_type;
106} dtls_srtp_keys;
107
108#endif /* MBEDTLS_SSL_DTLS_SRTP */
109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100111 mbedtls_ssl_context *ssl;
112 mbedtls_net_context *net;
113} io_ctx_t;
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115void my_debug(void *ctx, int level,
116 const char *file, int line,
117 const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100118
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200119#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100120mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Raoul Strackx9ed9bc92020-06-22 14:08:57 +0200121#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100122
Jerry Yu79c00412022-03-01 17:03:56 +0800123#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine8eb29432021-02-03 20:07:11 +0100124/* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use
125 * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator.
126 *
127 * The constraints are:
128 * - Without the entropy module, the PSA RNG is the only option.
129 * - Without at least one of the DRBG modules, the PSA RNG is the only option.
130 * - The PSA RNG does not support explicit seeding, so it is incompatible with
131 * the reproducible mode used by test programs.
132 * - For good overall test coverage, there should be at least one configuration
133 * where the test programs use the PSA RNG while the PSA RNG is itself based
134 * on entropy+DRBG, and at least one configuration where the test programs
135 * do not use the PSA RNG even though it's there.
136 *
137 * A simple choice that meets the constraints is to use the PSA RNG whenever
138 * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the
139 * choice to use the PSA RNG in the test programs and the choice to use
140 * PSA crypto when TLS code needs crypto have to be tied together, but it
141 * happens to be a good match. It's also a good match from an application
142 * perspective: either PSA is preferred for TLS (both for crypto and for
143 * random generation) or it isn't.
144 */
145#define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
146#endif
147
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100148/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150typedef struct {
Gilles Peskine8eb29432021-02-03 20:07:11 +0100151#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
152 unsigned char dummy;
153#else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100154 mbedtls_entropy_context entropy;
Gilles Peskineba749042021-01-13 20:02:03 +0100155#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100156 mbedtls_ctr_drbg_context drbg;
Gilles Peskineba749042021-01-13 20:02:03 +0100157#elif defined(MBEDTLS_HMAC_DRBG_C)
158 mbedtls_hmac_drbg_context drbg;
159#else
160#error "No DRBG available"
161#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100162#endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100163} rng_context_t;
164
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100165/** Initialize the RNG.
166 *
167 * This function only initializes the memory used by the RNG context.
168 * Before using the RNG, it must be seeded with rng_seed().
169 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100171
172/* Seed the random number generator.
173 *
174 * \param rng The RNG context to use. It must have been initialized
175 * with rng_init().
176 * \param reproducible If zero, seed the RNG from entropy.
177 * If nonzero, use a fixed seed, so that the program
178 * will produce the same sequence of random numbers
179 * each time it is invoked.
180 * \param pers A null-terminated string. Different values for this
181 * string cause the RNG to emit different output for
182 * the same seed.
183 *
184 * return 0 on success, a negative value on error.
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100187
188/** Deinitialize the RNG. Free any embedded resource.
189 *
190 * \param rng The RNG context to deinitialize. It must have been
191 * initialized with rng_init().
192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100194
Gilles Peskine535fb372021-01-13 18:59:46 +0100195/** Generate random data.
196 *
197 * This function is suitable for use as the \c f_rng argument to Mbed TLS
198 * library functions.
199 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100200 * \param p_rng The random generator context. This must be a pointer to
201 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100202 * \param output The buffer to fill.
203 * \param output_len The length of the buffer in bytes.
204 *
205 * \return \c 0 on success.
206 * \return An Mbed TLS error code on error.
207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100209
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200210/** Parse command-line option: key_opaque_algs
211 *
212 *
213 * \param arg String value of key_opaque_algs
214 * Coma-separated pair of values among the following:
215 * - "rsa-sign-pkcs1"
216 * - "rsa-sign-pss"
217 * - "rsa-decrypt"
218 * - "ecdsa-sign"
219 * - "ecdh"
220 * - "none" (only acceptable for the second value).
221 * \param alg1 Address of pointer to alg #1
222 * \param alg2 Address of pointer to alg #2
223 *
224 * \return \c 0 on success.
225 * \return \c 1 on parse failure.
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2);
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200228
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200229#if defined(MBEDTLS_USE_PSA_CRYPTO)
bootstrap-prime6dbbf442022-05-17 19:30:44 -0400230/** Parse given opaque key algorithms to obtain psa algs and usage
Przemek Stekiel01396a12022-05-02 13:41:53 +0200231 * that will be passed to mbedtls_pk_wrap_as_opaque().
232 *
233 *
234 * \param alg1 input string opaque key algorithm #1
Przemek Stekielcb20d202022-05-06 08:42:34 +0200235 * \param alg2 input string opaque key algorithm #2
Przemek Stekiel01396a12022-05-02 13:41:53 +0200236 * \param psa_alg1 output PSA algorithm #1
237 * \param psa_alg2 output PSA algorithm #2
238 * \param usage output key usage
Przemek Stekielcb20d202022-05-06 08:42:34 +0200239 * \param key_type key type used to set default psa algorithm/usage
240 * when alg1 in "none"
Przemek Stekiel01396a12022-05-02 13:41:53 +0200241 *
242 * \return \c 0 on success.
243 * \return \c 1 on parse failure.
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
246 psa_algorithm_t *psa_alg1,
247 psa_algorithm_t *psa_alg2,
248 psa_key_usage_t *usage,
249 mbedtls_pk_type_t key_type);
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200250#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200251
Gilles Peskine21462112021-01-13 23:53:09 +0100252#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
253/* The test implementation of the PSA external RNG is insecure. When
254 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
255 * function that makes use of an RNG, you must call
256 * mbedtls_test_enable_insecure_external_rng(). */
Gilles Peskine1af872d2021-01-20 20:02:01 +0100257#include <test/fake_external_rng_for_test.h>
Gilles Peskine21462112021-01-13 23:53:09 +0100258#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100259
260#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int ca_callback(void *data, mbedtls_x509_crt const *child,
262 mbedtls_x509_crt **candidates);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100263#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
264
265/*
266 * Test recv/send functions that make sure each try returns
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800267 * WANT_READ/WANT_WRITE at least once before succeeding
Gilles Peskine504c1a32021-01-05 23:40:14 +0100268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int delayed_recv(void *ctx, unsigned char *buf, size_t len);
270int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100271
272/*
273 * Wait for an event from the underlying transport or the timer
274 * (Used in event-driven IO mode).
275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int idle(mbedtls_net_context *fd,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100277#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_timing_delay_context *timer,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100279#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100281
Gilles Peskine53dea742021-02-02 22:55:06 +0100282#if defined(MBEDTLS_TEST_HOOKS)
283/** Initialize whatever test hooks are enabled by the compile-time
284 * configuration and make sense for the TLS test programs. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100286
287/** Check if any test hooks detected a problem.
288 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100289 * If a problem was detected, it's ok for the calling program to keep going,
290 * but it should ultimately exit with an error status.
291 *
292 * \note When implementing a test hook that detects errors on its own
293 * (as opposed to e.g. leaving the error for a memory sanitizer to
294 * report), make sure to print a message to standard error either at
295 * the time the problem is detected or during the execution of this
296 * function. This function does not indicate what problem was detected,
297 * so printing a message is the only way to provide feedback in the
298 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100299 *
300 * \return Nonzero if a problem was detected.
301 * \c 0 if no problem was detected.
302 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100304
305/** Free any resources allocated for the sake of test hooks.
306 *
307 * Call this at the end of the program so that resource leak analyzers
308 * don't complain.
309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100311
Gilles Peskine53dea742021-02-02 22:55:06 +0100312#endif /* !MBEDTLS_TEST_HOOKS */
313
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200314/* Helper functions for FFDH groups. */
Przemek Stekiel68e75442023-07-06 11:21:39 +0200315int parse_groups(const char *groups, uint16_t *group_list, size_t group_list_len);
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200316
Gilles Peskine504c1a32021-01-05 23:40:14 +0100317#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100318#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */