Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 1 | /* |
Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 2 | * 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 Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 6 | * |
| 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 Starzyk | 6c2e9b6 | 2021-05-19 17:54:54 +0200 | [diff] [blame] | 23 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 24 | |
Gilles Peskine | a3ed34f | 2021-01-05 21:11:16 +0100 | [diff] [blame] | 25 | #include "ssl_test_lib.h" |
| 26 | |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 27 | #if defined(MBEDTLS_TEST_HOOKS) |
| 28 | #include "test/helpers.h" |
| 29 | #endif |
| 30 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 31 | #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
| 32 | |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 33 | #define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0])) |
| 34 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | void my_debug(void *ctx, int level, |
| 36 | const char *file, int line, |
| 37 | const char *str) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 38 | { |
| 39 | const char *p, *basename; |
| 40 | |
| 41 | /* Extract basename from file */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | for (p = basename = file; *p != '\0'; p++) { |
| 43 | if (*p == '/' || *p == '\\') { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 44 | basename = p + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | } |
| 46 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 47 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s", |
| 49 | basename, line, level, str); |
| 50 | fflush((FILE *) ctx); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Raoul Strackx | 9ed9bc9 | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | mbedtls_time_t dummy_constant_time(mbedtls_time_t *time) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 55 | { |
| 56 | (void) time; |
| 57 | return 0x5af2a056; |
| 58 | } |
Raoul Strackx | 9ed9bc9 | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 59 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 60 | |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 61 | #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | static int dummy_entropy(void *data, unsigned char *output, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 63 | { |
| 64 | size_t i; |
| 65 | int ret; |
| 66 | (void) data; |
| 67 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | ret = mbedtls_entropy_func(data, output, len); |
| 69 | for (i = 0; i < len; i++) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 70 | //replace result with pseudo random |
| 71 | output[i] = (unsigned char) rand(); |
| 72 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 74 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 75 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | void rng_init(rng_context_t *rng) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 78 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 79 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 80 | (void) rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | psa_crypto_init(); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 82 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 83 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 84 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | mbedtls_ctr_drbg_init(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 86 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | mbedtls_hmac_drbg_init(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 88 | #else |
| 89 | #error "No DRBG available" |
| 90 | #endif |
| 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | mbedtls_entropy_init(&rng->entropy); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 93 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | int rng_seed(rng_context_t *rng, int reproducible, const char *pers) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 97 | { |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 98 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | if (reproducible) { |
| 100 | mbedtls_fprintf(stderr, |
| 101 | "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n"); |
| 102 | return -1; |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 103 | } |
| 104 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 105 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 106 | /* The PSA crypto RNG does its own seeding. */ |
| 107 | (void) rng; |
| 108 | (void) pers; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | if (reproducible) { |
| 110 | mbedtls_fprintf(stderr, |
| 111 | "The PSA RNG does not support reproducible mode.\n"); |
| 112 | return -1; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 113 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | return 0; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 115 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | int (*f_entropy)(void *, unsigned char *, size_t) = |
| 117 | (reproducible ? dummy_entropy : mbedtls_entropy_func); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if (reproducible) { |
| 120 | srand(1); |
| 121 | } |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 122 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 123 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | int ret = mbedtls_ctr_drbg_seed(&rng->drbg, |
| 125 | f_entropy, &rng->entropy, |
| 126 | (const unsigned char *) pers, |
| 127 | strlen(pers)); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 128 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 129 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 130 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256; |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 131 | #elif defined(MBEDTLS_MD_CAN_SHA512) |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 132 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512; |
| 133 | #else |
| 134 | #error "No message digest available for HMAC_DRBG" |
| 135 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | 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 Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 141 | #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 142 | #error "No DRBG available" |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 143 | #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | if (ret != 0) { |
| 146 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", |
| 147 | (unsigned int) -ret); |
| 148 | return ret; |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 149 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 150 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | return 0; |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | void rng_free(rng_context_t *rng) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 156 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 157 | #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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | mbedtls_psa_crypto_free(); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 163 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 164 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 165 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | mbedtls_ctr_drbg_free(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 167 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | mbedtls_hmac_drbg_free(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 169 | #else |
| 170 | #error "No DRBG available" |
| 171 | #endif |
| 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | mbedtls_entropy_free(&rng->entropy); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 174 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | int rng_get(void *p_rng, unsigned char *output, size_t output_len) |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 178 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 179 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 180 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 182 | output, output_len); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 183 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 184 | rng_context_t *rng = p_rng; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 186 | #if defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 188 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 190 | #else |
| 191 | #error "No DRBG available" |
| 192 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 193 | |
| 194 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2) |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 198 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | char *separator; |
| 200 | if ((separator = strchr(arg, ',')) == NULL) { |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 201 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | } |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 203 | *separator = '\0'; |
| 204 | |
| 205 | *alg1 = arg; |
| 206 | *alg2 = separator + 1; |
| 207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | 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 Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 216 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | } |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | 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 Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 228 | return 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | } |
Przemek Stekiel | 85d692d | 2022-04-25 12:42:55 +0200 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Przemek Stekiel | 76a41f5 | 2022-05-04 13:55:23 +0200 | [diff] [blame] | 234 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | int 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 Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 240 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | if (strcmp(alg1, "none") != 0) { |
| 242 | const char *algs[] = { alg1, alg2 }; |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 243 | psa_algorithm_t *psa_algs[] = { psa_alg1, psa_alg2 }; |
Przemek Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | 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 Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 248 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | } else if (strcmp(algs[i], "rsa-sign-pss") == 0) { |
| 250 | *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH); |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 251 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | } else if (strcmp(algs[i], "rsa-sign-pss-sha256") == 0) { |
| 253 | *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_256); |
Ronald Cron | 50969e3 | 2022-09-16 15:54:33 +0200 | [diff] [blame] | 254 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | } else if (strcmp(algs[i], "rsa-sign-pss-sha384") == 0) { |
| 256 | *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_384); |
Ronald Cron | 50969e3 | 2022-09-16 15:54:33 +0200 | [diff] [blame] | 257 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | } else if (strcmp(algs[i], "rsa-sign-pss-sha512") == 0) { |
| 259 | *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_512); |
Ronald Cron | 50969e3 | 2022-09-16 15:54:33 +0200 | [diff] [blame] | 260 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | } else if (strcmp(algs[i], "rsa-decrypt") == 0) { |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 262 | *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_CRYPT; |
| 263 | *usage |= PSA_KEY_USAGE_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | } else if (strcmp(algs[i], "ecdsa-sign") == 0) { |
| 265 | *psa_algs[i] = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH); |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 266 | *usage |= PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | } else if (strcmp(algs[i], "ecdh") == 0) { |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 268 | *psa_algs[i] = PSA_ALG_ECDH; |
| 269 | *usage |= PSA_KEY_USAGE_DERIVE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | } else if (strcmp(algs[i], "none") == 0) { |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 271 | *psa_algs[i] = PSA_ALG_NONE; |
| 272 | } |
| 273 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | } else { |
| 275 | if (key_type == MBEDTLS_PK_ECKEY) { |
| 276 | *psa_alg1 = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH); |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 277 | *psa_alg2 = PSA_ALG_ECDH; |
| 278 | *usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | } 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 Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 282 | *usage = PSA_KEY_USAGE_SIGN_HASH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | } else { |
Przemek Stekiel | cb20d20 | 2022-05-06 08:42:34 +0200 | [diff] [blame] | 284 | return 1; |
| 285 | } |
Przemek Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
Przemek Stekiel | 76a41f5 | 2022-05-04 13:55:23 +0200 | [diff] [blame] | 290 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemek Stekiel | 01396a1 | 2022-05-02 13:41:53 +0200 | [diff] [blame] | 291 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 292 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | int ca_callback(void *data, mbedtls_x509_crt const *child, |
| 294 | mbedtls_x509_crt **candidates) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 295 | { |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 311 | if (first == NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 312 | ret = -1; |
| 313 | goto exit; |
| 314 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | mbedtls_x509_crt_init(first); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 318 | ret = -1; |
| 319 | goto exit; |
| 320 | } |
| 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | while (ca->next != NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 323 | ca = ca->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 325 | ret = -1; |
| 326 | goto exit; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | exit: |
| 331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | if (ret != 0) { |
| 333 | mbedtls_x509_crt_free(first); |
| 334 | mbedtls_free(first); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 335 | first = NULL; |
| 336 | } |
| 337 | |
| 338 | *candidates = first; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 340 | } |
| 341 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | int delayed_recv(void *ctx, unsigned char *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 344 | { |
| 345 | static int first_try = 1; |
| 346 | int ret; |
| 347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | if (first_try) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 349 | first_try = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | ret = mbedtls_net_recv(ctx, buf, len); |
| 354 | if (ret != MBEDTLS_ERR_SSL_WANT_READ) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 355 | first_try = 1; /* Next call will be a new operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | } |
| 357 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | int delayed_send(void *ctx, const unsigned char *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 361 | { |
| 362 | static int first_try = 1; |
| 363 | int ret; |
| 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if (first_try) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 366 | first_try = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | ret = mbedtls_net_send(ctx, buf, len); |
| 371 | if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 372 | first_try = 1; /* Next call will be a new operation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | } |
| 374 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | #if !defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | int idle(mbedtls_net_context *fd, |
| 379 | int idle_reason) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 380 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | int idle(mbedtls_net_context *fd, |
| 382 | mbedtls_timing_delay_context *timer, |
| 383 | int idle_reason) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 384 | #endif |
| 385 | { |
| 386 | int ret; |
| 387 | int poll_type = 0; |
| 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 390 | poll_type = MBEDTLS_NET_POLL_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 392 | poll_type = MBEDTLS_NET_POLL_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 394 | #if !defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | else { |
| 396 | return 0; |
| 397 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 398 | #endif |
| 399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | while (1) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 401 | /* Check if timer has expired */ |
| 402 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | if (timer != NULL && |
| 404 | mbedtls_timing_get_delay(timer) == 2) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 405 | break; |
| 406 | } |
| 407 | #endif /* MBEDTLS_TIMING_C */ |
| 408 | |
| 409 | /* Check if underlying transport became available */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | 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 Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 416 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | return 0; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 424 | #if defined(MBEDTLS_TEST_HOOKS) |
| 425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | void test_hooks_init(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 427 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | mbedtls_test_info_reset(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 429 | |
| 430 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | mbedtls_test_mutex_usage_init(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 432 | #endif |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | int test_hooks_failure_detected(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 436 | { |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 437 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 438 | /* Errors are reported via mbedtls_test_info. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | mbedtls_test_mutex_usage_check(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 440 | #endif |
| 441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) { |
| 443 | return 1; |
| 444 | } |
| 445 | return 0; |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | void test_hooks_free(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 449 | { |
| 450 | } |
| 451 | |
| 452 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 453 | |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 454 | #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \ |
| 455 | defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | 7d42c0d | 2023-06-13 11:49:11 +0200 | [diff] [blame] | 456 | |
| 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 Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 464 | static uint16_t mbedtls_ssl_ffdh_group_from_name(const char *name) |
Przemek Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 465 | { |
| 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 Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 480 | static const uint16_t *mbedtls_ssl_ffdh_supported_groups(void) |
Przemek Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 481 | { |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 482 | static const uint16_t ffdh_groups[] = { |
Przemek Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 483 | 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 Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 490 | return ffdh_groups; |
Przemek Stekiel | 7d42c0d | 2023-06-13 11:49:11 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static 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 Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 509 | return NULL; |
Przemek Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 510 | } |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 511 | #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED && PSA_WANT_ALG_FFDH */ |
Przemek Stekiel | e7db09b | 2023-05-31 11:29:55 +0200 | [diff] [blame] | 512 | |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 513 | static 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 | |
| 587 | static 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 | |
| 602 | static 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 | |
| 624 | int parse_curves(const char *curves, uint16_t *group_list, size_t group_list_len) |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 625 | { |
Przemek Stekiel | 45255e4 | 2023-06-29 13:56:36 +0200 | [diff] [blame] | 626 | char *p = (char *) groups; |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 627 | char *q = NULL; |
| 628 | size_t i = 0; |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 629 | |
| 630 | if (strcmp(p, "none") == 0) { |
| 631 | group_list[0] = 0; |
| 632 | } else if (strcmp(p, "default") != 0) { |
Przemek Stekiel | 68e7544 | 2023-07-06 11:21:39 +0200 | [diff] [blame] | 633 | /* Leave room for a final NULL in group list */ |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 634 | while (i < group_list_len - 1 && *p != '\0') { |
| 635 | q = p; |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 636 | #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \ |
Przemek Stekiel | 7d42c0d | 2023-06-13 11:49:11 +0200 | [diff] [blame] | 637 | defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 638 | uint16_t ffdh_group = 0; |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 639 | #endif |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 640 | uint16_t curve_tls_id; |
| 641 | |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 642 | /* Terminate the current string */ |
| 643 | while (*p != ',' && *p != '\0') { |
| 644 | p++; |
| 645 | } |
| 646 | if (*p == ',') { |
| 647 | *p++ = '\0'; |
| 648 | } |
| 649 | |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 650 | if ((curve_tls_id = mbedtls_ssl_get_curve_tls_id_from_name(q)) != 0) { |
| 651 | group_list[i++] = curve_tls_id; |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 652 | } else |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 653 | #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \ |
Przemek Stekiel | 7d42c0d | 2023-06-13 11:49:11 +0200 | [diff] [blame] | 654 | defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 655 | if ((ffdh_group = mbedtls_ssl_ffdh_group_from_name(q)) != 0) { |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 656 | group_list[i++] = ffdh_group; |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 657 | } else |
| 658 | #endif |
| 659 | { |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 660 | 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 Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 667 | } |
Valerio Setti | acd32c0 | 2023-06-29 18:06:29 +0200 | [diff] [blame^] | 668 | mbedtls_free((char *) supported_curves); |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 669 | #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) && \ |
Przemek Stekiel | 7d42c0d | 2023-06-13 11:49:11 +0200 | [diff] [blame] | 670 | defined(PSA_WANT_ALG_FFDH) |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 671 | 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 Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 677 | #endif |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 678 | mbedtls_printf("\n"); |
| 679 | return -1; |
| 680 | } |
| 681 | } |
| 682 | |
Przemek Stekiel | 45255e4 | 2023-06-29 13:56:36 +0200 | [diff] [blame] | 683 | mbedtls_printf("Number of groups: %u\n", (unsigned int) i); |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 684 | |
| 685 | if (i == group_list_len - 1 && *p != '\0') { |
Przemek Stekiel | 45255e4 | 2023-06-29 13:56:36 +0200 | [diff] [blame] | 686 | mbedtls_printf("groups list too long, maximum %u", |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 687 | (unsigned int) (group_list_len - 1)); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | group_list[i] = 0; |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
Przemek Stekiel | ff9fcbc | 2023-06-05 12:32:55 +0200 | [diff] [blame] | 696 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 697 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |