blob: d6390035dd53e57dfa57c20d3f3ac85723d3b140 [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
23#include "ssl_test_lib.h"
24
Gilles Peskinee374b952021-02-03 00:05:19 +010025#if defined(MBEDTLS_TEST_HOOKS)
26#include "test/helpers.h"
27#endif
28
Gilles Peskineab7ce962021-01-05 21:27:53 +010029#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
30
David Horstmannceeaeb92023-01-05 15:44:23 +000031void my_debug(void *ctx, int level,
32 const char *file, int line,
33 const char *str)
Gilles Peskine504c1a32021-01-05 23:40:14 +010034{
35 const char *p, *basename;
36
37 /* Extract basename from file */
David Horstmannceeaeb92023-01-05 15:44:23 +000038 for (p = basename = file; *p != '\0'; p++) {
39 if (*p == '/' || *p == '\\') {
Gilles Peskine504c1a32021-01-05 23:40:14 +010040 basename = p + 1;
David Horstmannceeaeb92023-01-05 15:44:23 +000041 }
42 }
Gilles Peskine504c1a32021-01-05 23:40:14 +010043
David Horstmannceeaeb92023-01-05 15:44:23 +000044 mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s",
45 basename, line, level, str);
46 fflush((FILE *) ctx);
Gilles Peskine504c1a32021-01-05 23:40:14 +010047}
48
Raoul Strackx2db000f2020-06-22 14:08:57 +020049#if defined(MBEDTLS_HAVE_TIME)
David Horstmannceeaeb92023-01-05 15:44:23 +000050mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Gilles Peskine504c1a32021-01-05 23:40:14 +010051{
52 (void) time;
53 return 0x5af2a056;
54}
Raoul Strackx2db000f2020-06-22 14:08:57 +020055#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010056
Gilles Peskine8eb29432021-02-03 20:07:11 +010057#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
David Horstmannceeaeb92023-01-05 15:44:23 +000058static int dummy_entropy(void *data, unsigned char *output, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +010059{
60 size_t i;
61 int ret;
62 (void) data;
63
David Horstmannceeaeb92023-01-05 15:44:23 +000064 ret = mbedtls_entropy_func(data, output, len);
65 for (i = 0; i < len; i++) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010066 //replace result with pseudo random
67 output[i] = (unsigned char) rand();
68 }
David Horstmannceeaeb92023-01-05 15:44:23 +000069 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +010070}
Gilles Peskine8eb29432021-02-03 20:07:11 +010071#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010072
David Horstmannceeaeb92023-01-05 15:44:23 +000073void rng_init(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010074{
Gilles Peskine8eb29432021-02-03 20:07:11 +010075#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
76 (void) rng;
David Horstmannceeaeb92023-01-05 15:44:23 +000077 psa_crypto_init();
Gilles Peskine8eb29432021-02-03 20:07:11 +010078#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
79
Gilles Peskineba749042021-01-13 20:02:03 +010080#if defined(MBEDTLS_CTR_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000081 mbedtls_ctr_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010082#elif defined(MBEDTLS_HMAC_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +000083 mbedtls_hmac_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010084#else
85#error "No DRBG available"
86#endif
87
David Horstmannceeaeb92023-01-05 15:44:23 +000088 mbedtls_entropy_init(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +010089#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010090}
91
David Horstmannceeaeb92023-01-05 15:44:23 +000092int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010093{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010094#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmannceeaeb92023-01-05 15:44:23 +000095 if (reproducible) {
96 mbedtls_fprintf(stderr,
97 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
98 return -1;
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010099 }
100#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100101#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
102 /* The PSA crypto RNG does its own seeding. */
103 (void) rng;
104 (void) pers;
David Horstmannceeaeb92023-01-05 15:44:23 +0000105 if (reproducible) {
106 mbedtls_fprintf(stderr,
107 "The PSA RNG does not support reproducible mode.\n");
108 return -1;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100109 }
David Horstmannceeaeb92023-01-05 15:44:23 +0000110 return 0;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100111#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
David Horstmannceeaeb92023-01-05 15:44:23 +0000112 int (*f_entropy)(void *, unsigned char *, size_t) =
113 (reproducible ? dummy_entropy : mbedtls_entropy_func);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100114
David Horstmannceeaeb92023-01-05 15:44:23 +0000115 if (reproducible) {
116 srand(1);
117 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100118
Gilles Peskineba749042021-01-13 20:02:03 +0100119#if defined(MBEDTLS_CTR_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000120 int ret = mbedtls_ctr_drbg_seed(&rng->drbg,
121 f_entropy, &rng->entropy,
122 (const unsigned char *) pers,
123 strlen(pers));
Gilles Peskineba749042021-01-13 20:02:03 +0100124#elif defined(MBEDTLS_HMAC_DRBG_C)
125#if defined(MBEDTLS_SHA256_C)
126 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
127#elif defined(MBEDTLS_SHA512_C)
128 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
129#else
130#error "No message digest available for HMAC_DRBG"
131#endif
David Horstmannceeaeb92023-01-05 15:44:23 +0000132 int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
133 mbedtls_md_info_from_type(md_type),
134 f_entropy, &rng->entropy,
135 (const unsigned char *) pers,
136 strlen(pers));
Gilles Peskine8eb29432021-02-03 20:07:11 +0100137#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100138#error "No DRBG available"
Gilles Peskine8eb29432021-02-03 20:07:11 +0100139#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100140
David Horstmannceeaeb92023-01-05 15:44:23 +0000141 if (ret != 0) {
142 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
143 (unsigned int) -ret);
144 return ret;
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100145 }
Gilles Peskine8eb29432021-02-03 20:07:11 +0100146#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100147
David Horstmannceeaeb92023-01-05 15:44:23 +0000148 return 0;
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100149}
150
David Horstmannceeaeb92023-01-05 15:44:23 +0000151void rng_free(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100152{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100153#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
154 (void) rng;
155 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
156 * This is ok because none of our applications try to do any crypto after
157 * deinitializing the RNG. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000158 mbedtls_psa_crypto_free();
Gilles Peskine8eb29432021-02-03 20:07:11 +0100159#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
160
Gilles Peskineba749042021-01-13 20:02:03 +0100161#if defined(MBEDTLS_CTR_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000162 mbedtls_ctr_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100163#elif defined(MBEDTLS_HMAC_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000164 mbedtls_hmac_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100165#else
166#error "No DRBG available"
167#endif
168
David Horstmannceeaeb92023-01-05 15:44:23 +0000169 mbedtls_entropy_free(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100170#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100171}
172
David Horstmannceeaeb92023-01-05 15:44:23 +0000173int rng_get(void *p_rng, unsigned char *output, size_t output_len)
Gilles Peskine535fb372021-01-13 18:59:46 +0100174{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100175#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
176 (void) p_rng;
David Horstmannceeaeb92023-01-05 15:44:23 +0000177 return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
178 output, output_len);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100179#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100180 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100181
Gilles Peskineba749042021-01-13 20:02:03 +0100182#if defined(MBEDTLS_CTR_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000183 return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100184#elif defined(MBEDTLS_HMAC_DRBG_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000185 return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100186#else
187#error "No DRBG available"
188#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100189
190#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100191}
192
Gilles Peskine504c1a32021-01-05 23:40:14 +0100193#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
David Horstmannceeaeb92023-01-05 15:44:23 +0000194int ca_callback(void *data, mbedtls_x509_crt const *child,
195 mbedtls_x509_crt **candidates)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100196{
197 int ret = 0;
198 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
199 mbedtls_x509_crt *first;
200
201 /* This is a test-only implementation of the CA callback
202 * which always returns the entire list of trusted certificates.
203 * Production implementations managing a large number of CAs
204 * should use an efficient presentation and lookup for the
205 * set of trusted certificates (such as a hashtable) and only
206 * return those trusted certificates which satisfy basic
207 * parental checks, such as the matching of child `Issuer`
208 * and parent `Subject` field or matching key identifiers. */
209 ((void) child);
210
David Horstmannceeaeb92023-01-05 15:44:23 +0000211 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
212 if (first == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100213 ret = -1;
214 goto exit;
215 }
David Horstmannceeaeb92023-01-05 15:44:23 +0000216 mbedtls_x509_crt_init(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100217
David Horstmannceeaeb92023-01-05 15:44:23 +0000218 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100219 ret = -1;
220 goto exit;
221 }
222
David Horstmannceeaeb92023-01-05 15:44:23 +0000223 while (ca->next != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100224 ca = ca->next;
David Horstmannceeaeb92023-01-05 15:44:23 +0000225 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100226 ret = -1;
227 goto exit;
228 }
229 }
230
231exit:
232
David Horstmannceeaeb92023-01-05 15:44:23 +0000233 if (ret != 0) {
234 mbedtls_x509_crt_free(first);
235 mbedtls_free(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100236 first = NULL;
237 }
238
239 *candidates = first;
David Horstmannceeaeb92023-01-05 15:44:23 +0000240 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100241}
242#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
243
David Horstmannceeaeb92023-01-05 15:44:23 +0000244int delayed_recv(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100245{
246 static int first_try = 1;
247 int ret;
248
David Horstmannceeaeb92023-01-05 15:44:23 +0000249 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100250 first_try = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000251 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100252 }
253
David Horstmannceeaeb92023-01-05 15:44:23 +0000254 ret = mbedtls_net_recv(ctx, buf, len);
255 if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100256 first_try = 1; /* Next call will be a new operation */
David Horstmannceeaeb92023-01-05 15:44:23 +0000257 }
258 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100259}
260
David Horstmannceeaeb92023-01-05 15:44:23 +0000261int delayed_send(void *ctx, const unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100262{
263 static int first_try = 1;
264 int ret;
265
David Horstmannceeaeb92023-01-05 15:44:23 +0000266 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100267 first_try = 0;
David Horstmannceeaeb92023-01-05 15:44:23 +0000268 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100269 }
270
David Horstmannceeaeb92023-01-05 15:44:23 +0000271 ret = mbedtls_net_send(ctx, buf, len);
272 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100273 first_try = 1; /* Next call will be a new operation */
David Horstmannceeaeb92023-01-05 15:44:23 +0000274 }
275 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100276}
277
278#if !defined(MBEDTLS_TIMING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000279int idle(mbedtls_net_context *fd,
280 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100281#else
David Horstmannceeaeb92023-01-05 15:44:23 +0000282int idle(mbedtls_net_context *fd,
283 mbedtls_timing_delay_context *timer,
284 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100285#endif
286{
287 int ret;
288 int poll_type = 0;
289
David Horstmannceeaeb92023-01-05 15:44:23 +0000290 if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100291 poll_type = MBEDTLS_NET_POLL_WRITE;
David Horstmannceeaeb92023-01-05 15:44:23 +0000292 } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100293 poll_type = MBEDTLS_NET_POLL_READ;
David Horstmannceeaeb92023-01-05 15:44:23 +0000294 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100295#if !defined(MBEDTLS_TIMING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000296 else {
297 return 0;
298 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100299#endif
300
David Horstmannceeaeb92023-01-05 15:44:23 +0000301 while (1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100302 /* Check if timer has expired */
303#if defined(MBEDTLS_TIMING_C)
David Horstmannceeaeb92023-01-05 15:44:23 +0000304 if (timer != NULL &&
305 mbedtls_timing_get_delay(timer) == 2) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100306 break;
307 }
308#endif /* MBEDTLS_TIMING_C */
309
310 /* Check if underlying transport became available */
David Horstmannceeaeb92023-01-05 15:44:23 +0000311 if (poll_type != 0) {
312 ret = mbedtls_net_poll(fd, poll_type, 0);
313 if (ret < 0) {
314 return ret;
315 }
316 if (ret == poll_type) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100317 break;
David Horstmannceeaeb92023-01-05 15:44:23 +0000318 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100319 }
320 }
321
David Horstmannceeaeb92023-01-05 15:44:23 +0000322 return 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100323}
324
Gilles Peskine53dea742021-02-02 22:55:06 +0100325#if defined(MBEDTLS_TEST_HOOKS)
326
David Horstmannceeaeb92023-01-05 15:44:23 +0000327void test_hooks_init(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100328{
David Horstmannceeaeb92023-01-05 15:44:23 +0000329 mbedtls_test_info_reset();
Gilles Peskinee374b952021-02-03 00:05:19 +0100330
331#if defined(MBEDTLS_TEST_MUTEX_USAGE)
David Horstmannceeaeb92023-01-05 15:44:23 +0000332 mbedtls_test_mutex_usage_init();
Gilles Peskinee374b952021-02-03 00:05:19 +0100333#endif
Gilles Peskine53dea742021-02-02 22:55:06 +0100334}
335
David Horstmannceeaeb92023-01-05 15:44:23 +0000336int test_hooks_failure_detected(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100337{
Gilles Peskinee374b952021-02-03 00:05:19 +0100338#if defined(MBEDTLS_TEST_MUTEX_USAGE)
339 /* Errors are reported via mbedtls_test_info. */
David Horstmannceeaeb92023-01-05 15:44:23 +0000340 mbedtls_test_mutex_usage_check();
Gilles Peskinee374b952021-02-03 00:05:19 +0100341#endif
342
David Horstmannceeaeb92023-01-05 15:44:23 +0000343 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
344 return 1;
345 }
346 return 0;
Gilles Peskine53dea742021-02-02 22:55:06 +0100347}
348
David Horstmannceeaeb92023-01-05 15:44:23 +0000349void test_hooks_free(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100350{
351}
352
353#endif /* MBEDTLS_TEST_HOOKS */
354
Gilles Peskineab7ce962021-01-05 21:27:53 +0100355#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */