blob: fdb6a523a84afb7bf2c50fa9f865434bb9605486 [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)
28#include "test/helpers.h"
29#endif
30
Gilles Peskineab7ce962021-01-05 21:27:53 +010031#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
32
Gilles Peskine449bd832023-01-11 14:50:10 +010033void my_debug(void *ctx, int level,
34 const char *file, int line,
35 const char *str)
Gilles Peskine504c1a32021-01-05 23:40:14 +010036{
37 const char *p, *basename;
38
39 /* Extract basename from file */
Gilles Peskine449bd832023-01-11 14:50:10 +010040 for (p = basename = file; *p != '\0'; p++) {
41 if (*p == '/' || *p == '\\') {
Gilles Peskine504c1a32021-01-05 23:40:14 +010042 basename = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010043 }
44 }
Gilles Peskine504c1a32021-01-05 23:40:14 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046 mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s",
47 basename, line, level, str);
48 fflush((FILE *) ctx);
Gilles Peskine504c1a32021-01-05 23:40:14 +010049}
50
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020051#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010052mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Gilles Peskine504c1a32021-01-05 23:40:14 +010053{
54 (void) time;
55 return 0x5af2a056;
56}
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020057#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010058
Gilles Peskine8eb29432021-02-03 20:07:11 +010059#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +010060static int dummy_entropy(void *data, unsigned char *output, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +010061{
62 size_t i;
63 int ret;
64 (void) data;
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 ret = mbedtls_entropy_func(data, output, len);
67 for (i = 0; i < len; i++) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010068 //replace result with pseudo random
69 output[i] = (unsigned char) rand();
70 }
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +010072}
Gilles Peskine8eb29432021-02-03 20:07:11 +010073#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010074
Gilles Peskine449bd832023-01-11 14:50:10 +010075void rng_init(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010076{
Gilles Peskine8eb29432021-02-03 20:07:11 +010077#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
78 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 psa_crypto_init();
Gilles Peskine8eb29432021-02-03 20:07:11 +010080#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
81
Gilles Peskineba749042021-01-13 20:02:03 +010082#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_ctr_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010084#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_hmac_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010086#else
87#error "No DRBG available"
88#endif
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_entropy_init(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +010091#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010092}
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010095{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010096#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (reproducible) {
98 mbedtls_fprintf(stderr,
99 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
100 return -1;
Gilles Peskineaaedbdc2021-02-03 13:55:22 +0100101 }
102#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100103#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
104 /* The PSA crypto RNG does its own seeding. */
105 (void) rng;
106 (void) pers;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (reproducible) {
108 mbedtls_fprintf(stderr,
109 "The PSA RNG does not support reproducible mode.\n");
110 return -1;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100111 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100113#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 int (*f_entropy)(void *, unsigned char *, size_t) =
115 (reproducible ? dummy_entropy : mbedtls_entropy_func);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (reproducible) {
118 srand(1);
119 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100120
Gilles Peskineba749042021-01-13 20:02:03 +0100121#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 int ret = mbedtls_ctr_drbg_seed(&rng->drbg,
123 f_entropy, &rng->entropy,
124 (const unsigned char *) pers,
125 strlen(pers));
Gilles Peskineba749042021-01-13 20:02:03 +0100126#elif defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100127#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskineba749042021-01-13 20:02:03 +0100128 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100129#elif defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskineba749042021-01-13 20:02:03 +0100130 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
131#else
132#error "No message digest available for HMAC_DRBG"
133#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
135 mbedtls_md_info_from_type(md_type),
136 f_entropy, &rng->entropy,
137 (const unsigned char *) pers,
138 strlen(pers));
Gilles Peskine8eb29432021-02-03 20:07:11 +0100139#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100140#error "No DRBG available"
Gilles Peskine8eb29432021-02-03 20:07:11 +0100141#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ret != 0) {
144 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
145 (unsigned int) -ret);
146 return ret;
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100147 }
Gilles Peskine8eb29432021-02-03 20:07:11 +0100148#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return 0;
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100151}
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void rng_free(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100154{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100155#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
156 (void) rng;
157 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
158 * This is ok because none of our applications try to do any crypto after
159 * deinitializing the RNG. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_psa_crypto_free();
Gilles Peskine8eb29432021-02-03 20:07:11 +0100161#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
162
Gilles Peskineba749042021-01-13 20:02:03 +0100163#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_ctr_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100165#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_hmac_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100167#else
168#error "No DRBG available"
169#endif
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_entropy_free(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100172#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int rng_get(void *p_rng, unsigned char *output, size_t output_len)
Gilles Peskine535fb372021-01-13 18:59:46 +0100176{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100177#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
178 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
180 output, output_len);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100181#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100182 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100183
Gilles Peskineba749042021-01-13 20:02:03 +0100184#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100186#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100188#else
189#error "No DRBG available"
190#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100191
192#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100193}
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2)
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 char *separator;
198 if ((separator = strchr(arg, ',')) == NULL) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200199 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200201 *separator = '\0';
202
203 *alg1 = arg;
204 *alg2 = separator + 1;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (strcmp(*alg1, "rsa-sign-pkcs1") != 0 &&
207 strcmp(*alg1, "rsa-sign-pss") != 0 &&
208 strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
209 strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
210 strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
211 strcmp(*alg1, "rsa-decrypt") != 0 &&
212 strcmp(*alg1, "ecdsa-sign") != 0 &&
213 strcmp(*alg1, "ecdh") != 0) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200214 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (strcmp(*alg2, "rsa-sign-pkcs1") != 0 &&
218 strcmp(*alg2, "rsa-sign-pss") != 0 &&
219 strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
220 strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
221 strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
222 strcmp(*alg2, "rsa-decrypt") != 0 &&
223 strcmp(*alg2, "ecdsa-sign") != 0 &&
224 strcmp(*alg2, "ecdh") != 0 &&
225 strcmp(*alg2, "none") != 0) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200226 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200228
229 return 0;
230}
231
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200232#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
234 psa_algorithm_t *psa_alg1,
235 psa_algorithm_t *psa_alg2,
236 psa_key_usage_t *usage,
237 mbedtls_pk_type_t key_type)
Przemek Stekiel01396a12022-05-02 13:41:53 +0200238{
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (strcmp(alg1, "none") != 0) {
240 const char *algs[] = { alg1, alg2 };
Przemek Stekielcb20d202022-05-06 08:42:34 +0200241 psa_algorithm_t *psa_algs[] = { psa_alg1, psa_alg2 };
Przemek Stekiel01396a12022-05-02 13:41:53 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 for (int i = 0; i < 2; i++) {
244 if (strcmp(algs[i], "rsa-sign-pkcs1") == 0) {
245 *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200246 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 } else if (strcmp(algs[i], "rsa-sign-pss") == 0) {
248 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200249 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 } else if (strcmp(algs[i], "rsa-sign-pss-sha256") == 0) {
251 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron50969e32022-09-16 15:54:33 +0200252 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 } else if (strcmp(algs[i], "rsa-sign-pss-sha384") == 0) {
254 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron50969e32022-09-16 15:54:33 +0200255 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 } else if (strcmp(algs[i], "rsa-sign-pss-sha512") == 0) {
257 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron50969e32022-09-16 15:54:33 +0200258 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 } else if (strcmp(algs[i], "rsa-decrypt") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200260 *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_CRYPT;
261 *usage |= PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 } else if (strcmp(algs[i], "ecdsa-sign") == 0) {
263 *psa_algs[i] = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200264 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 } else if (strcmp(algs[i], "ecdh") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200266 *psa_algs[i] = PSA_ALG_ECDH;
267 *usage |= PSA_KEY_USAGE_DERIVE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 } else if (strcmp(algs[i], "none") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200269 *psa_algs[i] = PSA_ALG_NONE;
270 }
271 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 } else {
273 if (key_type == MBEDTLS_PK_ECKEY) {
274 *psa_alg1 = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200275 *psa_alg2 = PSA_ALG_ECDH;
276 *usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 } else if (key_type == MBEDTLS_PK_RSA) {
278 *psa_alg1 = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
279 *psa_alg2 = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200280 *usage = PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 } else {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200282 return 1;
283 }
Przemek Stekiel01396a12022-05-02 13:41:53 +0200284 }
285
286 return 0;
287}
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200288#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200289
Gilles Peskine504c1a32021-01-05 23:40:14 +0100290#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int ca_callback(void *data, mbedtls_x509_crt const *child,
292 mbedtls_x509_crt **candidates)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100293{
294 int ret = 0;
295 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
296 mbedtls_x509_crt *first;
297
298 /* This is a test-only implementation of the CA callback
299 * which always returns the entire list of trusted certificates.
300 * Production implementations managing a large number of CAs
301 * should use an efficient presentation and lookup for the
302 * set of trusted certificates (such as a hashtable) and only
303 * return those trusted certificates which satisfy basic
304 * parental checks, such as the matching of child `Issuer`
305 * and parent `Subject` field or matching key identifiers. */
306 ((void) child);
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
309 if (first == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100310 ret = -1;
311 goto exit;
312 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_x509_crt_init(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100316 ret = -1;
317 goto exit;
318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 while (ca->next != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100321 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100323 ret = -1;
324 goto exit;
325 }
326 }
327
328exit:
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (ret != 0) {
331 mbedtls_x509_crt_free(first);
332 mbedtls_free(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100333 first = NULL;
334 }
335
336 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100338}
339#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341int delayed_recv(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100342{
343 static int first_try = 1;
344 int ret;
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100347 first_try = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100349 }
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 ret = mbedtls_net_recv(ctx, buf, len);
352 if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100353 first_try = 1; /* Next call will be a new operation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 }
355 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100356}
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358int delayed_send(void *ctx, const unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100359{
360 static int first_try = 1;
361 int ret;
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100364 first_try = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ret = mbedtls_net_send(ctx, buf, len);
369 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100370 first_try = 1; /* Next call will be a new operation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
372 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100373}
374
375#if !defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100376int idle(mbedtls_net_context *fd,
377 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100378#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100379int idle(mbedtls_net_context *fd,
380 mbedtls_timing_delay_context *timer,
381 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100382#endif
383{
384 int ret;
385 int poll_type = 0;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100388 poll_type = MBEDTLS_NET_POLL_WRITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100390 poll_type = MBEDTLS_NET_POLL_READ;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100392#if !defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 else {
394 return 0;
395 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100396#endif
397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 while (1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100399 /* Check if timer has expired */
400#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (timer != NULL &&
402 mbedtls_timing_get_delay(timer) == 2) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100403 break;
404 }
405#endif /* MBEDTLS_TIMING_C */
406
407 /* Check if underlying transport became available */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (poll_type != 0) {
409 ret = mbedtls_net_poll(fd, poll_type, 0);
410 if (ret < 0) {
411 return ret;
412 }
413 if (ret == poll_type) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100414 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100416 }
417 }
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100420}
421
Gilles Peskine53dea742021-02-02 22:55:06 +0100422#if defined(MBEDTLS_TEST_HOOKS)
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424void test_hooks_init(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100425{
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_test_info_reset();
Gilles Peskinee374b952021-02-03 00:05:19 +0100427
428#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_test_mutex_usage_init();
Gilles Peskinee374b952021-02-03 00:05:19 +0100430#endif
Gilles Peskine53dea742021-02-02 22:55:06 +0100431}
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433int test_hooks_failure_detected(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100434{
Gilles Peskinee374b952021-02-03 00:05:19 +0100435#if defined(MBEDTLS_TEST_MUTEX_USAGE)
436 /* Errors are reported via mbedtls_test_info. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_test_mutex_usage_check();
Gilles Peskinee374b952021-02-03 00:05:19 +0100438#endif
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
441 return 1;
442 }
443 return 0;
Gilles Peskine53dea742021-02-02 22:55:06 +0100444}
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446void test_hooks_free(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100447{
448}
449
450#endif /* MBEDTLS_TEST_HOOKS */
451
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200452#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
453 defined(PSA_WANT_ALG_FFDH)
454static uint16_t mbedtls_ssl_ffdh_group_from_name(const char *name)
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200455{
456 if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048) == 0) {
457 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048;
458 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072) == 0) {
459 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072;
460 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096) == 0) {
461 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096;
462 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144) == 0) {
463 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144;
464 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192) == 0) {
465 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192;
466 }
467 return 0;
468}
469
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200470static const uint16_t *mbedtls_ssl_ffdh_supported_groups(void)
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200471{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200472#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +0200473 static const uint16_t ffdh_groups[] = {
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200474 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
475 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
476 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
477 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
478 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
479 0
480 };
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200481 return ffdh_groups;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200482#else
483 return NULL;
484#endif
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200485}
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200486#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200487
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200488int parse_curves(const char *curves, uint16_t *group_list, size_t group_list_len)
489{
490 char *p = (char *) curves;
491 char *q = NULL;
492 size_t i = 0;
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200493
494 if (strcmp(p, "none") == 0) {
495 group_list[0] = 0;
496 } else if (strcmp(p, "default") != 0) {
497 /* Leave room for a final NULL in curve list */
498 while (i < group_list_len - 1 && *p != '\0') {
499 q = p;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200500#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
501 defined(PSA_WANT_ALG_FFDH)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200502 uint16_t ffdh_group = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200503#endif
504#if defined(MBEDTLS_ECP_LIGHT)
505 const mbedtls_ecp_curve_info *curve_cur = NULL;
506#endif
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200507 /* Terminate the current string */
508 while (*p != ',' && *p != '\0') {
509 p++;
510 }
511 if (*p == ',') {
512 *p++ = '\0';
513 }
514
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200515#if defined(MBEDTLS_ECP_LIGHT)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200516 if ((curve_cur = mbedtls_ecp_curve_info_from_name(q)) != NULL) {
517 group_list[i++] = curve_cur->tls_id;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200518 } else
519#endif
520#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
521 defined(PSA_WANT_ALG_FFDH)
522 if ((ffdh_group = mbedtls_ssl_ffdh_group_from_name(q)) != 0) {
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200523 group_list[i++] = ffdh_group;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200524 } else
525#endif
526 {
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200527 mbedtls_printf("unknown curve %s\n", q);
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200528#if defined(MBEDTLS_ECP_LIGHT)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200529 mbedtls_printf("supported curves: ");
530 for (curve_cur = mbedtls_ecp_curve_list();
531 curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
532 curve_cur++) {
533 mbedtls_printf("%s ", curve_cur->name);
534 }
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200535#endif
536#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
537 defined(PSA_WANT_ALG_FFDH)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200538 const uint16_t *supported_ffdh_group = mbedtls_ssl_ffdh_supported_groups();
539 while (*supported_ffdh_group != 0) {
540 mbedtls_printf("%s ",
541 mbedtls_ssl_ffdh_name_from_group(*supported_ffdh_group));
542 supported_ffdh_group++;
543 }
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200544#endif
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200545 mbedtls_printf("\n");
546 return -1;
547 }
548 }
549
550 mbedtls_printf("Number of curves: %u\n", (unsigned int) i);
551
552 if (i == group_list_len - 1 && *p != '\0') {
553 mbedtls_printf("curves list too long, maximum %u",
554 (unsigned int) (group_list_len - 1));
555 return -1;
556 }
557
558 group_list[i] = 0;
559 }
560
561 return 0;
562}
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200563
Gilles Peskineab7ce962021-01-05 21:27:53 +0100564#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */