blob: 0ceffcc102c8587f8961a75c318233de1d1355ce [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
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
Gilles Peskine504c1a32021-01-05 23:40:14 +010026
Gilles Peskine449bd832023-01-11 14:50:10 +010027void eap_tls_key_derivation(void *p_expkey,
28 mbedtls_ssl_key_export_type secret_type,
29 const unsigned char *secret,
30 size_t secret_len,
31 const unsigned char client_random[32],
32 const unsigned char server_random[32],
33 mbedtls_tls_prf_types tls_prf_type)
Gilles Peskine504c1a32021-01-05 23:40:14 +010034{
Gilles Peskine449bd832023-01-11 14:50:10 +010035 eap_tls_keys *keys = (eap_tls_keys *) p_expkey;
Gilles Peskine504c1a32021-01-05 23:40:14 +010036
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010037 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +010038 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +010039 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010040 }
41 if (secret_len != sizeof(keys->master_secret)) {
Hanno Becker296fefe2021-06-21 09:32:27 +010042 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010043 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010044
Gilles Peskine449bd832023-01-11 14:50:10 +010045 memcpy(keys->master_secret, secret, sizeof(keys->master_secret));
46 memcpy(keys->randbytes, client_random, 32);
47 memcpy(keys->randbytes + 32, server_random, 32);
Gilles Peskine504c1a32021-01-05 23:40:14 +010048 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +010049}
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051void nss_keylog_export(void *p_expkey,
52 mbedtls_ssl_key_export_type secret_type,
53 const unsigned char *secret,
54 size_t secret_len,
55 const unsigned char client_random[32],
56 const unsigned char server_random[32],
57 mbedtls_tls_prf_types tls_prf_type)
Gilles Peskine504c1a32021-01-05 23:40:14 +010058{
Gilles Peskine449bd832023-01-11 14:50:10 +010059 char nss_keylog_line[200];
Gilles Peskine504c1a32021-01-05 23:40:14 +010060 size_t const client_random_len = 32;
Gilles Peskine504c1a32021-01-05 23:40:14 +010061 size_t len = 0;
62 size_t j;
Gilles Peskine504c1a32021-01-05 23:40:14 +010063
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010064 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +010066 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +010068
Gilles Peskine504c1a32021-01-05 23:40:14 +010069 ((void) p_expkey);
Gilles Peskine504c1a32021-01-05 23:40:14 +010070 ((void) server_random);
71 ((void) tls_prf_type);
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 len += sprintf(nss_keylog_line + len,
74 "%s", "CLIENT_RANDOM ");
Gilles Peskine504c1a32021-01-05 23:40:14 +010075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 for (j = 0; j < client_random_len; j++) {
77 len += sprintf(nss_keylog_line + len,
78 "%02x", client_random[j]);
Gilles Peskine504c1a32021-01-05 23:40:14 +010079 }
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 len += sprintf(nss_keylog_line + len, " ");
Gilles Peskine504c1a32021-01-05 23:40:14 +010082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 for (j = 0; j < secret_len; j++) {
84 len += sprintf(nss_keylog_line + len,
85 "%02x", secret[j]);
Gilles Peskine504c1a32021-01-05 23:40:14 +010086 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 len += sprintf(nss_keylog_line + len, "\n");
89 nss_keylog_line[len] = '\0';
Gilles Peskine504c1a32021-01-05 23:40:14 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_printf("\n");
92 mbedtls_printf("---------------- NSS KEYLOG -----------------\n");
93 mbedtls_printf("%s", nss_keylog_line);
94 mbedtls_printf("---------------------------------------------\n");
Gilles Peskine504c1a32021-01-05 23:40:14 +010095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (opt.nss_keylog_file != NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +010097 FILE *f;
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if ((f = fopen(opt.nss_keylog_file, "a")) == NULL) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100100 goto exit;
101 }
102
Gilles Peskine6d576c92022-06-30 17:06:11 +0200103 /* Ensure no stdio buffering of secrets, as such buffers cannot be
104 * wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_setbuf(f, NULL);
Gilles Peskine6d576c92022-06-30 17:06:11 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (fwrite(nss_keylog_line, 1, len, f) != len) {
108 fclose(f);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100109 goto exit;
110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 fclose(f);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100113 }
114
115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_platform_zeroize(nss_keylog_line,
117 sizeof(nss_keylog_line));
Gilles Peskine504c1a32021-01-05 23:40:14 +0100118}
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120#if defined(MBEDTLS_SSL_DTLS_SRTP)
121void dtls_srtp_key_derivation(void *p_expkey,
122 mbedtls_ssl_key_export_type secret_type,
123 const unsigned char *secret,
124 size_t secret_len,
125 const unsigned char client_random[32],
126 const unsigned char server_random[32],
127 mbedtls_tls_prf_types tls_prf_type)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100128{
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 dtls_srtp_keys *keys = (dtls_srtp_keys *) p_expkey;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100130
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100131 /* We're only interested in the TLS 1.2 master secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (secret_type != MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET) {
Hanno Becker296fefe2021-06-21 09:32:27 +0100133 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 }
135 if (secret_len != sizeof(keys->master_secret)) {
Hanno Becker296fefe2021-06-21 09:32:27 +0100136 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
Hanno Beckerc4c38ca2021-05-24 10:57:07 +0100138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 memcpy(keys->master_secret, secret, sizeof(keys->master_secret));
140 memcpy(keys->randbytes, client_random, 32);
141 memcpy(keys->randbytes + 32, server_random, 32);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100142 keys->tls_prf_type = tls_prf_type;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100143}
144#endif /* MBEDTLS_SSL_DTLS_SRTP */
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146int ssl_check_record(mbedtls_ssl_context const *ssl,
147 unsigned char const *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100148{
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200149 int my_ret = 0, ret_cr1, ret_cr2;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100150 unsigned char *tmp_buf;
151
152 /* Record checking may modify the input buffer,
153 * so make a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 tmp_buf = mbedtls_calloc(1, len);
155 if (tmp_buf == NULL) {
156 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
157 }
158 memcpy(tmp_buf, buf, len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 ret_cr1 = mbedtls_ssl_check_record(ssl, tmp_buf, len);
161 if (ret_cr1 != MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100162 /* Test-only: Make sure that mbedtls_ssl_check_record()
163 * doesn't alter state. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 memcpy(tmp_buf, buf, len); /* Restore buffer */
165 ret_cr2 = mbedtls_ssl_check_record(ssl, tmp_buf, len);
166 if (ret_cr2 != ret_cr1) {
167 mbedtls_printf("mbedtls_ssl_check_record() returned inconsistent results.\n");
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200168 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200169 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 switch (ret_cr1) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100173 case 0:
174 break;
175
176 case MBEDTLS_ERR_SSL_INVALID_RECORD:
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (opt.debug_level > 1) {
178 mbedtls_printf("mbedtls_ssl_check_record() detected invalid record.\n");
179 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100180 break;
181
182 case MBEDTLS_ERR_SSL_INVALID_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (opt.debug_level > 1) {
184 mbedtls_printf("mbedtls_ssl_check_record() detected unauthentic record.\n");
185 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100186 break;
187
188 case MBEDTLS_ERR_SSL_UNEXPECTED_RECORD:
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (opt.debug_level > 1) {
190 mbedtls_printf("mbedtls_ssl_check_record() detected unexpected record.\n");
191 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100192 break;
193
194 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_printf("mbedtls_ssl_check_record() failed fatally with -%#04x.\n",
196 (unsigned int) -ret_cr1);
Manuel Pégourié-Gonnarde5306f62021-07-07 10:48:26 +0200197 my_ret = -1;
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200198 goto cleanup;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100199 }
200
201 /* Regardless of the outcome, forward the record to the stack. */
202 }
203
Manuel Pégourié-Gonnard69c10a42021-07-06 12:05:23 +0200204cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_free(tmp_buf);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return my_ret;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100208}
Gilles Peskine504c1a32021-01-05 23:40:14 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210int recv_cb(void *ctx, unsigned char *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100213 size_t recv_len;
214 int ret;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (opt.nbio == 2) {
217 ret = delayed_recv(io_ctx->net, buf, len);
218 } else {
219 ret = mbedtls_net_recv(io_ctx->net, buf, len);
220 }
221 if (ret < 0) {
222 return ret;
223 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100224 recv_len = (size_t) ret;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100227 /* Here's the place to do any datagram/record checking
228 * in between receiving the packet from the underlying
229 * transport and passing it on to the TLS stack. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
231 return -1;
232 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100233 }
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return (int) recv_len;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100236}
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int recv_timeout_cb(void *ctx, unsigned char *buf, size_t len,
239 uint32_t timeout)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100240{
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100242 int ret;
243 size_t recv_len;
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 ret = mbedtls_net_recv_timeout(io_ctx->net, buf, len, timeout);
246 if (ret < 0) {
247 return ret;
248 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100249 recv_len = (size_t) ret;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine504c1a32021-01-05 23:40:14 +0100252 /* Here's the place to do any datagram/record checking
253 * in between receiving the packet from the underlying
254 * transport and passing it on to the TLS stack. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (ssl_check_record(io_ctx->ssl, buf, recv_len) != 0) {
256 return -1;
257 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100258 }
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return (int) recv_len;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100261}
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263int send_cb(void *ctx, unsigned char const *buf, size_t len)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100264{
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 io_ctx_t *io_ctx = (io_ctx_t *) ctx;
Gilles Peskine504c1a32021-01-05 23:40:14 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (opt.nbio == 2) {
268 return delayed_send(io_ctx->net, buf, len);
269 }
Gilles Peskine504c1a32021-01-05 23:40:14 +0100270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return mbedtls_net_send(io_ctx->net, buf, len);
Gilles Peskine504c1a32021-01-05 23:40:14 +0100272}
273
274#if defined(MBEDTLS_X509_CRT_PARSE_C)
Valerio Setti5ba1d5e2023-02-22 12:38:54 +0100275#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && defined(MBEDTLS_RSA_C)
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800276#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu9bb3ee42022-06-23 10:16:33 +0800277/*
Jerry Yua1255e62022-06-24 10:10:47 +0800278 * When GnuTLS/Openssl server is configured in TLS 1.2 mode with a certificate
279 * declaring an RSA public key and Mbed TLS is configured in hybrid mode, if
280 * `rsa_pss_rsae_*` algorithms are before `rsa_pkcs1_*` ones in this list then
Jerry Yucc539102022-06-27 16:27:35 +0800281 * the GnuTLS/Openssl server chooses an `rsa_pss_rsae_*` signature algorithm
282 * for its signature in the key exchange message. As Mbed TLS 1.2 does not
Jerry Yua1255e62022-06-24 10:10:47 +0800283 * support them, the handshake fails.
Jerry Yu3896ac62022-06-19 17:16:38 +0800284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
286 ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
287 (0x800 | hash),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800288#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100289#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA), \
290 ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800291#endif
Valerio Setti5ba1d5e2023-02-22 12:38:54 +0100292#elif defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA),
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800294#elif defined(MBEDTLS_RSA_C)
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800295#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3896ac62022-06-19 17:16:38 +0800296/* See above */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA), \
298 (0x800 | hash),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800299#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100300#define MBEDTLS_SSL_SIG_ALG(hash) ((hash << 8) | MBEDTLS_SSL_SIG_RSA),
Jerry Yu9f4cc5f2022-06-16 11:40:44 +0800301#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800302#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100303#define MBEDTLS_SSL_SIG_ALG(hash)
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800304#endif
Andrzej Kurek0bc834b2022-09-06 17:30:43 -0400305
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800306uint16_t ssl_sig_algs_for_test[] = {
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200307#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA512)
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200309#endif
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200310#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA384)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100312#endif
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200313#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA256)
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200315#endif
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200316#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA224)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100318#endif
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200319#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron903c9792022-06-16 16:55:31 +0200320 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Ronald Cronba65fbb2022-06-22 14:35:05 +0200321#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
Manuel Pégourié-Gonnarde8967052022-09-15 11:41:16 +0200322#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100323 /* Allow SHA-1 as we use it extensively in tests. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_SSL_SIG_ALG(MBEDTLS_SSL_HASH_SHA1)
Gilles Peskine504c1a32021-01-05 23:40:14 +0100325#endif
Jerry Yu11f0a9c2022-01-12 18:43:08 +0800326 MBEDTLS_TLS1_3_SIG_NONE
Gilles Peskine504c1a32021-01-05 23:40:14 +0100327};
328#endif /* MBEDTLS_X509_CRT_PARSE_C */
Chris Jonese383fa62021-04-27 14:50:43 +0100329
330#if defined(MBEDTLS_X509_CRT_PARSE_C)
Chris Jonese383fa62021-04-27 14:50:43 +0100331/** Functionally equivalent to mbedtls_x509_crt_verify_info, see that function
332 * for more info.
333 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334int x509_crt_verify_info(char *buf, size_t size, const char *prefix,
335 uint32_t flags)
Chris Jonese383fa62021-04-27 14:50:43 +0100336{
Chris Jonesfa1f9042021-04-28 10:04:05 +0100337#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return mbedtls_x509_crt_verify_info(buf, size, prefix, flags);
Chris Jonese383fa62021-04-27 14:50:43 +0100339
340#else /* !MBEDTLS_X509_REMOVE_INFO */
341 int ret;
342 char *p = buf;
343 size_t n = size;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345#define X509_CRT_ERROR_INFO(err, err_str, info) \
346 if ((flags & err) != 0) \
Chris Jonese383fa62021-04-27 14:50:43 +0100347 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, info); \
Chris Jonese383fa62021-04-27 14:50:43 +0100349 MBEDTLS_X509_SAFE_SNPRINTF; \
350 flags ^= err; \
351 }
352
353 MBEDTLS_X509_CRT_ERROR_INFO_LIST
354#undef X509_CRT_ERROR_INFO
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (flags != 0) {
357 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
358 "(this should not happen)\n", prefix);
Chris Jonese383fa62021-04-27 14:50:43 +0100359 MBEDTLS_X509_SAFE_SNPRINTF;
360 }
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return (int) (size - n);
Chris Jonese383fa62021-04-27 14:50:43 +0100363#endif /* MBEDTLS_X509_REMOVE_INFO */
364}
365#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu202919c2022-06-27 16:21:00 +0800366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367void mbedtls_print_supported_sig_algs(void)
Jerry Yu202919c2022-06-27 16:21:00 +0800368{
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_printf("supported signature algorithms:\n");
Jerry Yu202919c2022-06-27 16:21:00 +0800370 mbedtls_printf("\trsa_pkcs1_sha256 ");
371 mbedtls_printf("rsa_pkcs1_sha384 ");
372 mbedtls_printf("rsa_pkcs1_sha512\n");
373 mbedtls_printf("\tecdsa_secp256r1_sha256 ");
374 mbedtls_printf("ecdsa_secp384r1_sha384 ");
375 mbedtls_printf("ecdsa_secp521r1_sha512\n");
376 mbedtls_printf("\trsa_pss_rsae_sha256 ");
377 mbedtls_printf("rsa_pss_rsae_sha384 ");
378 mbedtls_printf("rsa_pss_rsae_sha512\n");
379 mbedtls_printf("\trsa_pss_pss_sha256 ");
380 mbedtls_printf("rsa_pss_pss_sha384 ");
381 mbedtls_printf("rsa_pss_pss_sha512\n");
382 mbedtls_printf("\ted25519 ");
383 mbedtls_printf("ed448 ");
384 mbedtls_printf("rsa_pkcs1_sha1 ");
385 mbedtls_printf("ecdsa_sha1\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_printf("\n");
Jerry Yucc539102022-06-27 16:27:35 +0800387}