blob: b3395f730045d2d5de7ae38c708b84f8f3ffa5a2 [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
25#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020026# include "mbedtls/platform.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010027#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020028# include <stdio.h>
29# include <stdlib.h>
30# define mbedtls_calloc calloc
31# define mbedtls_free free
32# define mbedtls_time time
33# define mbedtls_time_t time_t
34# define mbedtls_printf printf
35# define mbedtls_fprintf fprintf
36# define mbedtls_snprintf snprintf
37# define mbedtls_exit exit
38# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
39# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Gilles Peskineab7ce962021-01-05 21:27:53 +010040#endif
41
Gilles Peskine8133abd2021-02-08 21:20:12 +010042#undef HAVE_RNG
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
44 (defined(MBEDTLS_USE_PSA_CRYPTO) || \
45 defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG))
46# define HAVE_RNG
Gilles Peskine8133abd2021-02-08 21:20:12 +010047#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# define HAVE_RNG
49#elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
50 (defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C))
51# define HAVE_RNG
Gilles Peskine8133abd2021-02-08 21:20:12 +010052#endif
53
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054#if !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_SSL_TLS_C) || \
Gilles Peskineab7ce962021-01-05 21:27:53 +010055 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# define MBEDTLS_SSL_TEST_IMPOSSIBLE \
57 "MBEDTLS_NET_C and/or " \
58 "MBEDTLS_SSL_TLS_C not defined, " \
59 "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n"
Gilles Peskine8133abd2021-02-08 21:20:12 +010060#elif !defined(HAVE_RNG)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061# define MBEDTLS_SSL_TEST_IMPOSSIBLE "No random generator is available.\n"
Gilles Peskineab7ce962021-01-05 21:27:53 +010062#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063# undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065# undef HAVE_RNG
Gilles Peskine8133abd2021-02-08 21:20:12 +010066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067# include <stdio.h>
68# include <stdlib.h>
69# include <string.h>
Gilles Peskine67638d62021-01-05 21:36:29 +010070
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071# include "mbedtls/net_sockets.h"
72# include "mbedtls/ssl.h"
73# include "mbedtls/entropy.h"
74# include "mbedtls/ctr_drbg.h"
75# include "mbedtls/hmac_drbg.h"
76# include "mbedtls/x509.h"
77# include "mbedtls/error.h"
78# include "mbedtls/debug.h"
79# include "mbedtls/timing.h"
80# include "mbedtls/base64.h"
81# include "test/certs.h"
Gilles Peskine67638d62021-01-05 21:36:29 +010082
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083# if defined(MBEDTLS_USE_PSA_CRYPTO) || \
84 defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
85# include "psa/crypto.h"
86# include "mbedtls/psa_util.h"
87# endif
Gilles Peskineab7ce962021-01-05 21:27:53 +010088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
90# include "mbedtls/memory_buffer_alloc.h"
91# endif
Gilles Peskine67638d62021-01-05 21:36:29 +010092
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093# include <test/helpers.h>
Gilles Peskine67638d62021-01-05 21:36:29 +010094
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020095# include "../test/query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010096
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097# if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine7c818d62021-01-05 22:33:13 +010098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099typedef struct eap_tls_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100100 unsigned char master_secret[48];
101 unsigned char randbytes[64];
102 mbedtls_tls_prf_types tls_prf_type;
103} eap_tls_keys;
104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105# if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine7c818d62021-01-05 22:33:13 +0100106
107/* Supported SRTP mode needs a maximum of :
108 * - 16 bytes for key (AES-128)
109 * - 14 bytes SALT
110 * One for sender, one for receiver context
111 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112# define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
Gilles Peskine7c818d62021-01-05 22:33:13 +0100113
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114typedef struct dtls_srtp_keys {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100115 unsigned char master_secret[48];
116 unsigned char randbytes[64];
117 mbedtls_tls_prf_types tls_prf_type;
118} dtls_srtp_keys;
119
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120# endif /* MBEDTLS_SSL_DTLS_SRTP */
Gilles Peskine7c818d62021-01-05 22:33:13 +0100121
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122# endif /* MBEDTLS_SSL_EXPORT_KEYS */
Gilles Peskine7c818d62021-01-05 22:33:13 +0100123
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124typedef struct {
Gilles Peskine7c818d62021-01-05 22:33:13 +0100125 mbedtls_ssl_context *ssl;
126 mbedtls_net_context *net;
127} io_ctx_t;
128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129void my_debug(void *ctx, int level, const char *file, int line, const char *str);
Gilles Peskine67638d62021-01-05 21:36:29 +0100130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131mbedtls_time_t dummy_constant_time(mbedtls_time_t *time);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100132
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133# if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine8eb29432021-02-03 20:07:11 +0100134/* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use
135 * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator.
136 *
137 * The constraints are:
138 * - Without the entropy module, the PSA RNG is the only option.
139 * - Without at least one of the DRBG modules, the PSA RNG is the only option.
140 * - The PSA RNG does not support explicit seeding, so it is incompatible with
141 * the reproducible mode used by test programs.
142 * - For good overall test coverage, there should be at least one configuration
143 * where the test programs use the PSA RNG while the PSA RNG is itself based
144 * on entropy+DRBG, and at least one configuration where the test programs
145 * do not use the PSA RNG even though it's there.
146 *
147 * A simple choice that meets the constraints is to use the PSA RNG whenever
148 * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the
149 * choice to use the PSA RNG in the test programs and the choice to use
150 * PSA crypto when TLS code needs crypto have to be tied together, but it
151 * happens to be a good match. It's also a good match from an application
152 * perspective: either PSA is preferred for TLS (both for crypto and for
153 * random generation) or it isn't.
154 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200155# define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
156# endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100157
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100158/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100159 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160typedef struct {
161# if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine8eb29432021-02-03 20:07:11 +0100162 unsigned char dummy;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163# else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100164 mbedtls_entropy_context entropy;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165# if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100166 mbedtls_ctr_drbg_context drbg;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200167# elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskineba749042021-01-13 20:02:03 +0100168 mbedtls_hmac_drbg_context drbg;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169# else
170# error "No DRBG available"
171# endif
172# endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100173} rng_context_t;
174
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100175/** Initialize the RNG.
176 *
177 * This function only initializes the memory used by the RNG context.
178 * Before using the RNG, it must be seeded with rng_seed().
179 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180void rng_init(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100181
182/* Seed the random number generator.
183 *
184 * \param rng The RNG context to use. It must have been initialized
185 * with rng_init().
186 * \param reproducible If zero, seed the RNG from entropy.
187 * If nonzero, use a fixed seed, so that the program
188 * will produce the same sequence of random numbers
189 * each time it is invoked.
190 * \param pers A null-terminated string. Different values for this
191 * string cause the RNG to emit different output for
192 * the same seed.
193 *
194 * return 0 on success, a negative value on error.
195 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196int rng_seed(rng_context_t *rng, int reproducible, const char *pers);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100197
198/** Deinitialize the RNG. Free any embedded resource.
199 *
200 * \param rng The RNG context to deinitialize. It must have been
201 * initialized with rng_init().
202 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203void rng_free(rng_context_t *rng);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100204
Gilles Peskine535fb372021-01-13 18:59:46 +0100205/** Generate random data.
206 *
207 * This function is suitable for use as the \c f_rng argument to Mbed TLS
208 * library functions.
209 *
Gilles Peskineda9529f2021-01-25 13:42:42 +0100210 * \param p_rng The random generator context. This must be a pointer to
211 * a #rng_context_t structure.
Gilles Peskine535fb372021-01-13 18:59:46 +0100212 * \param output The buffer to fill.
213 * \param output_len The length of the buffer in bytes.
214 *
215 * \return \c 0 on success.
216 * \return An Mbed TLS error code on error.
217 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218int rng_get(void *p_rng, unsigned char *output, size_t output_len);
Gilles Peskine535fb372021-01-13 18:59:46 +0100219
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200220# if defined(MBEDTLS_USE_PSA_CRYPTO) && \
221 defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine21462112021-01-13 23:53:09 +0100222/* The test implementation of the PSA external RNG is insecure. When
223 * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
224 * function that makes use of an RNG, you must call
225 * mbedtls_test_enable_insecure_external_rng(). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226# include <test/fake_external_rng_for_test.h>
227# endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100228
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
230int ca_callback(void *data,
231 mbedtls_x509_crt const *child,
232 mbedtls_x509_crt **candidates);
233# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100234
235/*
236 * Test recv/send functions that make sure each try returns
237 * WANT_READ/WANT_WRITE at least once before sucesseding
238 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239int delayed_recv(void *ctx, unsigned char *buf, size_t len);
240int delayed_send(void *ctx, const unsigned char *buf, size_t len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100241
242/*
243 * Wait for an event from the underlying transport or the timer
244 * (Used in event-driven IO mode).
245 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200246int idle(mbedtls_net_context *fd,
247# if defined(MBEDTLS_TIMING_C)
248 mbedtls_timing_delay_context *timer,
249# endif
250 int idle_reason);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100251
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252# if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine53dea742021-02-02 22:55:06 +0100253/** Initialize whatever test hooks are enabled by the compile-time
254 * configuration and make sense for the TLS test programs. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200255void test_hooks_init(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100256
257/** Check if any test hooks detected a problem.
258 *
Gilles Peskine00d0ad42021-02-15 11:02:51 +0100259 * If a problem was detected, it's ok for the calling program to keep going,
260 * but it should ultimately exit with an error status.
261 *
262 * \note When implementing a test hook that detects errors on its own
263 * (as opposed to e.g. leaving the error for a memory sanitizer to
264 * report), make sure to print a message to standard error either at
265 * the time the problem is detected or during the execution of this
266 * function. This function does not indicate what problem was detected,
267 * so printing a message is the only way to provide feedback in the
268 * logs of the calling program.
Gilles Peskine53dea742021-02-02 22:55:06 +0100269 *
270 * \return Nonzero if a problem was detected.
271 * \c 0 if no problem was detected.
272 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273int test_hooks_failure_detected(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100274
275/** Free any resources allocated for the sake of test hooks.
276 *
277 * Call this at the end of the program so that resource leak analyzers
278 * don't complain.
279 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200280void test_hooks_free(void);
Gilles Peskine53dea742021-02-02 22:55:06 +0100281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282# endif /* !MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +0100283
Gilles Peskine504c1a32021-01-05 23:40:14 +0100284#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100285#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */