2021-01-05 21:11:16 +01:00
|
|
|
/*
|
2021-01-05 23:34:27 +01:00
|
|
|
* Common code library for SSL test programs.
|
|
|
|
*
|
|
|
|
* In addition to the functions in this file, there is shared source code
|
|
|
|
* that cannot be compiled separately in "ssl_test_common_source.c".
|
2021-01-05 21:11:16 +01:00
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-01-05 21:11:16 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-19 17:54:54 +02:00
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2021-01-05 21:11:16 +01:00
|
|
|
#include "ssl_test_lib.h"
|
|
|
|
|
2021-02-03 00:05:19 +01:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
#include "test/helpers.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-05 21:27:53 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
|
|
|
|
|
2023-06-29 18:06:29 +02:00
|
|
|
#define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0]))
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void my_debug(void *ctx, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *str)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
const char *p, *basename;
|
|
|
|
|
|
|
|
/* Extract basename from file */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (p = basename = file; *p != '\0'; p++) {
|
|
|
|
if (*p == '/' || *p == '\\') {
|
2021-01-05 23:40:14 +01:00
|
|
|
basename = p + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 23:40:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s",
|
|
|
|
basename, line, level, str);
|
|
|
|
fflush((FILE *) ctx);
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 14:08:57 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
(void) time;
|
|
|
|
return 0x5af2a056;
|
|
|
|
}
|
2020-06-22 14:08:57 +02:00
|
|
|
#endif
|
2021-01-05 23:40:14 +01:00
|
|
|
|
2021-02-03 20:07:11 +01:00
|
|
|
#if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int dummy_entropy(void *data, unsigned char *output, size_t len)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ret;
|
|
|
|
(void) data;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_entropy_func(data, output, len);
|
|
|
|
for (i = 0; i < len; i++) {
|
2021-01-05 23:40:14 +01:00
|
|
|
//replace result with pseudo random
|
|
|
|
output[i] = (unsigned char) rand();
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
2021-02-03 20:07:11 +01:00
|
|
|
#endif
|
2021-01-05 23:40:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void rng_init(rng_context_t *rng)
|
2021-01-13 18:38:27 +01:00
|
|
|
{
|
2021-02-03 20:07:11 +01:00
|
|
|
#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
|
|
|
|
(void) rng;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_crypto_init();
|
2021-02-03 20:07:11 +01:00
|
|
|
#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
|
|
|
|
2021-01-13 20:02:03 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&rng->drbg);
|
2021-01-13 20:02:03 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_init(&rng->drbg);
|
2021-01-13 20:02:03 +01:00
|
|
|
#else
|
|
|
|
#error "No DRBG available"
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&rng->entropy);
|
2021-02-03 20:07:11 +01:00
|
|
|
#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2021-01-13 18:38:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
|
2021-01-13 18:38:27 +01:00
|
|
|
{
|
2021-02-03 13:55:22 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reproducible) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
|
|
|
|
return -1;
|
2021-02-03 13:55:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
2021-02-03 20:07:11 +01:00
|
|
|
#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
|
|
|
|
/* The PSA crypto RNG does its own seeding. */
|
|
|
|
(void) rng;
|
|
|
|
(void) pers;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reproducible) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"The PSA RNG does not support reproducible mode.\n");
|
|
|
|
return -1;
|
2021-02-03 20:07:11 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-02-03 20:07:11 +01:00
|
|
|
#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*f_entropy)(void *, unsigned char *, size_t) =
|
|
|
|
(reproducible ? dummy_entropy : mbedtls_entropy_func);
|
2021-01-13 18:38:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reproducible) {
|
|
|
|
srand(1);
|
|
|
|
}
|
2021-01-13 18:46:01 +01:00
|
|
|
|
2021-01-13 20:02:03 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_ctr_drbg_seed(&rng->drbg,
|
|
|
|
f_entropy, &rng->entropy,
|
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen(pers));
|
2021-01-13 20:02:03 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2021-01-13 20:02:03 +01:00
|
|
|
const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
|
2023-03-17 12:50:01 +01:00
|
|
|
#elif defined(MBEDTLS_MD_CAN_SHA512)
|
2021-01-13 20:02:03 +01:00
|
|
|
const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
|
|
|
|
#else
|
|
|
|
#error "No message digest available for HMAC_DRBG"
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
|
|
|
|
mbedtls_md_info_from_type(md_type),
|
|
|
|
f_entropy, &rng->entropy,
|
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen(pers));
|
2021-02-03 20:07:11 +01:00
|
|
|
#else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
|
2021-01-13 20:02:03 +01:00
|
|
|
#error "No DRBG available"
|
2021-02-03 20:07:11 +01:00
|
|
|
#endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
|
2021-01-13 20:02:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
|
|
|
|
(unsigned int) -ret);
|
|
|
|
return ret;
|
2021-01-13 18:38:27 +01:00
|
|
|
}
|
2021-02-03 20:07:11 +01:00
|
|
|
#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2021-01-13 18:38:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-01-13 18:38:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void rng_free(rng_context_t *rng)
|
2021-01-13 18:38:27 +01:00
|
|
|
{
|
2021-02-03 20:07:11 +01:00
|
|
|
#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
|
|
|
|
(void) rng;
|
|
|
|
/* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
|
|
|
|
* This is ok because none of our applications try to do any crypto after
|
|
|
|
* deinitializing the RNG. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_psa_crypto_free();
|
2021-02-03 20:07:11 +01:00
|
|
|
#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
|
|
|
|
2021-01-13 20:02:03 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&rng->drbg);
|
2021-01-13 20:02:03 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(&rng->drbg);
|
2021-01-13 20:02:03 +01:00
|
|
|
#else
|
|
|
|
#error "No DRBG available"
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_free(&rng->entropy);
|
2021-02-03 20:07:11 +01:00
|
|
|
#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2021-01-13 18:38:27 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int rng_get(void *p_rng, unsigned char *output, size_t output_len)
|
2021-01-13 18:59:46 +01:00
|
|
|
{
|
2021-02-03 20:07:11 +01:00
|
|
|
#if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
|
|
|
|
(void) p_rng;
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output, output_len);
|
2021-02-03 20:07:11 +01:00
|
|
|
#else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2021-01-13 18:59:46 +01:00
|
|
|
rng_context_t *rng = p_rng;
|
2021-02-03 20:07:11 +01:00
|
|
|
|
2021-01-13 20:02:03 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
|
2021-01-13 20:02:03 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
|
2021-01-13 20:02:03 +01:00
|
|
|
#else
|
|
|
|
#error "No DRBG available"
|
|
|
|
#endif
|
2021-02-03 20:07:11 +01:00
|
|
|
|
|
|
|
#endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
|
2021-01-13 18:59:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2)
|
2022-04-25 12:42:55 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
char *separator;
|
|
|
|
if ((separator = strchr(arg, ',')) == NULL) {
|
2022-04-25 12:42:55 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-25 12:42:55 +02:00
|
|
|
*separator = '\0';
|
|
|
|
|
|
|
|
*alg1 = arg;
|
|
|
|
*alg2 = separator + 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(*alg1, "rsa-sign-pkcs1") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-decrypt") != 0 &&
|
|
|
|
strcmp(*alg1, "ecdsa-sign") != 0 &&
|
|
|
|
strcmp(*alg1, "ecdh") != 0) {
|
2022-04-25 12:42:55 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-25 12:42:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(*alg2, "rsa-sign-pkcs1") != 0 &&
|
|
|
|
strcmp(*alg2, "rsa-sign-pss") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
|
|
|
|
strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
|
|
|
|
strcmp(*alg2, "rsa-decrypt") != 0 &&
|
|
|
|
strcmp(*alg2, "ecdsa-sign") != 0 &&
|
|
|
|
strcmp(*alg2, "ecdh") != 0 &&
|
|
|
|
strcmp(*alg2, "none") != 0) {
|
2022-04-25 12:42:55 +02:00
|
|
|
return 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-25 12:42:55 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-04 13:55:23 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
|
|
|
|
psa_algorithm_t *psa_alg1,
|
|
|
|
psa_algorithm_t *psa_alg2,
|
|
|
|
psa_key_usage_t *usage,
|
|
|
|
mbedtls_pk_type_t key_type)
|
2022-05-02 13:41:53 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(alg1, "none") != 0) {
|
|
|
|
const char *algs[] = { alg1, alg2 };
|
2022-05-06 08:42:34 +02:00
|
|
|
psa_algorithm_t *psa_algs[] = { psa_alg1, psa_alg2 };
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (strcmp(algs[i], "rsa-sign-pkcs1") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
|
2022-05-06 08:42:34 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "rsa-sign-pss") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
|
2022-05-06 08:42:34 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "rsa-sign-pss-sha256") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
|
2022-09-16 15:54:33 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "rsa-sign-pss-sha384") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
|
2022-09-16 15:54:33 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "rsa-sign-pss-sha512") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
|
2022-09-16 15:54:33 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "rsa-decrypt") == 0) {
|
2022-05-06 08:42:34 +02:00
|
|
|
*psa_algs[i] = PSA_ALG_RSA_PKCS1V15_CRYPT;
|
|
|
|
*usage |= PSA_KEY_USAGE_DECRYPT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "ecdsa-sign") == 0) {
|
|
|
|
*psa_algs[i] = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
|
2022-05-06 08:42:34 +02:00
|
|
|
*usage |= PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "ecdh") == 0) {
|
2022-05-06 08:42:34 +02:00
|
|
|
*psa_algs[i] = PSA_ALG_ECDH;
|
|
|
|
*usage |= PSA_KEY_USAGE_DERIVE;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(algs[i], "none") == 0) {
|
2022-05-06 08:42:34 +02:00
|
|
|
*psa_algs[i] = PSA_ALG_NONE;
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if (key_type == MBEDTLS_PK_ECKEY) {
|
|
|
|
*psa_alg1 = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
|
2022-05-06 08:42:34 +02:00
|
|
|
*psa_alg2 = PSA_ALG_ECDH;
|
|
|
|
*usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (key_type == MBEDTLS_PK_RSA) {
|
|
|
|
*psa_alg1 = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
|
|
|
|
*psa_alg2 = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
|
2022-05-06 08:42:34 +02:00
|
|
|
*usage = PSA_KEY_USAGE_SIGN_HASH;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-05-06 08:42:34 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2022-05-02 13:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-04 13:55:23 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-05-02 13:41:53 +02:00
|
|
|
|
2021-01-05 23:40:14 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
2023-01-11 14:50:10 +01:00
|
|
|
int ca_callback(void *data, mbedtls_x509_crt const *child,
|
|
|
|
mbedtls_x509_crt **candidates)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
|
|
|
|
mbedtls_x509_crt *first;
|
|
|
|
|
|
|
|
/* This is a test-only implementation of the CA callback
|
|
|
|
* which always returns the entire list of trusted certificates.
|
|
|
|
* Production implementations managing a large number of CAs
|
|
|
|
* should use an efficient presentation and lookup for the
|
|
|
|
* set of trusted certificates (such as a hashtable) and only
|
|
|
|
* return those trusted certificates which satisfy basic
|
|
|
|
* parental checks, such as the matching of child `Issuer`
|
|
|
|
* and parent `Subject` field or matching key identifiers. */
|
|
|
|
((void) child);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
|
|
|
|
if (first == NULL) {
|
2021-01-05 23:40:14 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(first);
|
2021-01-05 23:40:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
|
2021-01-05 23:40:14 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ca->next != NULL) {
|
2021-01-05 23:40:14 +01:00
|
|
|
ca = ca->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
|
2021-01-05 23:40:14 +01:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_x509_crt_free(first);
|
|
|
|
mbedtls_free(first);
|
2021-01-05 23:40:14 +01:00
|
|
|
first = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*candidates = first;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int delayed_recv(void *ctx, unsigned char *buf, size_t len)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
static int first_try = 1;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (first_try) {
|
2021-01-05 23:40:14 +01:00
|
|
|
first_try = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_net_recv(ctx, buf, len);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
|
2021-01-05 23:40:14 +01:00
|
|
|
first_try = 1; /* Next call will be a new operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return ret;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int delayed_send(void *ctx, const unsigned char *buf, size_t len)
|
2021-01-05 23:40:14 +01:00
|
|
|
{
|
|
|
|
static int first_try = 1;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (first_try) {
|
2021-01-05 23:40:14 +01:00
|
|
|
first_try = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_net_send(ctx, buf, len);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2021-01-05 23:40:14 +01:00
|
|
|
first_try = 1; /* Next call will be a new operation */
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
return ret;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_TIMING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int idle(mbedtls_net_context *fd,
|
|
|
|
int idle_reason)
|
2021-01-05 23:40:14 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
int idle(mbedtls_net_context *fd,
|
|
|
|
mbedtls_timing_delay_context *timer,
|
|
|
|
int idle_reason)
|
2021-01-05 23:40:14 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int poll_type = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2021-01-05 23:40:14 +01:00
|
|
|
poll_type = MBEDTLS_NET_POLL_WRITE;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) {
|
2021-01-05 23:40:14 +01:00
|
|
|
poll_type = MBEDTLS_NET_POLL_READ;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-05 23:40:14 +01:00
|
|
|
#if !defined(MBEDTLS_TIMING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-05 23:40:14 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
2021-01-05 23:40:14 +01:00
|
|
|
/* Check if timer has expired */
|
|
|
|
#if defined(MBEDTLS_TIMING_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (timer != NULL &&
|
|
|
|
mbedtls_timing_get_delay(timer) == 2) {
|
2021-01-05 23:40:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_TIMING_C */
|
|
|
|
|
|
|
|
/* Check if underlying transport became available */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (poll_type != 0) {
|
|
|
|
ret = mbedtls_net_poll(fd, poll_type, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (ret == poll_type) {
|
2021-01-05 23:40:14 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-01-05 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 22:55:06 +01:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void test_hooks_init(void)
|
2021-02-02 22:55:06 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_info_reset();
|
2021-02-03 00:05:19 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_mutex_usage_init();
|
2021-02-03 00:05:19 +01:00
|
|
|
#endif
|
2021-02-02 22:55:06 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int test_hooks_failure_detected(void)
|
2021-02-02 22:55:06 +01:00
|
|
|
{
|
2021-02-03 00:05:19 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
|
|
|
/* Errors are reported via mbedtls_test_info. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_mutex_usage_check();
|
2021-02-03 00:05:19 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2021-02-02 22:55:06 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void test_hooks_free(void)
|
2021-02-02 22:55:06 +01:00
|
|
|
{
|
2023-11-23 19:49:43 +01:00
|
|
|
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
|
|
|
|
mbedtls_test_mutex_usage_end();
|
|
|
|
#endif
|
2021-02-02 22:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_TEST_HOOKS */
|
|
|
|
|
2023-06-29 18:06:29 +02:00
|
|
|
static const struct {
|
|
|
|
uint16_t tls_id;
|
|
|
|
const char *name;
|
|
|
|
uint8_t is_supported;
|
2023-07-07 10:52:18 +02:00
|
|
|
} tls_id_group_name_table[] =
|
2023-06-29 18:06:29 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448", 1 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#else
|
2023-07-03 14:26:04 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448", 0 },
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif
|
2023-07-03 14:26:04 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
|
|
|
|
defined(PSA_WANT_ALG_FFDH)
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, "ffdhe2048", 1 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, "ffdhe3072", 1 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, "ffdhe4096", 1 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, "ffdhe6144", 1 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, "ffdhe8192", 1 },
|
|
|
|
#else
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048, "ffdhe2048", 0 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072, "ffdhe3072", 0 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096, "ffdhe4096", 0 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144, "ffdhe6144", 0 },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192, "ffdhe8192", 0 },
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint16_t mbedtls_ssl_get_curve_tls_id_from_name(const char *name)
|
|
|
|
{
|
|
|
|
if (name == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-07 10:52:18 +02:00
|
|
|
for (int i = 0; tls_id_group_name_table[i].tls_id != 0; i++) {
|
|
|
|
if (strcmp(tls_id_group_name_table[i].name, name) == 0) {
|
|
|
|
return tls_id_group_name_table[i].tls_id;
|
2023-06-29 18:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-10 10:22:51 +02:00
|
|
|
static void mbedtls_ssl_print_supported_groups_list(void)
|
2023-06-29 18:06:29 +02:00
|
|
|
{
|
2023-07-07 10:52:18 +02:00
|
|
|
for (int i = 0; tls_id_group_name_table[i].tls_id != 0; i++) {
|
|
|
|
if (tls_id_group_name_table[i].is_supported == 1) {
|
|
|
|
mbedtls_printf("%s ", tls_id_group_name_table[i].name);
|
2023-06-29 18:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 10:22:51 +02:00
|
|
|
int parse_groups(const char *groups, uint16_t *group_list, size_t group_list_len)
|
2023-06-05 12:32:55 +02:00
|
|
|
{
|
2023-06-29 13:56:36 +02:00
|
|
|
char *p = (char *) groups;
|
2023-06-05 12:32:55 +02:00
|
|
|
char *q = NULL;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (strcmp(p, "none") == 0) {
|
|
|
|
group_list[0] = 0;
|
|
|
|
} else if (strcmp(p, "default") != 0) {
|
2023-07-06 11:21:39 +02:00
|
|
|
/* Leave room for a final NULL in group list */
|
2023-06-05 12:32:55 +02:00
|
|
|
while (i < group_list_len - 1 && *p != '\0') {
|
2023-06-29 18:06:29 +02:00
|
|
|
uint16_t curve_tls_id;
|
2023-07-03 14:26:04 +02:00
|
|
|
q = p;
|
2023-06-29 18:06:29 +02:00
|
|
|
|
2023-06-05 12:32:55 +02:00
|
|
|
/* Terminate the current string */
|
|
|
|
while (*p != ',' && *p != '\0') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == ',') {
|
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:06:29 +02:00
|
|
|
if ((curve_tls_id = mbedtls_ssl_get_curve_tls_id_from_name(q)) != 0) {
|
|
|
|
group_list[i++] = curve_tls_id;
|
2023-07-03 14:26:04 +02:00
|
|
|
} else {
|
2023-07-10 10:22:51 +02:00
|
|
|
mbedtls_printf("unknown group %s\n", q);
|
|
|
|
mbedtls_printf("supported groups: ");
|
|
|
|
mbedtls_ssl_print_supported_groups_list();
|
2023-06-05 12:32:55 +02:00
|
|
|
mbedtls_printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:56:36 +02:00
|
|
|
mbedtls_printf("Number of groups: %u\n", (unsigned int) i);
|
2023-06-05 12:32:55 +02:00
|
|
|
|
|
|
|
if (i == group_list_len - 1 && *p != '\0') {
|
2023-06-29 13:56:36 +02:00
|
|
|
mbedtls_printf("groups list too long, maximum %u",
|
2023-06-05 12:32:55 +02:00
|
|
|
(unsigned int) (group_list_len - 1));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
group_list[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-05 21:27:53 +01:00
|
|
|
#endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */
|