blob: 42336f1f66905afc3f5d1d4698027e058894cf7b [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
Valerio Settiacd32c02023-06-29 18:06:29 +020033#define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0]))
34
Gilles Peskine449bd832023-01-11 14:50:10 +010035void my_debug(void *ctx, int level,
36 const char *file, int line,
37 const char *str)
Gilles Peskine504c1a32021-01-05 23:40:14 +010038{
39 const char *p, *basename;
40
41 /* Extract basename from file */
Gilles Peskine449bd832023-01-11 14:50:10 +010042 for (p = basename = file; *p != '\0'; p++) {
43 if (*p == '/' || *p == '\\') {
Gilles Peskine504c1a32021-01-05 23:40:14 +010044 basename = p + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +010045 }
46 }
Gilles Peskine504c1a32021-01-05 23:40:14 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s",
49 basename, line, level, str);
50 fflush((FILE *) ctx);
Gilles Peskine504c1a32021-01-05 23:40:14 +010051}
52
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020053#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010054mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
Gilles Peskine504c1a32021-01-05 23:40:14 +010055{
56 (void) time;
57 return 0x5af2a056;
58}
Raoul Strackx9ed9bc92020-06-22 14:08:57 +020059#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010060
Gilles Peskine8eb29432021-02-03 20:07:11 +010061#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
Gilles Peskine449bd832023-01-11 14:50:10 +010062static int dummy_entropy(void *data, unsigned char *output, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +010063{
64 size_t i;
65 int ret;
66 (void) data;
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 ret = mbedtls_entropy_func(data, output, len);
69 for (i = 0; i < len; i++) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010070 //replace result with pseudo random
71 output[i] = (unsigned char) rand();
72 }
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +010074}
Gilles Peskine8eb29432021-02-03 20:07:11 +010075#endif
Gilles Peskine504c1a32021-01-05 23:40:14 +010076
Gilles Peskine449bd832023-01-11 14:50:10 +010077void rng_init(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010078{
Gilles Peskine8eb29432021-02-03 20:07:11 +010079#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
80 (void) rng;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 psa_crypto_init();
Gilles Peskine8eb29432021-02-03 20:07:11 +010082#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
83
Gilles Peskineba749042021-01-13 20:02:03 +010084#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010085 mbedtls_ctr_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010086#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_hmac_drbg_init(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +010088#else
89#error "No DRBG available"
90#endif
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_entropy_init(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +010093#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +010094}
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
Gilles Peskinedaa94c42021-01-13 18:38:27 +010097{
Gilles Peskineaaedbdc2021-02-03 13:55:22 +010098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (reproducible) {
100 mbedtls_fprintf(stderr,
101 "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
102 return -1;
Gilles Peskineaaedbdc2021-02-03 13:55:22 +0100103 }
104#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100105#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
106 /* The PSA crypto RNG does its own seeding. */
107 (void) rng;
108 (void) pers;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (reproducible) {
110 mbedtls_fprintf(stderr,
111 "The PSA RNG does not support reproducible mode.\n");
112 return -1;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100113 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return 0;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100115#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 int (*f_entropy)(void *, unsigned char *, size_t) =
117 (reproducible ? dummy_entropy : mbedtls_entropy_func);
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (reproducible) {
120 srand(1);
121 }
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100122
Gilles Peskineba749042021-01-13 20:02:03 +0100123#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 int ret = mbedtls_ctr_drbg_seed(&rng->drbg,
125 f_entropy, &rng->entropy,
126 (const unsigned char *) pers,
127 strlen(pers));
Gilles Peskineba749042021-01-13 20:02:03 +0100128#elif defined(MBEDTLS_HMAC_DRBG_C)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100129#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskineba749042021-01-13 20:02:03 +0100130 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100131#elif defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskineba749042021-01-13 20:02:03 +0100132 const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
133#else
134#error "No message digest available for HMAC_DRBG"
135#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
137 mbedtls_md_info_from_type(md_type),
138 f_entropy, &rng->entropy,
139 (const unsigned char *) pers,
140 strlen(pers));
Gilles Peskine8eb29432021-02-03 20:07:11 +0100141#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100142#error "No DRBG available"
Gilles Peskine8eb29432021-02-03 20:07:11 +0100143#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
Gilles Peskineba749042021-01-13 20:02:03 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if (ret != 0) {
146 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
147 (unsigned int) -ret);
148 return ret;
Gilles Peskinef1cb75f2021-01-13 18:46:01 +0100149 }
Gilles Peskine8eb29432021-02-03 20:07:11 +0100150#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return 0;
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100153}
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155void rng_free(rng_context_t *rng)
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100156{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100157#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
158 (void) rng;
159 /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
160 * This is ok because none of our applications try to do any crypto after
161 * deinitializing the RNG. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_psa_crypto_free();
Gilles Peskine8eb29432021-02-03 20:07:11 +0100163#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
164
Gilles Peskineba749042021-01-13 20:02:03 +0100165#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_ctr_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100167#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 mbedtls_hmac_drbg_free(&rng->drbg);
Gilles Peskineba749042021-01-13 20:02:03 +0100169#else
170#error "No DRBG available"
171#endif
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_entropy_free(&rng->entropy);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100174#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskinedaa94c42021-01-13 18:38:27 +0100175}
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177int rng_get(void *p_rng, unsigned char *output, size_t output_len)
Gilles Peskine535fb372021-01-13 18:59:46 +0100178{
Gilles Peskine8eb29432021-02-03 20:07:11 +0100179#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
180 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
182 output, output_len);
Gilles Peskine8eb29432021-02-03 20:07:11 +0100183#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100184 rng_context_t *rng = p_rng;
Gilles Peskine8eb29432021-02-03 20:07:11 +0100185
Gilles Peskineba749042021-01-13 20:02:03 +0100186#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100188#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
Gilles Peskineba749042021-01-13 20:02:03 +0100190#else
191#error "No DRBG available"
192#endif
Gilles Peskine8eb29432021-02-03 20:07:11 +0100193
194#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
Gilles Peskine535fb372021-01-13 18:59:46 +0100195}
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2)
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200198{
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 char *separator;
200 if ((separator = strchr(arg, ',')) == NULL) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200201 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200203 *separator = '\0';
204
205 *alg1 = arg;
206 *alg2 = separator + 1;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (strcmp(*alg1, "rsa-sign-pkcs1") != 0 &&
209 strcmp(*alg1, "rsa-sign-pss") != 0 &&
210 strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
211 strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
212 strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
213 strcmp(*alg1, "rsa-decrypt") != 0 &&
214 strcmp(*alg1, "ecdsa-sign") != 0 &&
215 strcmp(*alg1, "ecdh") != 0) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200216 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (strcmp(*alg2, "rsa-sign-pkcs1") != 0 &&
220 strcmp(*alg2, "rsa-sign-pss") != 0 &&
221 strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
222 strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
223 strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
224 strcmp(*alg2, "rsa-decrypt") != 0 &&
225 strcmp(*alg2, "ecdsa-sign") != 0 &&
226 strcmp(*alg2, "ecdh") != 0 &&
227 strcmp(*alg2, "none") != 0) {
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200228 return 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 }
Przemek Stekiel85d692d2022-04-25 12:42:55 +0200230
231 return 0;
232}
233
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
236 psa_algorithm_t *psa_alg1,
237 psa_algorithm_t *psa_alg2,
238 psa_key_usage_t *usage,
239 mbedtls_pk_type_t key_type)
Przemek Stekiel01396a12022-05-02 13:41:53 +0200240{
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (strcmp(alg1, "none") != 0) {
242 const char *algs[] = { alg1, alg2 };
Przemek Stekielcb20d202022-05-06 08:42:34 +0200243 psa_algorithm_t *psa_algs[] = { psa_alg1, psa_alg2 };
Przemek Stekiel01396a12022-05-02 13:41:53 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 for (int i = 0; i < 2; i++) {
246 if (strcmp(algs[i], "rsa-sign-pkcs1") == 0) {
247 *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200248 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 } else if (strcmp(algs[i], "rsa-sign-pss") == 0) {
250 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200251 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 } else if (strcmp(algs[i], "rsa-sign-pss-sha256") == 0) {
253 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron50969e32022-09-16 15:54:33 +0200254 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 } else if (strcmp(algs[i], "rsa-sign-pss-sha384") == 0) {
256 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron50969e32022-09-16 15:54:33 +0200257 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 } else if (strcmp(algs[i], "rsa-sign-pss-sha512") == 0) {
259 *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron50969e32022-09-16 15:54:33 +0200260 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 } else if (strcmp(algs[i], "rsa-decrypt") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200262 *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_CRYPT;
263 *usage |= PSA_KEY_USAGE_DECRYPT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 } else if (strcmp(algs[i], "ecdsa-sign") == 0) {
265 *psa_algs[i] = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200266 *usage |= PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 } else if (strcmp(algs[i], "ecdh") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200268 *psa_algs[i] = PSA_ALG_ECDH;
269 *usage |= PSA_KEY_USAGE_DERIVE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 } else if (strcmp(algs[i], "none") == 0) {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200271 *psa_algs[i] = PSA_ALG_NONE;
272 }
273 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 } else {
275 if (key_type == MBEDTLS_PK_ECKEY) {
276 *psa_alg1 = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200277 *psa_alg2 = PSA_ALG_ECDH;
278 *usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 } else if (key_type == MBEDTLS_PK_RSA) {
280 *psa_alg1 = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
281 *psa_alg2 = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
Przemek Stekielcb20d202022-05-06 08:42:34 +0200282 *usage = PSA_KEY_USAGE_SIGN_HASH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 } else {
Przemek Stekielcb20d202022-05-06 08:42:34 +0200284 return 1;
285 }
Przemek Stekiel01396a12022-05-02 13:41:53 +0200286 }
287
288 return 0;
289}
Przemek Stekiel76a41f52022-05-04 13:55:23 +0200290#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel01396a12022-05-02 13:41:53 +0200291
Gilles Peskine504c1a32021-01-05 23:40:14 +0100292#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int ca_callback(void *data, mbedtls_x509_crt const *child,
294 mbedtls_x509_crt **candidates)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100295{
296 int ret = 0;
297 mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
298 mbedtls_x509_crt *first;
299
300 /* This is a test-only implementation of the CA callback
301 * which always returns the entire list of trusted certificates.
302 * Production implementations managing a large number of CAs
303 * should use an efficient presentation and lookup for the
304 * set of trusted certificates (such as a hashtable) and only
305 * return those trusted certificates which satisfy basic
306 * parental checks, such as the matching of child `Issuer`
307 * and parent `Subject` field or matching key identifiers. */
308 ((void) child);
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
311 if (first == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100312 ret = -1;
313 goto exit;
314 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 mbedtls_x509_crt_init(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100318 ret = -1;
319 goto exit;
320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 while (ca->next != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100323 ca = ca->next;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100325 ret = -1;
326 goto exit;
327 }
328 }
329
330exit:
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (ret != 0) {
333 mbedtls_x509_crt_free(first);
334 mbedtls_free(first);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100335 first = NULL;
336 }
337
338 *candidates = first;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100340}
341#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343int delayed_recv(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100344{
345 static int first_try = 1;
346 int ret;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100349 first_try = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 ret = mbedtls_net_recv(ctx, buf, len);
354 if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100355 first_try = 1; /* Next call will be a new operation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
357 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100358}
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360int delayed_send(void *ctx, const unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100361{
362 static int first_try = 1;
363 int ret;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (first_try) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100366 first_try = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100368 }
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 ret = mbedtls_net_send(ctx, buf, len);
371 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100372 first_try = 1; /* Next call will be a new operation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 }
374 return ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100375}
376
377#if !defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378int idle(mbedtls_net_context *fd,
379 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100380#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100381int idle(mbedtls_net_context *fd,
382 mbedtls_timing_delay_context *timer,
383 int idle_reason)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100384#endif
385{
386 int ret;
387 int poll_type = 0;
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100390 poll_type = MBEDTLS_NET_POLL_WRITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100392 poll_type = MBEDTLS_NET_POLL_READ;
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100394#if !defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 else {
396 return 0;
397 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100398#endif
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 while (1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100401 /* Check if timer has expired */
402#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (timer != NULL &&
404 mbedtls_timing_get_delay(timer) == 2) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100405 break;
406 }
407#endif /* MBEDTLS_TIMING_C */
408
409 /* Check if underlying transport became available */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (poll_type != 0) {
411 ret = mbedtls_net_poll(fd, poll_type, 0);
412 if (ret < 0) {
413 return ret;
414 }
415 if (ret == poll_type) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100416 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100418 }
419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return 0;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100422}
423
Gilles Peskine53dea742021-02-02 22:55:06 +0100424#if defined(MBEDTLS_TEST_HOOKS)
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426void test_hooks_init(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100427{
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 mbedtls_test_info_reset();
Gilles Peskinee374b952021-02-03 00:05:19 +0100429
430#if defined(MBEDTLS_TEST_MUTEX_USAGE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_test_mutex_usage_init();
Gilles Peskinee374b952021-02-03 00:05:19 +0100432#endif
Gilles Peskine53dea742021-02-02 22:55:06 +0100433}
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435int test_hooks_failure_detected(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100436{
Gilles Peskinee374b952021-02-03 00:05:19 +0100437#if defined(MBEDTLS_TEST_MUTEX_USAGE)
438 /* Errors are reported via mbedtls_test_info. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_test_mutex_usage_check();
Gilles Peskinee374b952021-02-03 00:05:19 +0100440#endif
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
443 return 1;
444 }
445 return 0;
Gilles Peskine53dea742021-02-02 22:55:06 +0100446}
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448void test_hooks_free(void)
Gilles Peskine53dea742021-02-02 22:55:06 +0100449{
450}
451
452#endif /* MBEDTLS_TEST_HOOKS */
453
Valerio Settiacd32c02023-06-29 18:06:29 +0200454static const struct {
455 uint16_t tls_id;
456 const char *name;
457 uint8_t is_supported;
458} tls_id_curve_name_table[] =
459{
460#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settideb67642023-07-03 14:26:04 +0200461 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200462#else
Valerio Settideb67642023-07-03 14:26:04 +0200463 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200464#endif
465#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settideb67642023-07-03 14:26:04 +0200466 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200467#else
Valerio Settideb67642023-07-03 14:26:04 +0200468 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200469#endif
470#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settideb67642023-07-03 14:26:04 +0200471 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200472#else
Valerio Settideb67642023-07-03 14:26:04 +0200473 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200474#endif
475#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settideb67642023-07-03 14:26:04 +0200476 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200477#else
Valerio Settideb67642023-07-03 14:26:04 +0200478 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200479#endif
480#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settideb67642023-07-03 14:26:04 +0200481 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200482#else
Valerio Settideb67642023-07-03 14:26:04 +0200483 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200484#endif
485#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settideb67642023-07-03 14:26:04 +0200486 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200487#else
Valerio Settideb67642023-07-03 14:26:04 +0200488 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200489#endif
490#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settideb67642023-07-03 14:26:04 +0200491 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200492#else
Valerio Settideb67642023-07-03 14:26:04 +0200493 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200494#endif
495#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settideb67642023-07-03 14:26:04 +0200496 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200497#else
Valerio Settideb67642023-07-03 14:26:04 +0200498 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200499#endif
500#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Settideb67642023-07-03 14:26:04 +0200501 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200502#else
Valerio Settideb67642023-07-03 14:26:04 +0200503 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200504#endif
505#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settideb67642023-07-03 14:26:04 +0200506 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200507#else
Valerio Settideb67642023-07-03 14:26:04 +0200508 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200509#endif
510#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settideb67642023-07-03 14:26:04 +0200511 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200512#else
Valerio Settideb67642023-07-03 14:26:04 +0200513 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200514#endif
515#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settideb67642023-07-03 14:26:04 +0200516 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200517#else
Valerio Settideb67642023-07-03 14:26:04 +0200518 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200519#endif
520#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settideb67642023-07-03 14:26:04 +0200521 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448", 1 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200522#else
Valerio Settideb67642023-07-03 14:26:04 +0200523 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448", 0 },
Valerio Settiacd32c02023-06-29 18:06:29 +0200524#endif
Valerio Settideb67642023-07-03 14:26:04 +0200525#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
526 defined(PSA_WANT_ALG_FFDH)
527 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, "ffdhe2048", 1 },
528 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, "ffdhe3072", 1 },
529 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, "ffdhe4096", 1 },
530 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, "ffdhe6144", 1 },
531 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, "ffdhe8192", 1 },
532#else
533 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, "ffdhe2048", 0 },
534 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, "ffdhe3072", 0 },
535 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, "ffdhe4096", 0 },
536 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, "ffdhe6144", 0 },
537 { MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, "ffdhe8192", 0 },
538#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */
Valerio Settiacd32c02023-06-29 18:06:29 +0200539 { 0, NULL, 0 },
540};
541
542static uint16_t mbedtls_ssl_get_curve_tls_id_from_name(const char *name)
543{
544 if (name == NULL) {
545 return 0;
546 }
547
548 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
549 if (strcmp(tls_id_curve_name_table[i].name, name) == 0) {
550 return tls_id_curve_name_table[i].tls_id;
551 }
552 }
553
554 return 0;
555}
556
557static const char **mbedtls_ssl_get_supported_curves_list(void)
558{
559 const char **supported_list = NULL;
560 int i = 0, j = 0;
561
562 /* The allocated area might be bigger than strictly required (because not
563 * all the curves might be supported), but it is enough to contain all the
564 * pointers when all curves are enabled. */
565 supported_list = mbedtls_calloc(ARRAY_LENGTH(tls_id_curve_name_table),
566 sizeof(char *));
567 for (i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
568 if (tls_id_curve_name_table[i].is_supported == 1) {
569 supported_list[j] = tls_id_curve_name_table[i].name;
570 j++;
571 }
572 }
573 // Keep NULL as last element as guard for end-of-array.
574 supported_list[j] = NULL;
575
576 return supported_list;
577}
578
579int parse_curves(const char *curves, uint16_t *group_list, size_t group_list_len)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200580{
Przemek Stekiel45255e42023-06-29 13:56:36 +0200581 char *p = (char *) groups;
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200582 char *q = NULL;
583 size_t i = 0;
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200584
585 if (strcmp(p, "none") == 0) {
586 group_list[0] = 0;
587 } else if (strcmp(p, "default") != 0) {
Przemek Stekiel68e75442023-07-06 11:21:39 +0200588 /* Leave room for a final NULL in group list */
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200589 while (i < group_list_len - 1 && *p != '\0') {
Valerio Settiacd32c02023-06-29 18:06:29 +0200590 uint16_t curve_tls_id;
Valerio Settideb67642023-07-03 14:26:04 +0200591 q = p;
Valerio Settiacd32c02023-06-29 18:06:29 +0200592
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200593 /* Terminate the current string */
594 while (*p != ',' && *p != '\0') {
595 p++;
596 }
597 if (*p == ',') {
598 *p++ = '\0';
599 }
600
Valerio Settiacd32c02023-06-29 18:06:29 +0200601 if ((curve_tls_id = mbedtls_ssl_get_curve_tls_id_from_name(q)) != 0) {
602 group_list[i++] = curve_tls_id;
Valerio Settideb67642023-07-03 14:26:04 +0200603 } else {
Valerio Settiacd32c02023-06-29 18:06:29 +0200604 mbedtls_printf("unknown curve %s\n", q);
605 mbedtls_printf("supported curves: ");
606 const char **supported_curves = mbedtls_ssl_get_supported_curves_list();
607 for (int index = 0;
608 supported_curves[index] != NULL;
609 index++) {
610 mbedtls_printf("%s ", supported_curves[index]);
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200611 }
Valerio Settiacd32c02023-06-29 18:06:29 +0200612 mbedtls_free((char *) supported_curves);
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200613 mbedtls_printf("\n");
614 return -1;
615 }
616 }
617
Przemek Stekiel45255e42023-06-29 13:56:36 +0200618 mbedtls_printf("Number of groups: %u\n", (unsigned int) i);
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200619
620 if (i == group_list_len - 1 && *p != '\0') {
Przemek Stekiel45255e42023-06-29 13:56:36 +0200621 mbedtls_printf("groups list too long, maximum %u",
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200622 (unsigned int) (group_list_len - 1));
623 return -1;
624 }
625
626 group_list[i] = 0;
627 }
628
629 return 0;
630}
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200631
Gilles Peskineab7ce962021-01-05 21:27:53 +0100632#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */