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 | |
| 23 | #include "ssl_test_lib.h" |
| 24 | |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 25 | #if defined(MBEDTLS_TEST_HOOKS) |
| 26 | #include "test/helpers.h" |
| 27 | #endif |
| 28 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 29 | #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) |
| 30 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 31 | void my_debug(void *ctx, int level, |
| 32 | const char *file, int line, |
| 33 | const char *str) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 34 | { |
| 35 | const char *p, *basename; |
| 36 | |
| 37 | /* Extract basename from file */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 38 | for (p = basename = file; *p != '\0'; p++) { |
| 39 | if (*p == '/' || *p == '\\') { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 40 | basename = p + 1; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 41 | } |
| 42 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 43 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 44 | mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s", |
| 45 | basename, line, level, str); |
| 46 | fflush((FILE *) ctx); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Raoul Strackx | 2db000f | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_HAVE_TIME) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 50 | mbedtls_time_t dummy_constant_time(mbedtls_time_t *time) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 51 | { |
| 52 | (void) time; |
| 53 | return 0x5af2a056; |
| 54 | } |
Raoul Strackx | 2db000f | 2020-06-22 14:08:57 +0200 | [diff] [blame] | 55 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 56 | |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 57 | #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 58 | static int dummy_entropy(void *data, unsigned char *output, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 59 | { |
| 60 | size_t i; |
| 61 | int ret; |
| 62 | (void) data; |
| 63 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 64 | ret = mbedtls_entropy_func(data, output, len); |
| 65 | for (i = 0; i < len; i++) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 66 | //replace result with pseudo random |
| 67 | output[i] = (unsigned char) rand(); |
| 68 | } |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 69 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 70 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 71 | #endif |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 72 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 73 | void rng_init(rng_context_t *rng) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 74 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 75 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 76 | (void) rng; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 77 | psa_crypto_init(); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 78 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 79 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 80 | #if defined(MBEDTLS_CTR_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 81 | mbedtls_ctr_drbg_init(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 82 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 83 | mbedtls_hmac_drbg_init(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 84 | #else |
| 85 | #error "No DRBG available" |
| 86 | #endif |
| 87 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 88 | mbedtls_entropy_init(&rng->entropy); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 89 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 90 | } |
| 91 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 92 | int rng_seed(rng_context_t *rng, int reproducible, const char *pers) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 93 | { |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 94 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 95 | if (reproducible) { |
| 96 | mbedtls_fprintf(stderr, |
| 97 | "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n"); |
| 98 | return -1; |
Gilles Peskine | aaedbdc | 2021-02-03 13:55:22 +0100 | [diff] [blame] | 99 | } |
| 100 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 101 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 102 | /* The PSA crypto RNG does its own seeding. */ |
| 103 | (void) rng; |
| 104 | (void) pers; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 105 | if (reproducible) { |
| 106 | mbedtls_fprintf(stderr, |
| 107 | "The PSA RNG does not support reproducible mode.\n"); |
| 108 | return -1; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 109 | } |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 110 | return 0; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 111 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 112 | int (*f_entropy)(void *, unsigned char *, size_t) = |
| 113 | (reproducible ? dummy_entropy : mbedtls_entropy_func); |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 114 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 115 | if (reproducible) { |
| 116 | srand(1); |
| 117 | } |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 118 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 119 | #if defined(MBEDTLS_CTR_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 120 | int ret = mbedtls_ctr_drbg_seed(&rng->drbg, |
| 121 | f_entropy, &rng->entropy, |
| 122 | (const unsigned char *) pers, |
| 123 | strlen(pers)); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 124 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 125 | #if defined(MBEDTLS_SHA256_C) |
| 126 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256; |
| 127 | #elif defined(MBEDTLS_SHA512_C) |
| 128 | const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512; |
| 129 | #else |
| 130 | #error "No message digest available for HMAC_DRBG" |
| 131 | #endif |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 132 | int ret = mbedtls_hmac_drbg_seed(&rng->drbg, |
| 133 | mbedtls_md_info_from_type(md_type), |
| 134 | f_entropy, &rng->entropy, |
| 135 | (const unsigned char *) pers, |
| 136 | strlen(pers)); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 137 | #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 138 | #error "No DRBG available" |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 139 | #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */ |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 140 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 141 | if (ret != 0) { |
| 142 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n", |
| 143 | (unsigned int) -ret); |
| 144 | return ret; |
Gilles Peskine | f1cb75f | 2021-01-13 18:46:01 +0100 | [diff] [blame] | 145 | } |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 146 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 147 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 148 | return 0; |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 149 | } |
| 150 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 151 | void rng_free(rng_context_t *rng) |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 152 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 153 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 154 | (void) rng; |
| 155 | /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs. |
| 156 | * This is ok because none of our applications try to do any crypto after |
| 157 | * deinitializing the RNG. */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 158 | mbedtls_psa_crypto_free(); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 159 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
| 160 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 161 | #if defined(MBEDTLS_CTR_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 162 | mbedtls_ctr_drbg_free(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 163 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 164 | mbedtls_hmac_drbg_free(&rng->drbg); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 165 | #else |
| 166 | #error "No DRBG available" |
| 167 | #endif |
| 168 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 169 | mbedtls_entropy_free(&rng->entropy); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 170 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | daa94c4 | 2021-01-13 18:38:27 +0100 | [diff] [blame] | 171 | } |
| 172 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 173 | 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] | 174 | { |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 175 | #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) |
| 176 | (void) p_rng; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 177 | return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, |
| 178 | output, output_len); |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 179 | #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 180 | rng_context_t *rng = p_rng; |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 181 | |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 182 | #if defined(MBEDTLS_CTR_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 183 | return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 184 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 185 | return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len); |
Gilles Peskine | ba74904 | 2021-01-13 20:02:03 +0100 | [diff] [blame] | 186 | #else |
| 187 | #error "No DRBG available" |
| 188 | #endif |
Gilles Peskine | 8eb2943 | 2021-02-03 20:07:11 +0100 | [diff] [blame] | 189 | |
| 190 | #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */ |
Gilles Peskine | 535fb37 | 2021-01-13 18:59:46 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 193 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 194 | int ca_callback(void *data, mbedtls_x509_crt const *child, |
| 195 | mbedtls_x509_crt **candidates) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 196 | { |
| 197 | int ret = 0; |
| 198 | mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data; |
| 199 | mbedtls_x509_crt *first; |
| 200 | |
| 201 | /* This is a test-only implementation of the CA callback |
| 202 | * which always returns the entire list of trusted certificates. |
| 203 | * Production implementations managing a large number of CAs |
| 204 | * should use an efficient presentation and lookup for the |
| 205 | * set of trusted certificates (such as a hashtable) and only |
| 206 | * return those trusted certificates which satisfy basic |
| 207 | * parental checks, such as the matching of child `Issuer` |
| 208 | * and parent `Subject` field or matching key identifiers. */ |
| 209 | ((void) child); |
| 210 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 211 | first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
| 212 | if (first == NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 213 | ret = -1; |
| 214 | goto exit; |
| 215 | } |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 216 | mbedtls_x509_crt_init(first); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 217 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 218 | 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] | 219 | ret = -1; |
| 220 | goto exit; |
| 221 | } |
| 222 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 223 | while (ca->next != NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 224 | ca = ca->next; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 225 | 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] | 226 | ret = -1; |
| 227 | goto exit; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | exit: |
| 232 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 233 | if (ret != 0) { |
| 234 | mbedtls_x509_crt_free(first); |
| 235 | mbedtls_free(first); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 236 | first = NULL; |
| 237 | } |
| 238 | |
| 239 | *candidates = first; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 240 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 241 | } |
| 242 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 243 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 244 | int delayed_recv(void *ctx, unsigned char *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 245 | { |
| 246 | static int first_try = 1; |
| 247 | int ret; |
| 248 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 249 | if (first_try) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 250 | first_try = 0; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 251 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 252 | } |
| 253 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 254 | ret = mbedtls_net_recv(ctx, buf, len); |
| 255 | if (ret != MBEDTLS_ERR_SSL_WANT_READ) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 256 | first_try = 1; /* Next call will be a new operation */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 257 | } |
| 258 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 259 | } |
| 260 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 261 | int delayed_send(void *ctx, const unsigned char *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 262 | { |
| 263 | static int first_try = 1; |
| 264 | int ret; |
| 265 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 266 | if (first_try) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 267 | first_try = 0; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 268 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 269 | } |
| 270 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 271 | ret = mbedtls_net_send(ctx, buf, len); |
| 272 | if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 273 | first_try = 1; /* Next call will be a new operation */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 274 | } |
| 275 | return ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | #if !defined(MBEDTLS_TIMING_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 279 | int idle(mbedtls_net_context *fd, |
| 280 | int idle_reason) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 281 | #else |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 282 | int idle(mbedtls_net_context *fd, |
| 283 | mbedtls_timing_delay_context *timer, |
| 284 | int idle_reason) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 285 | #endif |
| 286 | { |
| 287 | int ret; |
| 288 | int poll_type = 0; |
| 289 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 290 | if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 291 | poll_type = MBEDTLS_NET_POLL_WRITE; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 292 | } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 293 | poll_type = MBEDTLS_NET_POLL_READ; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 294 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 295 | #if !defined(MBEDTLS_TIMING_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 296 | else { |
| 297 | return 0; |
| 298 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 301 | while (1) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 302 | /* Check if timer has expired */ |
| 303 | #if defined(MBEDTLS_TIMING_C) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 304 | if (timer != NULL && |
| 305 | mbedtls_timing_get_delay(timer) == 2) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | #endif /* MBEDTLS_TIMING_C */ |
| 309 | |
| 310 | /* Check if underlying transport became available */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 311 | if (poll_type != 0) { |
| 312 | ret = mbedtls_net_poll(fd, poll_type, 0); |
| 313 | if (ret < 0) { |
| 314 | return ret; |
| 315 | } |
| 316 | if (ret == poll_type) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 317 | break; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 318 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 322 | return 0; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 325 | #if defined(MBEDTLS_TEST_HOOKS) |
| 326 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 327 | void test_hooks_init(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 328 | { |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 329 | mbedtls_test_info_reset(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 330 | |
| 331 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 332 | mbedtls_test_mutex_usage_init(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 333 | #endif |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 334 | } |
| 335 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 336 | int test_hooks_failure_detected(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 337 | { |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 338 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 339 | /* Errors are reported via mbedtls_test_info. */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 340 | mbedtls_test_mutex_usage_check(); |
Gilles Peskine | e374b95 | 2021-02-03 00:05:19 +0100 | [diff] [blame] | 341 | #endif |
| 342 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 343 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) { |
| 344 | return 1; |
| 345 | } |
| 346 | return 0; |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 347 | } |
| 348 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 349 | void test_hooks_free(void) |
Gilles Peskine | 53dea74 | 2021-02-02 22:55:06 +0100 | [diff] [blame] | 350 | { |
| 351 | } |
| 352 | |
| 353 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 354 | |
Gilles Peskine | ab7ce96 | 2021-01-05 21:27:53 +0100 | [diff] [blame] | 355 | #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */ |