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
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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-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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_TEST_HOOKS */
|
|
|
|
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
|
|
|
|
defined(PSA_WANT_ALG_FFDH)
|
2023-06-13 11:49:11 +02:00
|
|
|
|
|
|
|
/* Finite Field Group Names (DHE) */
|
|
|
|
#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048 "ffdhe2048"
|
|
|
|
#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072 "ffdhe3072"
|
|
|
|
#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096 "ffdhe4096"
|
|
|
|
#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144 "ffdhe6144"
|
|
|
|
#define MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192 "ffdhe8192"
|
|
|
|
|
2023-06-12 11:21:18 +02:00
|
|
|
static uint16_t mbedtls_ssl_ffdh_group_from_name(const char *name)
|
2023-05-31 11:29:55 +02:00
|
|
|
{
|
|
|
|
if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048) == 0) {
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048;
|
|
|
|
} else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072) == 0) {
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072;
|
|
|
|
} else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096) == 0) {
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096;
|
|
|
|
} else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144) == 0) {
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144;
|
|
|
|
} else if (strcmp(name, MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192) == 0) {
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-12 11:21:18 +02:00
|
|
|
static const uint16_t *mbedtls_ssl_ffdh_supported_groups(void)
|
2023-05-31 11:29:55 +02:00
|
|
|
{
|
2023-06-02 14:52:28 +02:00
|
|
|
static const uint16_t ffdh_groups[] = {
|
2023-05-31 11:29:55 +02:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return ffdh_groups;
|
2023-06-13 11:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *mbedtls_ssl_ffdh_name_from_group(uint16_t group)
|
|
|
|
{
|
|
|
|
switch (group) {
|
|
|
|
case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE2048;
|
|
|
|
case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE3072;
|
|
|
|
case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE4096;
|
|
|
|
case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE6144;
|
|
|
|
case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
|
|
|
|
return MBEDTLS_SSL_IANA_TLS_GROUP_NAME_FFDHE8192;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
return NULL;
|
2023-05-31 11:29:55 +02:00
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */
|
2023-05-31 11:29:55 +02:00
|
|
|
|
2023-06-05 12:32:55 +02:00
|
|
|
int parse_curves(const char *curves, uint16_t *group_list, size_t group_list_len)
|
|
|
|
{
|
|
|
|
char *p = (char *) curves;
|
|
|
|
char *q = NULL;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (strcmp(p, "none") == 0) {
|
|
|
|
group_list[0] = 0;
|
|
|
|
} else if (strcmp(p, "default") != 0) {
|
|
|
|
/* Leave room for a final NULL in curve list */
|
|
|
|
while (i < group_list_len - 1 && *p != '\0') {
|
|
|
|
q = p;
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
|
2023-06-13 11:49:11 +02:00
|
|
|
defined(PSA_WANT_ALG_FFDH)
|
2023-06-05 12:32:55 +02:00
|
|
|
uint16_t ffdh_group = 0;
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_LIGHT)
|
|
|
|
const mbedtls_ecp_curve_info *curve_cur = NULL;
|
|
|
|
#endif
|
2023-06-05 12:32:55 +02:00
|
|
|
/* Terminate the current string */
|
|
|
|
while (*p != ',' && *p != '\0') {
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (*p == ',') {
|
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(MBEDTLS_ECP_LIGHT)
|
2023-06-05 12:32:55 +02:00
|
|
|
if ((curve_cur = mbedtls_ecp_curve_info_from_name(q)) != NULL) {
|
|
|
|
group_list[i++] = curve_cur->tls_id;
|
2023-06-12 11:21:18 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
|
2023-06-13 11:49:11 +02:00
|
|
|
defined(PSA_WANT_ALG_FFDH)
|
2023-06-12 11:21:18 +02:00
|
|
|
if ((ffdh_group = mbedtls_ssl_ffdh_group_from_name(q)) != 0) {
|
2023-06-05 12:32:55 +02:00
|
|
|
group_list[i++] = ffdh_group;
|
2023-06-12 11:21:18 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2023-06-05 12:32:55 +02:00
|
|
|
mbedtls_printf("unknown curve %s\n", q);
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(MBEDTLS_ECP_LIGHT)
|
2023-06-05 12:32:55 +02:00
|
|
|
mbedtls_printf("supported curves: ");
|
|
|
|
for (curve_cur = mbedtls_ecp_curve_list();
|
|
|
|
curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
|
|
|
|
curve_cur++) {
|
|
|
|
mbedtls_printf("%s ", curve_cur->name);
|
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \
|
2023-06-13 11:49:11 +02:00
|
|
|
defined(PSA_WANT_ALG_FFDH)
|
2023-06-05 12:32:55 +02:00
|
|
|
const uint16_t *supported_ffdh_group = mbedtls_ssl_ffdh_supported_groups();
|
|
|
|
while (*supported_ffdh_group != 0) {
|
|
|
|
mbedtls_printf("%s ",
|
|
|
|
mbedtls_ssl_ffdh_name_from_group(*supported_ffdh_group));
|
|
|
|
supported_ffdh_group++;
|
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif
|
2023-06-05 12:32:55 +02:00
|
|
|
mbedtls_printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_printf("Number of curves: %u\n", (unsigned int) i);
|
|
|
|
|
|
|
|
if (i == group_list_len - 1 && *p != '\0') {
|
|
|
|
mbedtls_printf("curves list too long, maximum %u",
|
|
|
|
(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) */
|