blob: 344cd28fd374fe86aac1ef491e83e32c1706bef8 [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
Gilles Peskineab7ce962021-01-05 21:27:53 +010023#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
29#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdio.h>
33#include <stdlib.h>
34#define mbedtls_calloc calloc
35#define mbedtls_free free
36#define mbedtls_time time
37#define mbedtls_time_t time_t
38#define mbedtls_printf printf
39#define mbedtls_fprintf fprintf
40#define mbedtls_snprintf snprintf
41#define mbedtls_exit exit
42#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
43#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
44#endif
45
46#if !defined(MBEDTLS_CTR_DRBG_C) || \
47 !defined(MBEDTLS_ENTROPY_C) || \
48 !defined(MBEDTLS_NET_C) || \
49 !defined(MBEDTLS_SSL_TLS_C) || \
50 defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
51#define MBEDTLS_SSL_TEST_IMPOSSIBLE \
52 "MBEDTLS_CTR_DRBG_C and/or " \
53 "MBEDTLS_ENTROPY_C and/or " \
54 "MBEDTLS_NET_C and/or " \
55 "MBEDTLS_SSL_TLS_C not defined, " \
56 "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n"
57#else
58#undef MBEDTLS_SSL_TEST_IMPOSSIBLE
Gilles Peskine67638d62021-01-05 21:36:29 +010059
60#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"
68#include "mbedtls/certs.h"
69#include "mbedtls/x509.h"
70#include "mbedtls/error.h"
71#include "mbedtls/debug.h"
72#include "mbedtls/timing.h"
73#include "mbedtls/base64.h"
74
75#if defined(MBEDTLS_USE_PSA_CRYPTO)
76#include "psa/crypto.h"
77#include "mbedtls/psa_util.h"
Gilles Peskineab7ce962021-01-05 21:27:53 +010078#endif
79
Gilles Peskine67638d62021-01-05 21:36:29 +010080#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
81#include "mbedtls/memory_buffer_alloc.h"
82#endif
83
84#include <test/helpers.h>
85
Gilles Peskinec772b182021-01-12 15:55:10 +010086#include "../test/query_config.h"
Gilles Peskine7c818d62021-01-05 22:33:13 +010087
88#if defined(MBEDTLS_SSL_EXPORT_KEYS)
89
90typedef struct eap_tls_keys
91{
92 unsigned char master_secret[48];
93 unsigned char randbytes[64];
94 mbedtls_tls_prf_types tls_prf_type;
95} eap_tls_keys;
96
97#if defined( MBEDTLS_SSL_DTLS_SRTP )
98
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
106typedef struct dtls_srtp_keys
107{
108 unsigned char master_secret[48];
109 unsigned char randbytes[64];
110 mbedtls_tls_prf_types tls_prf_type;
111} dtls_srtp_keys;
112
113#endif /* MBEDTLS_SSL_DTLS_SRTP */
114
115#endif /* MBEDTLS_SSL_EXPORT_KEYS */
116
117typedef struct
118{
119 mbedtls_ssl_context *ssl;
120 mbedtls_net_context *net;
121} io_ctx_t;
122
Gilles Peskine504c1a32021-01-05 23:40:14 +0100123void my_debug( void *ctx, int level,
124 const char *file, int line,
125 const char *str );
Gilles Peskine67638d62021-01-05 21:36:29 +0100126
Gilles Peskine504c1a32021-01-05 23:40:14 +0100127mbedtls_time_t dummy_constant_time( mbedtls_time_t* time );
128
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100129/** A context for random number generation (RNG).
Gilles Peskine8a8492b2021-01-13 18:17:32 +0100130 */
131typedef struct
132{
133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context drbg;
135} rng_context_t;
136
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100137/** Initialize the RNG.
138 *
139 * This function only initializes the memory used by the RNG context.
140 * Before using the RNG, it must be seeded with rng_seed().
141 */
142void rng_init( rng_context_t *rng );
143
144/* Seed the random number generator.
145 *
146 * \param rng The RNG context to use. It must have been initialized
147 * with rng_init().
148 * \param reproducible If zero, seed the RNG from entropy.
149 * If nonzero, use a fixed seed, so that the program
150 * will produce the same sequence of random numbers
151 * each time it is invoked.
152 * \param pers A null-terminated string. Different values for this
153 * string cause the RNG to emit different output for
154 * the same seed.
155 *
156 * return 0 on success, a negative value on error.
157 */
158int rng_seed( rng_context_t *rng, int reproducible, const char *pers );
159
160/** Deinitialize the RNG. Free any embedded resource.
161 *
162 * \param rng The RNG context to deinitialize. It must have been
163 * initialized with rng_init().
164 */
165void rng_free( rng_context_t *rng );
166
Gilles Peskine535fb372021-01-13 18:59:46 +0100167/** Generate random data.
168 *
169 * This function is suitable for use as the \c f_rng argument to Mbed TLS
170 * library functions.
171 *
172 * \param p_rng The CTR_DRBG context. This must be a pointer to a
173 * #rng_context_t structure.
174 * \param output The buffer to fill.
175 * \param output_len The length of the buffer in bytes.
176 *
177 * \return \c 0 on success.
178 * \return An Mbed TLS error code on error.
179 */
180int rng_get( void *p_rng, unsigned char *output, size_t output_len );
181
Gilles Peskine504c1a32021-01-05 23:40:14 +0100182#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
183int ca_callback( void *data, mbedtls_x509_crt const *child,
184 mbedtls_x509_crt **candidates );
185#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
186
187/*
188 * Test recv/send functions that make sure each try returns
189 * WANT_READ/WANT_WRITE at least once before sucesseding
190 */
191int delayed_recv( void *ctx, unsigned char *buf, size_t len );
192int delayed_send( void *ctx, const unsigned char *buf, size_t len );
193
194/*
195 * Wait for an event from the underlying transport or the timer
196 * (Used in event-driven IO mode).
197 */
198int idle( mbedtls_net_context *fd,
199#if defined(MBEDTLS_TIMING_C)
200 mbedtls_timing_delay_context *timer,
201#endif
202 int idle_reason );
203
204#endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
Gilles Peskinea3ed34f2021-01-05 21:11:16 +0100205#endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */