blob: 57bfdf81287b8c35e3d4bf15a693ce4642b48521 [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
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200454#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
455 defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel7d42c0d2023-06-13 11:49:11 +0200456
457/* Finite Field Group Names (DHE) */
458#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048 "ffdhe2048"
459#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072 "ffdhe3072"
460#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096 "ffdhe4096"
461#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144 "ffdhe6144"
462#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192 "ffdhe8192"
463
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200464static uint16_t mbedtls_ssl_ffdh_group_from_name(const char *name)
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200465{
466 if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048) == 0) {
467 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048;
468 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072) == 0) {
469 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072;
470 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096) == 0) {
471 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096;
472 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144) == 0) {
473 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144;
474 } else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192) == 0) {
475 return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192;
476 }
477 return 0;
478}
479
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200480static const uint16_t *mbedtls_ssl_ffdh_supported_groups(void)
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200481{
Przemek Stekielda4fba62023-06-02 14:52:28 +0200482 static const uint16_t ffdh_groups[] = {
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200483 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
484 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
485 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
486 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
487 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
488 0
489 };
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200490 return ffdh_groups;
Przemek Stekiel7d42c0d2023-06-13 11:49:11 +0200491}
492
493static inline const char *mbedtls_ssl_ffdh_name_from_group(uint16_t group)
494{
495 switch (group) {
496 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
497 return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048;
498 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
499 return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072;
500 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
501 return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096;
502 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
503 return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144;
504 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
505 return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192;
506 default:
507 return NULL;
508 }
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200509 return NULL;
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200510}
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200511#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */
Przemek Stekiele7db09b2023-05-31 11:29:55 +0200512
Valerio Settiacd32c02023-06-29 18:06:29 +0200513static const struct {
514 uint16_t tls_id;
515 const char *name;
516 uint8_t is_supported;
517} tls_id_curve_name_table[] =
518{
519#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
520 { 25, "secp521r1", 1 },
521#else
522 { 25, "secp521r1", 0 },
523#endif
524#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
525 { 28, "brainpoolP512r1", 1 },
526#else
527 { 28, "brainpoolP512r1", 0 },
528#endif
529#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
530 { 24, "secp384r1", 1 },
531#else
532 { 24, "secp384r1", 0 },
533#endif
534#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
535 { 27, "brainpoolP384r1", 1 },
536#else
537 { 27, "brainpoolP384r1", 0 },
538#endif
539#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
540 { 23, "secp256r1", 1 },
541#else
542 { 23, "secp256r1", 0 },
543#endif
544#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
545 { 22, "secp256k1", 1 },
546#else
547 { 22, "secp256k1", 0 },
548#endif
549#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
550 { 26, "brainpoolP256r1", 1 },
551#else
552 { 26, "brainpoolP256r1", 0 },
553#endif
554#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
555 { 21, "secp224r1", 1 },
556#else
557 { 21, "secp224r1", 0 },
558#endif
559#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
560 { 20, "secp224k1", 1 },
561#else
562 { 20, "secp224k1", 0 },
563#endif
564#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
565 { 19, "secp192r1", 1 },
566#else
567 { 19, "secp192r1", 0 },
568#endif
569#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
570 { 18, "secp192k1", 1 },
571#else
572 { 18, "secp192k1", 0 },
573#endif
574#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
575 { 29, "x25519", 1 },
576#else
577 { 29, "x25519", 0 },
578#endif
579#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
580 { 30, "x448", 1 },
581#else
582 { 30, "x448", 0 },
583#endif
584 { 0, NULL, 0 },
585};
586
587static uint16_t mbedtls_ssl_get_curve_tls_id_from_name(const char *name)
588{
589 if (name == NULL) {
590 return 0;
591 }
592
593 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
594 if (strcmp(tls_id_curve_name_table[i].name, name) == 0) {
595 return tls_id_curve_name_table[i].tls_id;
596 }
597 }
598
599 return 0;
600}
601
602static const char **mbedtls_ssl_get_supported_curves_list(void)
603{
604 const char **supported_list = NULL;
605 int i = 0, j = 0;
606
607 /* The allocated area might be bigger than strictly required (because not
608 * all the curves might be supported), but it is enough to contain all the
609 * pointers when all curves are enabled. */
610 supported_list = mbedtls_calloc(ARRAY_LENGTH(tls_id_curve_name_table),
611 sizeof(char *));
612 for (i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
613 if (tls_id_curve_name_table[i].is_supported == 1) {
614 supported_list[j] = tls_id_curve_name_table[i].name;
615 j++;
616 }
617 }
618 // Keep NULL as last element as guard for end-of-array.
619 supported_list[j] = NULL;
620
621 return supported_list;
622}
623
624int parse_curves(const char *curves, uint16_t *group_list, size_t group_list_len)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200625{
Przemek Stekiel45255e42023-06-29 13:56:36 +0200626 char *p = (char *) groups;
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200627 char *q = NULL;
628 size_t i = 0;
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200629
630 if (strcmp(p, "none") == 0) {
631 group_list[0] = 0;
632 } else if (strcmp(p, "default") != 0) {
Przemek Stekiel68e75442023-07-06 11:21:39 +0200633 /* Leave room for a final NULL in group list */
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200634 while (i < group_list_len - 1 && *p != '\0') {
635 q = p;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200636#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
Przemek Stekiel7d42c0d2023-06-13 11:49:11 +0200637 defined(PSA_WANT_ALG_FFDH)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200638 uint16_t ffdh_group = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200639#endif
Valerio Settiacd32c02023-06-29 18:06:29 +0200640 uint16_t curve_tls_id;
641
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200642 /* Terminate the current string */
643 while (*p != ',' && *p != '\0') {
644 p++;
645 }
646 if (*p == ',') {
647 *p++ = '\0';
648 }
649
Valerio Settiacd32c02023-06-29 18:06:29 +0200650 if ((curve_tls_id = mbedtls_ssl_get_curve_tls_id_from_name(q)) != 0) {
651 group_list[i++] = curve_tls_id;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200652 } else
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200653#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
Przemek Stekiel7d42c0d2023-06-13 11:49:11 +0200654 defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200655 if ((ffdh_group = mbedtls_ssl_ffdh_group_from_name(q)) != 0) {
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200656 group_list[i++] = ffdh_group;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200657 } else
658#endif
659 {
Valerio Settiacd32c02023-06-29 18:06:29 +0200660 mbedtls_printf("unknown curve %s\n", q);
661 mbedtls_printf("supported curves: ");
662 const char **supported_curves = mbedtls_ssl_get_supported_curves_list();
663 for (int index = 0;
664 supported_curves[index] != NULL;
665 index++) {
666 mbedtls_printf("%s ", supported_curves[index]);
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200667 }
Valerio Settiacd32c02023-06-29 18:06:29 +0200668 mbedtls_free((char *) supported_curves);
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200669#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
Przemek Stekiel7d42c0d2023-06-13 11:49:11 +0200670 defined(PSA_WANT_ALG_FFDH)
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200671 const uint16_t *supported_ffdh_group = mbedtls_ssl_ffdh_supported_groups();
672 while (*supported_ffdh_group != 0) {
673 mbedtls_printf("%s ",
674 mbedtls_ssl_ffdh_name_from_group(*supported_ffdh_group));
675 supported_ffdh_group++;
676 }
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +0200677#endif
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200678 mbedtls_printf("\n");
679 return -1;
680 }
681 }
682
Przemek Stekiel45255e42023-06-29 13:56:36 +0200683 mbedtls_printf("Number of groups: %u\n", (unsigned int) i);
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200684
685 if (i == group_list_len - 1 && *p != '\0') {
Przemek Stekiel45255e42023-06-29 13:56:36 +0200686 mbedtls_printf("groups list too long, maximum %u",
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200687 (unsigned int) (group_list_len - 1));
688 return -1;
689 }
690
691 group_list[i] = 0;
692 }
693
694 return 0;
695}
Przemek Stekielff9fcbc2023-06-05 12:32:55 +0200696
Gilles Peskineab7ce962021-01-05 21:27:53 +0100697#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */