blob: ab41a0bf6d8b24ec8c8afe27bbdc41a47bc37824 [file] [log] [blame]
Gilles Peskine0d980b82021-01-05 23:34:27 +01001/*
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 Rodgman16799db2023-11-02 19:47:20 +000012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine0d980b82021-01-05 23:34:27 +010013 */
Gilles Peskine504c1a32021-01-05 23:40:14 +010014
Michael Schuster6fa32fd2024-06-01 21:15:02 +020015static void eap_tls_key_derivation(void *p_expkey,
Michael Schuster82984bc2024-06-12 00:05:25 +020016 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 Peskine504c1a32021-01-05 23:40:14 +010022{
Gilles Peskine449bd832023-01-11 14:50:10 +010023 eap_tls_keys *keys = (eap_tls_keys *) p_expkey;
Gilles Peskine504c1a32021-01-05 23:40:14 +010024
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010025 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +010026 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +010027 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010028 }
29 if (secret_len != sizeof(keys->master_secret)) {
Hanno Becker296fefe2021-06-21 09:32:27 +010030 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010031 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 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 Peskine504c1a32021-01-05 23:40:14 +010036 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +010037}
38
Michael Schuster6fa32fd2024-06-01 21:15:02 +020039static void nss_keylog_export(void *p_expkey,
Michael Schuster82984bc2024-06-12 00:05:25 +020040 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 Peskine504c1a32021-01-05 23:40:14 +010046{
Gilles Peskine449bd832023-01-11 14:50:10 +010047 char nss_keylog_line[200];
Gilles Peskine504c1a32021-01-05 23:40:14 +010048 size_t const client_random_len = 32;
Gilles Peskine504c1a32021-01-05 23:40:14 +010049 size_t len = 0;
50 size_t j;
Gilles Peskine504c1a32021-01-05 23:40:14 +010051
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010052 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +010054 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010056
Gilles Peskine504c1a32021-01-05 23:40:14 +010057 ((void) p_expkey);
Gilles Peskine504c1a32021-01-05 23:40:14 +010058 ((void) server_random);
59 ((void) tls_prf_type);
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 len += sprintf(nss_keylog_line + len,
62 "%s", "CLIENT_RANDOM ");
Gilles Peskine504c1a32021-01-05 23:40:14 +010063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 for (j = 0; j < client_random_len; j++) {
65 len += sprintf(nss_keylog_line + len,
66 "%02x", client_random[j]);
Gilles Peskine504c1a32021-01-05 23:40:14 +010067 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 len += sprintf(nss_keylog_line + len, " ");
Gilles Peskine504c1a32021-01-05 23:40:14 +010070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 for (j = 0; j < secret_len; j++) {
72 len += sprintf(nss_keylog_line + len,
73 "%02x", secret[j]);
Gilles Peskine504c1a32021-01-05 23:40:14 +010074 }
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 len += sprintf(nss_keylog_line + len, "\n");
77 nss_keylog_line[len] = '\0';
Gilles Peskine504c1a32021-01-05 23:40:14 +010078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_printf("\n");
80 mbedtls_printf("---------------- NSS KEYLOG -----------------\n");
81 mbedtls_printf("%s", nss_keylog_line);
82 mbedtls_printf("---------------------------------------------\n");
Gilles Peskine504c1a32021-01-05 23:40:14 +010083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (opt.nss_keylog_file != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010085 FILE *f;
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((f = fopen(opt.nss_keylog_file, "a")) == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010088 goto exit;
89 }
90
Gilles Peskine6d576c92022-06-30 17:06:11 +020091 /* Ensure no stdio buffering of secrets, as such buffers cannot be
92 * wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_setbuf(f, NULL);
Gilles Peskine6d576c92022-06-30 17:06:11 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (fwrite(nss_keylog_line, 1, len, f) != len) {
96 fclose(f);
Gilles Peskine504c1a32021-01-05 23:40:14 +010097 goto exit;
98 }
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 fclose(f);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100101 }
102
103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_platform_zeroize(nss_keylog_line,
105 sizeof(nss_keylog_line));
Gilles Peskine504c1a32021-01-05 23:40:14 +0100106}
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108#if defined(MBEDTLS_SSL_DTLS_SRTP)
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200109static void dtls_srtp_key_derivation(void *p_expkey,
Michael Schuster82984bc2024-06-12 00:05:25 +0200110 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 Peskine504c1a32021-01-05 23:40:14 +0100116{
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 dtls_srtp_keys *keys = (dtls_srtp_keys *) p_expkey;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100118
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100119 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +0100121 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 }
123 if (secret_len != sizeof(keys->master_secret)) {
Hanno Becker296fefe2021-06-21 09:32:27 +0100124 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 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 Peskine504c1a32021-01-05 23:40:14 +0100130 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100131}
132#endif /* MBEDTLS_SSL_DTLS_SRTP */
133
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200134static int ssl_check_record(mbedtls_ssl_context const *ssl,
Michael Schuster82984bc2024-06-12 00:05:25 +0200135 unsigned char const *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100136{
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200137 int my_ret = 0, ret_cr1, ret_cr2;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100138 unsigned char *tmp_buf;
139
140 /* Record checking may modify the input buffer,
141 * so make a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 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 Peskine504c1a32021-01-05 23:40:14 +0100147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 ret_cr1 = mbedtls_ssl_check_record(ssl, tmp_buf, len);
149 if (ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100150 /* Test-only: Make sure that mbedtls_ssl_check_record()
151 * doesn't alter state. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 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é-Gonnarde5306f62021-07-07 10:48:26 +0200156 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200157 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 switch (ret_cr1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100161 case 0:
162 break;
163
164 case MBEDTLS_ERR_SSL_INVALID_RECORD:
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (opt.debug_level > 1) {
166 mbedtls_printf("mbedtls_ssl_check_record() detected invalid record.\n");
167 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100168 break;
169
170 case MBEDTLS_ERR_SSL_INVALID_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (opt.debug_level > 1) {
172 mbedtls_printf("mbedtls_ssl_check_record() detected unauthentic record.\n");
173 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100174 break;
175
176 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (opt.debug_level > 1) {
178 mbedtls_printf("mbedtls_ssl_check_record() detected unexpected record.\n");
179 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100180 break;
181
182 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_printf("mbedtls_ssl_check_record() failed fatally with -%#04x.\n",
184 (unsigned int) -ret_cr1);
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200185 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200186 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100187 }
188
189 /* Regardless of the outcome, forward the record to the stack. */
190 }
191
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200192cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_free(tmp_buf);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return my_ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100196}
Gilles Peskine504c1a32021-01-05 23:40:14 +0100197
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200198static int recv_cb(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100199{
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100201 size_t recv_len;
202 int ret;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 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 Peskine504c1a32021-01-05 23:40:14 +0100212 recv_len = (size_t) ret;
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100215 /* 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 Peskine449bd832023-01-11 14:50:10 +0100218 if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
219 return -1;
220 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100221 }
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return (int) recv_len;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100224}
225
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200226static int recv_timeout_cb(void *ctx, unsigned char *buf, size_t len,
Michael Schuster82984bc2024-06-12 00:05:25 +0200227 uint32_t timeout)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100228{
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100230 int ret;
231 size_t recv_len;
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 ret = mbedtls_net_recv_timeout(io_ctx->net, buf, len, timeout);
234 if (ret < 0) {
235 return ret;
236 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100237 recv_len = (size_t) ret;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100240 /* 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 Peskine449bd832023-01-11 14:50:10 +0100243 if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
244 return -1;
245 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100246 }
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return (int) recv_len;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100249}
250
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200251static int send_cb(void *ctx, unsigned char const *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100252{
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (opt.nbio == 2) {
256 return delayed_send(io_ctx->net, buf, len);
257 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return mbedtls_net_send(io_ctx->net, buf, len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100260}
261
262#if defined(MBEDTLS_X509_CRT_PARSE_C)
Valerio Setti5ba1d5e2023-02-22 12:38:54 +0100263#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && defined(MBEDTLS_RSA_C)
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800264#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu9bb3ee42022-06-23 10:16:33 +0800265/*
Jerry Yua1255e62022-06-24 10:10:47 +0800266 * 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 Yucc539102022-06-27 16:27:35 +0800269 * 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 Yua1255e62022-06-24 10:10:47 +0800271 * support them, the handshake fails.
Jerry Yu3896ac62022-06-19 17:16:38 +0800272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
274 ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
275 (0x800 | hash),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800276#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100277#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
278 ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800279#endif
Valerio Setti5ba1d5e2023-02-22 12:38:54 +0100280#elif defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA),
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800282#elif defined(MBEDTLS_RSA_C)
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800283#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3896ac62022-06-19 17:16:38 +0800284/* See above */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
286 (0x800 | hash),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800287#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100288#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800289#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800290#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100291#define MBEDTLS_SSL_SIG_ALG(hash)
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800292#endif
Andrzej Kurek0bc834b2022-09-06 17:30:43 -0400293
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800294uint16_t ssl_sig_algs_for_test[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100295#if defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA512)
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200297#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100298#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA384)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100300#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100301#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA256)
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200303#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100304#if defined(MBEDTLS_MD_CAN_SHA224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA224)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100306#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100307#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron903c9792022-06-16 16:55:31 +0200308 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100309#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
310#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100311 /* Allow SHA-1 as we use it extensively in tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA1)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100313#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800314 MBEDTLS_TLS1_3_SIG_NONE
Gilles Peskine504c1a32021-01-05 23:40:14 +0100315};
316#endif /* MBEDTLS_X509_CRT_PARSE_C */
Chris Jonese383fa62021-04-27 14:50:43 +0100317
318#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100319/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
320 * for more info.
321 */
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200322static int x509_crt_verify_info(char *buf, size_t size, const char *prefix,
Michael Schuster82984bc2024-06-12 00:05:25 +0200323 uint32_t flags)
Chris Jonese383fa62021-04-27 14:50:43 +0100324{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100325#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return mbedtls_x509_crt_verify_info(buf, size, prefix, flags);
Chris Jonese383fa62021-04-27 14:50:43 +0100327
328#else /* !MBEDTLS_X509_REMOVE_INFO */
329 int ret;
330 char *p = buf;
331 size_t n = size;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333#define X509_CRT_ERROR_INFO(err, err_str, info) \
334 if ((flags & err) != 0) \
Chris Jonese383fa62021-04-27 14:50:43 +0100335 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, info); \
Chris Jonese383fa62021-04-27 14:50:43 +0100337 MBEDTLS_X509_SAFE_SNPRINTF; \
338 flags ^= err; \
339 }
340
341 MBEDTLS_X509_CRT_ERROR_INFO_LIST
342#undef X509_CRT_ERROR_INFO
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (flags != 0) {
345 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
346 "(this should not happen)\n", prefix);
Chris Jonese383fa62021-04-27 14:50:43 +0100347 MBEDTLS_X509_SAFE_SNPRINTF;
348 }
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return (int) (size - n);
Chris Jonese383fa62021-04-27 14:50:43 +0100351#endif /* MBEDTLS_X509_REMOVE_INFO */
352}
353#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu202919c2022-06-27 16:21:00 +0800354
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200355static void mbedtls_print_supported_sig_algs(void)
Jerry Yu202919c2022-06-27 16:21:00 +0800356{
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_printf("supported signature algorithms:\n");
Jerry Yu202919c2022-06-27 16:21:00 +0800358 mbedtls_printf("\trsa_pkcs1_sha256 ");
359 mbedtls_printf("rsa_pkcs1_sha384 ");
360 mbedtls_printf("rsa_pkcs1_sha512\n");
361 mbedtls_printf("\tecdsa_secp256r1_sha256 ");
362 mbedtls_printf("ecdsa_secp384r1_sha384 ");
363 mbedtls_printf("ecdsa_secp521r1_sha512\n");
364 mbedtls_printf("\trsa_pss_rsae_sha256 ");
365 mbedtls_printf("rsa_pss_rsae_sha384 ");
366 mbedtls_printf("rsa_pss_rsae_sha512\n");
367 mbedtls_printf("\trsa_pss_pss_sha256 ");
368 mbedtls_printf("rsa_pss_pss_sha384 ");
369 mbedtls_printf("rsa_pss_pss_sha512\n");
370 mbedtls_printf("\ted25519 ");
371 mbedtls_printf("ed448 ");
372 mbedtls_printf("rsa_pkcs1_sha1 ");
373 mbedtls_printf("ecdsa_sha1\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_printf("\n");
Jerry Yucc539102022-06-27 16:27:35 +0800375}