blob: 58804e606f8dbfe9aa1da22c29b848495fd0f7cc [file] [log] [blame]
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01001/*
Gilles Peskine0d980b82021-01-05 23:34:27 +01002 * Common code library for SSL test programs.
3 *
4 * In addition to the functions in this file, there is shared source code
5 * that cannot be compiled separately in "ssl_test_common_source.c".
Gilles Peskinea3ed34f2021-01-05 21:11:16 +01006 *
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020023#define MBEDTLS_ALLOW_PRIVATE_ACCESS
24
Gilles Peskinea3ed34f2021-01-05 21:11:16 +010025#include "ssl_test_lib.h"
26
Gilles Peskinee374b952021-02-03 00:05:19 +010027#if defined(MBEDTLS_TEST_HOOKS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020028# include "test/helpers.h"
Gilles Peskinee374b952021-02-03 00:05:19 +010029#endif
30
Gilles Peskineab7ce962021-01-05 21:27:53 +010031#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
32
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033void my_debug(void *ctx, int level, const char *file, int line, const char *str)
Gilles Peskine504c1a32021-01-05 23:40:14 +010034{
35 const char *p, *basename;
36
37 /* Extract basename from file */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038 for (p = basename = file; *p != '\0'; p++)
39 if (*p == '/' || *p == '\\')
Gilles Peskine504c1a32021-01-05 23:40:14 +010040 basename = p + 1;
41
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042 mbedtls_fprintf((FILE *)ctx, "%s:%04d: |%d| %s", basename, line, level,
43 str);
44 fflush((FILE *)ctx);
Gilles Peskine504c1a32021-01-05 23:40:14 +010045}
46
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Gilles Peskine504c1a32021-01-05 23:40:14 +010048{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049 (void)time;
Gilles Peskine504c1a32021-01-05 23:40:14 +010050 return 0x5af2a056;
51}
52
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053# if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
54static int dummy_entropy(void *data, unsigned char *output, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +010055{
56 size_t i;
57 int ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058 (void)data;
Gilles Peskine504c1a32021-01-05 23:40:14 +010059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060 ret = mbedtls_entropy_func(data, output, len);
61 for (i = 0; i < len; i++) {
62 // replace result with pseudo random
63 output[i] = (unsigned char)rand();
Gilles Peskine504c1a32021-01-05 23:40:14 +010064 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +010066}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067# endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010068
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069void rng_init(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010070{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071# if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
72 (void)rng;
73 psa_crypto_init();
74# else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8eb29432021-02-03 20:07:11 +010075
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020076# if defined(MBEDTLS_CTR_DRBG_C)
77 mbedtls_ctr_drbg_init(&rng->drbg);
78# elif defined(MBEDTLS_HMAC_DRBG_C)
79 mbedtls_hmac_drbg_init(&rng->drbg);
80# else
81# error "No DRBG available"
82# endif
Gilles Peskineba749042021-01-13 20:02:03 +010083
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020084 mbedtls_entropy_init(&rng->entropy);
85# endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010086}
87
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020088int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010089{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090# if defined(MBEDTLS_USE_PSA_CRYPTO)
91 if (reproducible) {
92 mbedtls_fprintf(
93 stderr,
94 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
95 return -1;
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010096 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097# endif
98# if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine8eb29432021-02-03 20:07:11 +010099 /* The PSA crypto RNG does its own seeding. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100 (void)rng;
101 (void)pers;
102 if (reproducible) {
103 mbedtls_fprintf(stderr,
104 "The PSA RNG does not support reproducible mode.\n");
105 return -1;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100106 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200107 return 0;
108# else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
109 int (*f_entropy)(void *, unsigned char *, size_t) =
110 (reproducible ? dummy_entropy : mbedtls_entropy_func);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100111
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200112 if (reproducible)
113 srand(1);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115# if defined(MBEDTLS_CTR_DRBG_C)
116 int ret = mbedtls_ctr_drbg_seed(&rng->drbg, f_entropy, &rng->entropy,
117 (const unsigned char *)pers, strlen(pers));
118# elif defined(MBEDTLS_HMAC_DRBG_C)
119# if defined(MBEDTLS_SHA256_C)
Gilles Peskineba749042021-01-13 20:02:03 +0100120 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121# elif defined(MBEDTLS_SHA512_C)
Gilles Peskineba749042021-01-13 20:02:03 +0100122 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200123# else
124# error "No message digest available for HMAC_DRBG"
125# endif
126 int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
127 mbedtls_md_info_from_type(md_type),
128 f_entropy, &rng->entropy,
129 (const unsigned char *)pers, strlen(pers));
130# else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) \
131 */
132# error "No DRBG available"
133# endif /* !defined(MBEDTLS_CTR_DRBG_C) && \
134 !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100135
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136 if (ret != 0) {
137 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
138 (unsigned int)-ret);
139 return ret;
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100140 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141# endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100142
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143 return 0;
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100144}
145
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200146void rng_free(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100147{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148# if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
149 (void)rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100150 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
151 * This is ok because none of our applications try to do any crypto after
152 * deinitializing the RNG. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 mbedtls_psa_crypto_free();
154# else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine8eb29432021-02-03 20:07:11 +0100155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156# if defined(MBEDTLS_CTR_DRBG_C)
157 mbedtls_ctr_drbg_free(&rng->drbg);
158# elif defined(MBEDTLS_HMAC_DRBG_C)
159 mbedtls_hmac_drbg_free(&rng->drbg);
160# else
161# error "No DRBG available"
162# endif
Gilles Peskineba749042021-01-13 20:02:03 +0100163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 mbedtls_entropy_free(&rng->entropy);
165# endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100166}
167
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200168int rng_get(void *p_rng, unsigned char *output, size_t output_len)
Gilles Peskine535fb372021-01-13 18:59:46 +0100169{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170# if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
171 (void)p_rng;
172 return (
173 mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, output, output_len));
174# else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100175 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177# if defined(MBEDTLS_CTR_DRBG_C)
178 return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
179# elif defined(MBEDTLS_HMAC_DRBG_C)
180 return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
181# else
182# error "No DRBG available"
183# endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100184
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185# endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100186}
187
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
189int ca_callback(void *data,
190 mbedtls_x509_crt const *child,
191 mbedtls_x509_crt **candidates)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100192{
193 int ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200194 mbedtls_x509_crt *ca = (mbedtls_x509_crt *)data;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100195 mbedtls_x509_crt *first;
196
197 /* This is a test-only implementation of the CA callback
198 * which always returns the entire list of trusted certificates.
199 * Production implementations managing a large number of CAs
200 * should use an efficient presentation and lookup for the
201 * set of trusted certificates (such as a hashtable) and only
202 * return those trusted certificates which satisfy basic
203 * parental checks, such as the matching of child `Issuer`
204 * and parent `Subject` field or matching key identifiers. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 ((void)child);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100206
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200207 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
208 if (first == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100209 ret = -1;
210 goto exit;
211 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 mbedtls_x509_crt_init(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100213
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100215 ret = -1;
216 goto exit;
217 }
218
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 while (ca->next != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100220 ca = ca->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100222 ret = -1;
223 goto exit;
224 }
225 }
226
227exit:
228
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229 if (ret != 0) {
230 mbedtls_x509_crt_free(first);
231 mbedtls_free(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100232 first = NULL;
233 }
234
235 *candidates = first;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100237}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100239
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200240int delayed_recv(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100241{
242 static int first_try = 1;
243 int ret;
244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100246 first_try = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100248 }
249
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250 ret = mbedtls_net_recv(ctx, buf, len);
251 if (ret != MBEDTLS_ERR_SSL_WANT_READ)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100252 first_try = 1; /* Next call will be a new operation */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100254}
255
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256int delayed_send(void *ctx, const unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100257{
258 static int first_try = 1;
259 int ret;
260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200261 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100262 first_try = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100264 }
265
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266 ret = mbedtls_net_send(ctx, buf, len);
267 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100268 first_try = 1; /* Next call will be a new operation */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200269 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100270}
271
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200272# if !defined(MBEDTLS_TIMING_C)
273int idle(mbedtls_net_context *fd, int idle_reason)
274# else
275int idle(mbedtls_net_context *fd,
276 mbedtls_timing_delay_context *timer,
277 int idle_reason)
278# endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100279{
280 int ret;
281 int poll_type = 0;
282
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200283 if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100284 poll_type = MBEDTLS_NET_POLL_WRITE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200285 else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100286 poll_type = MBEDTLS_NET_POLL_READ;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200287# if !defined(MBEDTLS_TIMING_C)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100288 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289 return 0;
290# endif
Gilles Peskine504c1a32021-01-05 23:40:14 +0100291
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 while (1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100293 /* Check if timer has expired */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200294# if defined(MBEDTLS_TIMING_C)
295 if (timer != NULL && mbedtls_timing_get_delay(timer) == 2) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100296 break;
297 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298# endif /* MBEDTLS_TIMING_C */
Gilles Peskine504c1a32021-01-05 23:40:14 +0100299
300 /* Check if underlying transport became available */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200301 if (poll_type != 0) {
302 ret = mbedtls_net_poll(fd, poll_type, 0);
303 if (ret < 0)
304 return ret;
305 if (ret == poll_type)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100306 break;
307 }
308 }
309
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310 return 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100311}
312
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200313# if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine53dea742021-02-02 22:55:06 +0100314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315void test_hooks_init(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100316{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200317 mbedtls_test_info_reset();
Gilles Peskinee374b952021-02-03 00:05:19 +0100318
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200319# if defined(MBEDTLS_TEST_MUTEX_USAGE)
320 mbedtls_test_mutex_usage_init();
321# endif
Gilles Peskine53dea742021-02-02 22:55:06 +0100322}
323
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200324int test_hooks_failure_detected(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100325{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200326# if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskinee374b952021-02-03 00:05:19 +0100327 /* Errors are reported via mbedtls_test_info. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200328 mbedtls_test_mutex_usage_check();
329# endif
Gilles Peskinee374b952021-02-03 00:05:19 +0100330
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS)
332 return 1;
333 return 0;
Gilles Peskine53dea742021-02-02 22:55:06 +0100334}
335
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200336void test_hooks_free(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100337{
338}
339
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200340# endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskine53dea742021-02-02 22:55:06 +0100341
Gilles Peskineab7ce962021-01-05 21:27:53 +0100342#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */