blob: b6d199592dfbca980ae235da1bbec23fd25e071a [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
Yanray Wangd818c082022-11-10 16:30:57 +080023#include "mbedtls/version.h"
24
Gilles Peskineab7ce962021-01-05 21:27:53 +010025#if !defined(MBEDTLS_CONFIG_FILE)
26#include "mbedtls/config.h"
27#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
Gilles Peskineab7ce962021-01-05 21:27:53 +010031#include "mbedtls/platform.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010032
Gilles Peskine8133abd2021-02-08 21:20:12 +010033#undef HAVE_RNG
34#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
David Horstmannceeaeb92023-01-05 15:44:23 +000035 (defined(MBEDTLS_USE_PSA_CRYPTO) || \
36 defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG))
Gilles Peskine8133abd2021-02-08 21:20:12 +010037#define HAVE_RNG
38#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
39#define HAVE_RNG
40#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
David Horstmannceeaeb92023-01-05 15:44:23 +000041 (defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C))
Gilles Peskine8133abd2021-02-08 21:20:12 +010042#define HAVE_RNG
43#endif
44
45#if !defined(MBEDTLS_NET_C) || \
Gilles Peskineab7ce962021-01-05 21:27:53 +010046 !defined(MBEDTLS_SSL_TLS_C) || \
47 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
48#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
Gilles Peskineab7ce962021-01-05 21:27:53 +010049 "MBEDTLS_NET_C and/or " \
50 "MBEDTLS_SSL_TLS_C not defined, " \
51 "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n"
Gilles Peskine8133abd2021-02-08 21:20:12 +010052#elif !defined(HAVE_RNG)
53#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
54 "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010055#else
56#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010057
Gilles Peskine8133abd2021-02-08 21:20:12 +010058#undef HAVE_RNG
59
Gilles Peskine67638d62021-01-05 21:36:29 +010060#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"
Gilles Peskineba749042021-01-13 20:02:03 +010068#include "mbedtls/hmac_drbg.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010069#include "mbedtls/certs.h"
70#include "mbedtls/x509.h"
71#include "mbedtls/error.h"
72#include "mbedtls/debug.h"
73#include "mbedtls/timing.h"
74#include "mbedtls/base64.h"
75
Gilles Peskine8133abd2021-02-08 21:20:12 +010076#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine67638d62021-01-05 21:36:29 +010077#include "psa/crypto.h"
78#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010079#endif
80
Gilles Peskine67638d62021-01-05 21:36:29 +010081#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
82#include "mbedtls/memory_buffer_alloc.h"
83#endif
84
85#include <test/helpers.h>
86
Gilles Peskinec772b182021-01-12 15:55:10 +010087#include "../test/query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010088
89#if defined(MBEDTLS_SSL_EXPORT_KEYS)
90
David Horstmannceeaeb92023-01-05 15:44:23 +000091typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +010092 unsigned char master_secret[48];
93 unsigned char randbytes[64];
94 mbedtls_tls_prf_types tls_prf_type;
95} eap_tls_keys;
96
David Horstmannceeaeb92023-01-05 15:44:23 +000097#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +010098
99/* Supported SRTP mode needs a maximum of :
100 * - 16 bytes for key (AES-128)
101 * - 14 bytes SALT
102 * One for sender, one for receiver context
103 */
104#define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
105
David Horstmannceeaeb92023-01-05 15:44:23 +0000106typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100107 unsigned char master_secret[48];
108 unsigned char randbytes[64];
109 mbedtls_tls_prf_types tls_prf_type;
110} dtls_srtp_keys;
111
112#endif /* MBEDTLS_SSL_DTLS_SRTP */
113
114#endif /* MBEDTLS_SSL_EXPORT_KEYS */
115
David Horstmannceeaeb92023-01-05 15:44:23 +0000116typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100117 mbedtls_ssl_context *ssl;
118 mbedtls_net_context *net;
119} io_ctx_t;
120
David Horstmannceeaeb92023-01-05 15:44:23 +0000121void my_debug(void *ctx, int level,
122 const char *file, int line,
123 const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100124
Raoul Strackx2db000f2020-06-22 14:08:57 +0200125#if defined(MBEDTLS_HAVE_TIME)
David Horstmannceeaeb92023-01-05 15:44:23 +0000126mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Raoul Strackx2db000f2020-06-22 14:08:57 +0200127#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100128
Gilles Peskine8eb29432021-02-03 20:07:11 +0100129#if defined(MBEDTLS_USE_PSA_CRYPTO)
130/* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use
131 * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator.
132 *
133 * The constraints are:
134 * - Without the entropy module, the PSA RNG is the only option.
135 * - Without at least one of the DRBG modules, the PSA RNG is the only option.
136 * - The PSA RNG does not support explicit seeding, so it is incompatible with
137 * the reproducible mode used by test programs.
138 * - For good overall test coverage, there should be at least one configuration
139 * where the test programs use the PSA RNG while the PSA RNG is itself based
140 * on entropy+DRBG, and at least one configuration where the test programs
141 * do not use the PSA RNG even though it's there.
142 *
143 * A simple choice that meets the constraints is to use the PSA RNG whenever
144 * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the
145 * choice to use the PSA RNG in the test programs and the choice to use
146 * PSA crypto when TLS code needs crypto have to be tied together, but it
147 * happens to be a good match. It's also a good match from an application
148 * perspective: either PSA is preferred for TLS (both for crypto and for
149 * random generation) or it isn't.
150 */
151#define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
152#endif
153
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100154/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100155 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000156typedef struct {
Gilles Peskine8eb29432021-02-03 20:07:11 +0100157#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
158 unsigned char dummy;
159#else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100160 mbedtls_entropy_context entropy;
Gilles Peskineba749042021-01-13 20:02:03 +0100161#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100162 mbedtls_ctr_drbg_context drbg;
Gilles Peskineba749042021-01-13 20:02:03 +0100163#elif defined(MBEDTLS_HMAC_DRBG_C)
164 mbedtls_hmac_drbg_context drbg;
165#else
166#error "No DRBG available"
167#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100168#endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100169} rng_context_t;
170
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100171/** Initialize the RNG.
172 *
173 * This function only initializes the memory used by the RNG context.
174 * Before using the RNG, it must be seeded with rng_seed().
175 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000176void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100177
178/* Seed the random number generator.
179 *
180 * \param rng The RNG context to use. It must have been initialized
181 * with rng_init().
182 * \param reproducible If zero, seed the RNG from entropy.
183 * If nonzero, use a fixed seed, so that the program
184 * will produce the same sequence of random numbers
185 * each time it is invoked.
186 * \param pers A null-terminated string. Different values for this
187 * string cause the RNG to emit different output for
188 * the same seed.
189 *
190 * return 0 on success, a negative value on error.
191 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000192int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100193
194/** Deinitialize the RNG. Free any embedded resource.
195 *
196 * \param rng The RNG context to deinitialize. It must have been
197 * initialized with rng_init().
198 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000199void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100200
Gilles Peskine535fb372021-01-13 18:59:46 +0100201/** Generate random data.
202 *
203 * This function is suitable for use as the \c f_rng argument to Mbed TLS
204 * library functions.
205 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100206 * \param p_rng The random generator context. This must be a pointer to
207 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100208 * \param output The buffer to fill.
209 * \param output_len The length of the buffer in bytes.
210 *
211 * \return \c 0 on success.
212 * \return An Mbed TLS error code on error.
213 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000214int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100215
Gilles Peskine21462112021-01-13 23:53:09 +0100216#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
217/* The test implementation of the PSA external RNG is insecure. When
218 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
219 * function that makes use of an RNG, you must call
220 * mbedtls_test_enable_insecure_external_rng(). */
Gilles Peskine1af872d2021-01-20 20:02:01 +0100221#include <test/fake_external_rng_for_test.h>
Gilles Peskine21462112021-01-13 23:53:09 +0100222#endif
223
Gilles Peskine504c1a32021-01-05 23:40:14 +0100224#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
David Horstmannceeaeb92023-01-05 15:44:23 +0000225int ca_callback(void *data, mbedtls_x509_crt const *child,
226 mbedtls_x509_crt **candidates);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100227#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
228
229/*
230 * Test recv/send functions that make sure each try returns
Shaun Case0e7791f2021-12-20 21:14:10 -0800231 * WANT_READ/WANT_WRITE at least once before succeeding
Gilles Peskine504c1a32021-01-05 23:40:14 +0100232 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000233int delayed_recv(void *ctx, unsigned char *buf, size_t len);
234int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100235
236/*
237 * Wait for an event from the underlying transport or the timer
238 * (Used in event-driven IO mode).
239 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000240int idle(mbedtls_net_context *fd,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100241#if defined(MBEDTLS_TIMING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000242 mbedtls_timing_delay_context *timer,
Gilles Peskine504c1a32021-01-05 23:40:14 +0100243#endif
David Horstmannceeaeb92023-01-05 15:44:23 +0000244 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100245
Gilles Peskine53dea742021-02-02 22:55:06 +0100246#if defined(MBEDTLS_TEST_HOOKS)
247/** Initialize whatever test hooks are enabled by the compile-time
248 * configuration and make sense for the TLS test programs. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000249void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100250
251/** Check if any test hooks detected a problem.
252 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100253 * If a problem was detected, it's ok for the calling program to keep going,
254 * but it should ultimately exit with an error status.
255 *
256 * \note When implementing a test hook that detects errors on its own
257 * (as opposed to e.g. leaving the error for a memory sanitizer to
258 * report), make sure to print a message to standard error either at
259 * the time the problem is detected or during the execution of this
260 * function. This function does not indicate what problem was detected,
261 * so printing a message is the only way to provide feedback in the
262 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100263 *
264 * \return Nonzero if a problem was detected.
265 * \c 0 if no problem was detected.
266 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000267int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100268
269/** Free any resources allocated for the sake of test hooks.
270 *
271 * Call this at the end of the program so that resource leak analyzers
272 * don't complain.
273 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000274void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100275
Gilles Peskine53dea742021-02-02 22:55:06 +0100276#endif /* !MBEDTLS_TEST_HOOKS */
277
Gilles Peskine504c1a32021-01-05 23:40:14 +0100278#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100279#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */