Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Common source code for SSL test programs. This file is included by |
| 3 | * both ssl_client2.c and ssl_server2.c and is intended for source |
| 4 | * code that is textually identical in both programs, but that cannot be |
| 5 | * compiled separately because it refers to types or macros that are |
| 6 | * different in the two programs, or because it would have an incomplete |
| 7 | * type. |
| 8 | * |
| 9 | * This file is meant to be #include'd and cannot be compiled separately. |
| 10 | * |
| 11 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 12 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | 0d980b8 | 2021-01-05 23:34:27 +0100 | [diff] [blame] | 13 | */ |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 14 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 15 | static void eap_tls_key_derivation(void *p_expkey, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 16 | mbedtls_ssl_key_export_type secret_type, |
| 17 | const unsigned char *secret, |
| 18 | size_t secret_len, |
| 19 | const unsigned char client_random[32], |
| 20 | const unsigned char server_random[32], |
| 21 | mbedtls_tls_prf_types tls_prf_type) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 22 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 23 | eap_tls_keys *keys = (eap_tls_keys *) p_expkey; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 24 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 25 | /* We're only interested in the TLS 1.2 master secret */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 26 | if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) { |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 27 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 28 | } |
| 29 | if (secret_len != sizeof(keys->master_secret)) { |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 30 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | } |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 32 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | memcpy(keys->master_secret, secret, sizeof(keys->master_secret)); |
| 34 | memcpy(keys->randbytes, client_random, 32); |
| 35 | memcpy(keys->randbytes + 32, server_random, 32); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 36 | keys->tls_prf_type = tls_prf_type; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 39 | static void nss_keylog_export(void *p_expkey, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 40 | mbedtls_ssl_key_export_type secret_type, |
| 41 | const unsigned char *secret, |
| 42 | size_t secret_len, |
| 43 | const unsigned char client_random[32], |
| 44 | const unsigned char server_random[32], |
| 45 | mbedtls_tls_prf_types tls_prf_type) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 46 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | char nss_keylog_line[200]; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 48 | size_t const client_random_len = 32; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 49 | size_t len = 0; |
| 50 | size_t j; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 51 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 52 | /* We're only interested in the TLS 1.2 master secret */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) { |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 54 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | } |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 56 | |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 57 | ((void) p_expkey); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 58 | ((void) server_random); |
| 59 | ((void) tls_prf_type); |
| 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | len += sprintf(nss_keylog_line + len, |
| 62 | "%s", "CLIENT_RANDOM "); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 63 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | for (j = 0; j < client_random_len; j++) { |
| 65 | len += sprintf(nss_keylog_line + len, |
| 66 | "%02x", client_random[j]); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | len += sprintf(nss_keylog_line + len, " "); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 70 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | for (j = 0; j < secret_len; j++) { |
| 72 | len += sprintf(nss_keylog_line + len, |
| 73 | "%02x", secret[j]); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | len += sprintf(nss_keylog_line + len, "\n"); |
| 77 | nss_keylog_line[len] = '\0'; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 78 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | mbedtls_printf("\n"); |
| 80 | mbedtls_printf("---------------- NSS KEYLOG -----------------\n"); |
| 81 | mbedtls_printf("%s", nss_keylog_line); |
| 82 | mbedtls_printf("---------------------------------------------\n"); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 83 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | if (opt.nss_keylog_file != NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 85 | FILE *f; |
| 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | if ((f = fopen(opt.nss_keylog_file, "a")) == NULL) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 88 | goto exit; |
| 89 | } |
| 90 | |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 91 | /* Ensure no stdio buffering of secrets, as such buffers cannot be |
| 92 | * wiped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | mbedtls_setbuf(f, NULL); |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 94 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | if (fwrite(nss_keylog_line, 1, len, f) != len) { |
| 96 | fclose(f); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 97 | goto exit; |
| 98 | } |
| 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | fclose(f); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_platform_zeroize(nss_keylog_line, |
| 105 | sizeof(nss_keylog_line)); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | #if defined(MBEDTLS_SSL_DTLS_SRTP) |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 109 | static void dtls_srtp_key_derivation(void *p_expkey, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 110 | mbedtls_ssl_key_export_type secret_type, |
| 111 | const unsigned char *secret, |
| 112 | size_t secret_len, |
| 113 | const unsigned char client_random[32], |
| 114 | const unsigned char server_random[32], |
| 115 | mbedtls_tls_prf_types tls_prf_type) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 116 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | dtls_srtp_keys *keys = (dtls_srtp_keys *) p_expkey; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 118 | |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 119 | /* We're only interested in the TLS 1.2 master secret */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) { |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 121 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | } |
| 123 | if (secret_len != sizeof(keys->master_secret)) { |
Hanno Becker | 296fefe | 2021-06-21 09:32:27 +0100 | [diff] [blame] | 124 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | } |
Hanno Becker | c4c38ca | 2021-05-24 10:57:07 +0100 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | memcpy(keys->master_secret, secret, sizeof(keys->master_secret)); |
| 128 | memcpy(keys->randbytes, client_random, 32); |
| 129 | memcpy(keys->randbytes + 32, server_random, 32); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 130 | keys->tls_prf_type = tls_prf_type; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 131 | } |
| 132 | #endif /* MBEDTLS_SSL_DTLS_SRTP */ |
| 133 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 134 | static int ssl_check_record(mbedtls_ssl_context const *ssl, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 135 | unsigned char const *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 136 | { |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 137 | int my_ret = 0, ret_cr1, ret_cr2; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 138 | unsigned char *tmp_buf; |
| 139 | |
| 140 | /* Record checking may modify the input buffer, |
| 141 | * so make a copy. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | tmp_buf = mbedtls_calloc(1, len); |
| 143 | if (tmp_buf == NULL) { |
| 144 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 145 | } |
| 146 | memcpy(tmp_buf, buf, len); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | ret_cr1 = mbedtls_ssl_check_record(ssl, tmp_buf, len); |
| 149 | if (ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 150 | /* Test-only: Make sure that mbedtls_ssl_check_record() |
| 151 | * doesn't alter state. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | memcpy(tmp_buf, buf, len); /* Restore buffer */ |
| 153 | ret_cr2 = mbedtls_ssl_check_record(ssl, tmp_buf, len); |
| 154 | if (ret_cr2 != ret_cr1) { |
| 155 | mbedtls_printf("mbedtls_ssl_check_record() returned inconsistent results.\n"); |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 156 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 157 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | switch (ret_cr1) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 161 | case 0: |
| 162 | break; |
| 163 | |
| 164 | case MBEDTLS_ERR_SSL_INVALID_RECORD: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | if (opt.debug_level > 1) { |
| 166 | mbedtls_printf("mbedtls_ssl_check_record() detected invalid record.\n"); |
| 167 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | case MBEDTLS_ERR_SSL_INVALID_MAC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | if (opt.debug_level > 1) { |
| 172 | mbedtls_printf("mbedtls_ssl_check_record() detected unauthentic record.\n"); |
| 173 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | if (opt.debug_level > 1) { |
| 178 | mbedtls_printf("mbedtls_ssl_check_record() detected unexpected record.\n"); |
| 179 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 180 | break; |
| 181 | |
| 182 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | mbedtls_printf("mbedtls_ssl_check_record() failed fatally with -%#04x.\n", |
| 184 | (unsigned int) -ret_cr1); |
Manuel Pégourié-Gonnard | e5306f6 | 2021-07-07 10:48:26 +0200 | [diff] [blame] | 185 | my_ret = -1; |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 186 | goto cleanup; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Regardless of the outcome, forward the record to the stack. */ |
| 190 | } |
| 191 | |
Manuel Pégourié-Gonnard | 69c10a4 | 2021-07-06 12:05:23 +0200 | [diff] [blame] | 192 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | mbedtls_free(tmp_buf); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | return my_ret; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 196 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 197 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 198 | static int recv_cb(void *ctx, unsigned char *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 199 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | io_ctx_t *io_ctx = (io_ctx_t *) ctx; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 201 | size_t recv_len; |
| 202 | int ret; |
| 203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 204 | if (opt.nbio == 2) { |
| 205 | ret = delayed_recv(io_ctx->net, buf, len); |
| 206 | } else { |
| 207 | ret = mbedtls_net_recv(io_ctx->net, buf, len); |
| 208 | } |
| 209 | if (ret < 0) { |
| 210 | return ret; |
| 211 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 212 | recv_len = (size_t) ret; |
| 213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 215 | /* Here's the place to do any datagram/record checking |
| 216 | * in between receiving the packet from the underlying |
| 217 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) { |
| 219 | return -1; |
| 220 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | return (int) recv_len; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 226 | static int recv_timeout_cb(void *ctx, unsigned char *buf, size_t len, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 227 | uint32_t timeout) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 228 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | io_ctx_t *io_ctx = (io_ctx_t *) ctx; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 230 | int ret; |
| 231 | size_t recv_len; |
| 232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | ret = mbedtls_net_recv_timeout(io_ctx->net, buf, len, timeout); |
| 234 | if (ret < 0) { |
| 235 | return ret; |
| 236 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 237 | recv_len = (size_t) ret; |
| 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 240 | /* Here's the place to do any datagram/record checking |
| 241 | * in between receiving the packet from the underlying |
| 242 | * transport and passing it on to the TLS stack. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) { |
| 244 | return -1; |
| 245 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | return (int) recv_len; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 251 | static int send_cb(void *ctx, unsigned char const *buf, size_t len) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 252 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | io_ctx_t *io_ctx = (io_ctx_t *) ctx; |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | if (opt.nbio == 2) { |
| 256 | return delayed_send(io_ctx->net, buf, len); |
| 257 | } |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | return mbedtls_net_send(io_ctx->net, buf, len); |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Valerio Setti | 5ba1d5e | 2023-02-22 12:38:54 +0100 | [diff] [blame] | 263 | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && defined(MBEDTLS_RSA_C) |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 264 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 9bb3ee4 | 2022-06-23 10:16:33 +0800 | [diff] [blame] | 265 | /* |
Jerry Yu | a1255e6 | 2022-06-24 10:10:47 +0800 | [diff] [blame] | 266 | * When GnuTLS/Openssl server is configured in TLS 1.2 mode with a certificate |
| 267 | * declaring an RSA public key and Mbed TLS is configured in hybrid mode, if |
| 268 | * `rsa_pss_rsae_*` algorithms are before `rsa_pkcs1_*` ones in this list then |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 269 | * the GnuTLS/Openssl server chooses an `rsa_pss_rsae_*` signature algorithm |
| 270 | * for its signature in the key exchange message. As Mbed TLS 1.2 does not |
Jerry Yu | a1255e6 | 2022-06-24 10:10:47 +0800 | [diff] [blame] | 271 | * support them, the handshake fails. |
Jerry Yu | 3896ac6 | 2022-06-19 17:16:38 +0800 | [diff] [blame] | 272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \ |
| 274 | ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \ |
| 275 | (0x800 | hash), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 276 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \ |
| 278 | ((hash << 8) | MBEDTLS_SSL_SIG_RSA), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 279 | #endif |
Valerio Setti | 5ba1d5e | 2023-02-22 12:38:54 +0100 | [diff] [blame] | 280 | #elif defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 282 | #elif defined(MBEDTLS_RSA_C) |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 3896ac6 | 2022-06-19 17:16:38 +0800 | [diff] [blame] | 284 | /* See above */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \ |
| 286 | (0x800 | hash), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 287 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | #define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA), |
Jerry Yu | 9f4cc5f | 2022-06-16 11:40:44 +0800 | [diff] [blame] | 289 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 290 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | #define MBEDTLS_SSL_SIG_ALG(hash) |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 292 | #endif |
Andrzej Kurek | 0bc834b | 2022-09-06 17:30:43 -0400 | [diff] [blame] | 293 | |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 294 | uint16_t ssl_sig_algs_for_test[] = { |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 295 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA512) |
Mateusz Starzyk | 3352a53 | 2021-04-06 14:28:22 +0200 | [diff] [blame] | 297 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 298 | #if defined(MBEDTLS_MD_CAN_SHA384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA384) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 300 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 301 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA256) |
Mateusz Starzyk | e3c48b4 | 2021-04-19 16:46:28 +0200 | [diff] [blame] | 303 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 304 | #if defined(MBEDTLS_MD_CAN_SHA224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA224) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 306 | #endif |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 307 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) |
Ronald Cron | 903c979 | 2022-06-16 16:55:31 +0200 | [diff] [blame] | 308 | MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256, |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 309 | #endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */ |
| 310 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 311 | /* Allow SHA-1 as we use it extensively in tests. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA1) |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 313 | #endif |
Jerry Yu | 11f0a9c | 2022-01-12 18:43:08 +0800 | [diff] [blame] | 314 | MBEDTLS_TLS1_3_SIG_NONE |
Gilles Peskine | 504c1a3 | 2021-01-05 23:40:14 +0100 | [diff] [blame] | 315 | }; |
| 316 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 317 | |
| 318 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 319 | /** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function |
| 320 | * for more info. |
| 321 | */ |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 322 | static int x509_crt_verify_info(char *buf, size_t size, const char *prefix, |
Michael Schuster | 82984bc | 2024-06-12 00:05:25 +0200 | [diff] [blame] | 323 | uint32_t flags) |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 324 | { |
Chris Jones | fa1f904 | 2021-04-28 10:04:05 +0100 | [diff] [blame] | 325 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | return mbedtls_x509_crt_verify_info(buf, size, prefix, flags); |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 327 | |
| 328 | #else /* !MBEDTLS_X509_REMOVE_INFO */ |
| 329 | int ret; |
| 330 | char *p = buf; |
| 331 | size_t n = size; |
| 332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | #define X509_CRT_ERROR_INFO(err, err_str, info) \ |
| 334 | if ((flags & err) != 0) \ |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 335 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, info); \ |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 337 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
| 338 | flags ^= err; \ |
| 339 | } |
| 340 | |
| 341 | MBEDTLS_X509_CRT_ERROR_INFO_LIST |
| 342 | #undef X509_CRT_ERROR_INFO |
| 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | if (flags != 0) { |
| 345 | ret = mbedtls_snprintf(p, n, "%sUnknown reason " |
| 346 | "(this should not happen)\n", prefix); |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 347 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 348 | } |
| 349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | return (int) (size - n); |
Chris Jones | e383fa6 | 2021-04-27 14:50:43 +0100 | [diff] [blame] | 351 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
| 352 | } |
| 353 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Jerry Yu | 202919c | 2022-06-27 16:21:00 +0800 | [diff] [blame] | 354 | |
Michael Schuster | ff4d6ae | 2024-06-07 07:02:45 +0200 | [diff] [blame^] | 355 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Michael Schuster | 6fa32fd | 2024-06-01 21:15:02 +0200 | [diff] [blame] | 356 | static void mbedtls_print_supported_sig_algs(void) |
Jerry Yu | 202919c | 2022-06-27 16:21:00 +0800 | [diff] [blame] | 357 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | mbedtls_printf("supported signature algorithms:\n"); |
Jerry Yu | 202919c | 2022-06-27 16:21:00 +0800 | [diff] [blame] | 359 | mbedtls_printf("\trsa_pkcs1_sha256 "); |
| 360 | mbedtls_printf("rsa_pkcs1_sha384 "); |
| 361 | mbedtls_printf("rsa_pkcs1_sha512\n"); |
| 362 | mbedtls_printf("\tecdsa_secp256r1_sha256 "); |
| 363 | mbedtls_printf("ecdsa_secp384r1_sha384 "); |
| 364 | mbedtls_printf("ecdsa_secp521r1_sha512\n"); |
| 365 | mbedtls_printf("\trsa_pss_rsae_sha256 "); |
| 366 | mbedtls_printf("rsa_pss_rsae_sha384 "); |
| 367 | mbedtls_printf("rsa_pss_rsae_sha512\n"); |
| 368 | mbedtls_printf("\trsa_pss_pss_sha256 "); |
| 369 | mbedtls_printf("rsa_pss_pss_sha384 "); |
| 370 | mbedtls_printf("rsa_pss_pss_sha512\n"); |
| 371 | mbedtls_printf("\ted25519 "); |
| 372 | mbedtls_printf("ed448 "); |
| 373 | mbedtls_printf("rsa_pkcs1_sha1 "); |
| 374 | mbedtls_printf("ecdsa_sha1\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | mbedtls_printf("\n"); |
Jerry Yu | cc53910 | 2022-06-27 16:27:35 +0800 | [diff] [blame] | 376 | } |
Michael Schuster | ff4d6ae | 2024-06-07 07:02:45 +0200 | [diff] [blame^] | 377 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |