blob: b5bea7521a19f25a818445136e2d6f0086f176d0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * http://www.ietf.org/rfc/rfc2246.txt
9 * http://www.ietf.org/rfc/rfc4346.txt
10 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000015
SimonBd5800b72016-04-26 07:43:27 +010016#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010019#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010020#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000021#include "ssl_misc.h"
Max Fillinger44042f02024-07-22 14:43:56 +020022#include "ssl_tls13_keys.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040023
Valerio Settib4f50762024-01-17 10:24:52 +010024#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000025#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050026#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010027#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020028#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050032#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010033#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020034#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020035#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050036#include "psa/crypto.h"
37#endif
38
Janos Follath23bdca02016-10-07 14:47:14 +010039#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020041#endif
42
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040044/* Define local translating functions to save code size by not using too many
45 * arguments in each translating place. */
46static int local_err_translation(psa_status_t status)
47{
48 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040049 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040050 psa_generic_status_to_mbedtls);
51}
52#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
Sam Berry9722fd12024-06-11 14:34:17 +0100136 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000242#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
243 defined(MBEDTLS_SSL_EARLY_DATA)
244 dst->ticket_alpn = NULL;
245#endif
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000248
249#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
254 if (dst->peer_cert == NULL) {
255 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
256 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
261 src->peer_cert->raw.len)) != 0) {
262 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265 }
266 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000267#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000269 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_calloc(1, src->peer_cert_digest_len);
271 if (dst->peer_cert_digest == NULL) {
272 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
273 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
276 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000278 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 }
280#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200283
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000284#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
285 defined(MBEDTLS_SSL_EARLY_DATA)
286 {
287 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
288 if (ret != 0) {
289 return ret;
290 }
291 }
292#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
293
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200294#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (src->ticket != NULL) {
296 dst->ticket = mbedtls_calloc(1, src->ticket_len);
297 if (dst->ticket == NULL) {
298 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
299 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200302 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000303
304#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
305 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
309 if (ret != 0) {
310 return ret;
311 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000312 }
313#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
314 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200315#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200318}
319
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500320#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200321MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100322static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500323{
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
325 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800326 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500328
329 /* We want to copy len_new bytes when downsizing the buffer, and
330 * len_old bytes when upsizing, so we choose the smaller of two sizes,
331 * to fit one buffer into another. Size checks, ensuring that no data is
332 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 memcpy(resized_buffer, *buffer,
334 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100335 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500336
337 *buffer = resized_buffer;
338 *len_old = len_new;
339
340 return 0;
341}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
344 size_t in_buf_new_len,
345 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200346{
347 int modified = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000348 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
Andrzej Kurek4a063792020-10-21 15:08:44 +0200349 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200351 written_in = ssl->in_msg - ssl->in_buf;
352 iv_offset_in = ssl->in_iv - ssl->in_buf;
353 len_offset_in = ssl->in_len - ssl->in_buf;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000354 hdr_in = ssl->in_hdr - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 ssl->in_buf_len < in_buf_new_len) {
358 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
359 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
360 } else {
361 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
362 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200363 modified = 1;
364 }
365 }
366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200369 written_out = ssl->out_msg - ssl->out_buf;
370 iv_offset_out = ssl->out_iv - ssl->out_buf;
371 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200373 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 ssl->out_buf_len < out_buf_new_len) {
375 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
376 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
377 } else {
378 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
379 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200380 modified = 1;
381 }
382 }
383 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200385 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000386 ssl->in_hdr = ssl->in_buf + hdr_in;
387 mbedtls_ssl_update_in_pointers(ssl);
388 mbedtls_ssl_reset_out_pointers(ssl);
389
Andrzej Kurek4a063792020-10-21 15:08:44 +0200390 /* Fields below might not be properly updated with record
391 * splitting or with CID, so they are manually updated here. */
392 ssl->out_msg = ssl->out_buf + written_out;
393 ssl->out_len = ssl->out_buf + len_offset_out;
394 ssl->out_iv = ssl->out_buf + iv_offset_out;
395
396 ssl->in_msg = ssl->in_buf + written_in;
397 ssl->in_len = ssl->in_buf + len_offset_in;
398 ssl->in_iv = ssl->in_buf + iv_offset_in;
399 }
400}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500401#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
402
Jerry Yudb8c48a2022-01-27 14:54:54 +0800403#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800404
405#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
407 const char *label,
408 const unsigned char *random, size_t rlen,
409 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800412
413#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
414
415/* Type for the TLS PRF */
416typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
417 const unsigned char *, size_t,
418 unsigned char *, size_t);
419
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
422 int ciphersuite,
423 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200424#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200426#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 ssl_tls_prf_t tls_prf,
428 const unsigned char randbytes[64],
429 mbedtls_ssl_protocol_version tls_version,
430 unsigned endpoint,
431 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800432
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100433#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300436 const char *label,
437 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100439static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
440static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100441
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100442#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100443
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100444#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445MBEDTLS_CHECK_RETURN_CRITICAL
446static int tls_prf_sha384(const unsigned char *secret, size_t slen,
447 const char *label,
448 const unsigned char *random, size_t rlen,
449 unsigned char *dstbuf, size_t dlen);
450
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100451static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
452static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100453#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455MBEDTLS_CHECK_RETURN_CRITICAL
456static int ssl_tls12_session_load(mbedtls_ssl_session *session,
457 const unsigned char *buf,
458 size_t len);
459#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
460
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100461static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100462
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100463#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100464static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100465#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100466
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100467#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100468static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100469#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100470
471int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
472 const unsigned char *secret, size_t slen,
473 const char *label,
474 const unsigned char *random, size_t rlen,
475 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300476{
477 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300480#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100481#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA384:
483 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100485#endif /* MBEDTLS_MD_CAN_SHA384*/
486#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 case MBEDTLS_SSL_TLS_PRF_SHA256:
488 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100490#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 default:
493 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300497}
498
Jerry Yuc73c6182022-02-08 20:29:25 +0800499#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100500static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800501{
502#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (session->peer_cert != NULL) {
504 mbedtls_x509_crt_free(session->peer_cert);
505 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800506 session->peer_cert = NULL;
507 }
508#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800510 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800512 session->peer_cert_digest = NULL;
513 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
514 session->peer_cert_digest_len = 0;
515 }
516#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
517}
518#endif /* MBEDTLS_X509_CRT_PARSE_C */
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800521{
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800523 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800525
526 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800528
529 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800531
532 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800534
535 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800537
538 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800540
541 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800543
544 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800546
547 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800549
550 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800552
553 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800555
556 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800558
559 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800561
562 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800564
565 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800567
568 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800570
571 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800573
574 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800576
577 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800579
580 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800582
583 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800585
586 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800588
589 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800591
592 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800594
595 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800597
598 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800600
Jan Bruckner151f6422023-02-10 12:45:19 +0100601 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
602 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
603
Jerry Yu7a485c12022-10-31 13:08:18 +0800604 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800606
607 }
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800610}
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800613{
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800615}
616
Jerry Yud25cab02022-10-31 12:48:30 +0800617#if defined(MBEDTLS_DEBUG_C)
618static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800619 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800620 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
621 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
622 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
623 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
624 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
625 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
626 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
627 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
628 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
629 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
630 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
631 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
632 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
633 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
634 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
635 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
636 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
637 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
638 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
639 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
640 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
641 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
642 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
643 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
644 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
645 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100646 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
647 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800648};
649
Chien Wong4e9683e2023-12-28 17:07:43 +0800650static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800651 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
652 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
653 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
654 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
655 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
656 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
657 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
658 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
659 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
660 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
661 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
662 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
663 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
664 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
665 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
666 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
667 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
668 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
669 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
670 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
671 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
672 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
673 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
674 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
675 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
676 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
677 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100678 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
679 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800680};
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800683{
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return extension_name_table[
685 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800686}
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800689{
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800701 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800703 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800705 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800707}
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
710 int level, const char *file, int line,
711 int hs_msg_type, unsigned int extension_type,
712 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800713{
714 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800716 mbedtls_debug_print_msg(
717 ssl, level, file, line,
718 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800721 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800723 return;
724 }
725
726 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800728 mbedtls_debug_print_msg(
729 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
731 mbedtls_ssl_get_extension_name(extension_type), extension_type,
732 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800733 return;
734 }
735
736 mbedtls_debug_print_msg(
737 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
739 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800740}
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
743 int level, const char *file, int line,
744 int hs_msg_type, uint32_t extensions_mask,
745 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800746{
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 for (unsigned i = 0;
749 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
750 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800751 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800752 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800754 }
755}
756
Pengyu Lvee455c02023-01-12 14:37:24 +0800757#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800758static const char *ticket_flag_name_table[] =
759{
760 [0] = "ALLOW_PSK_RESUMPTION",
761 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
762 [3] = "ALLOW_EARLY_DATA",
763};
764
Pengyu Lv4938a562023-01-16 11:28:49 +0800765void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
766 int level, const char *file, int line,
767 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800768{
769 size_t i;
770
771 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800772 "print ticket_flags (0x%02x)", flags);
773
774 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800775
776 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800777 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800778 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
779 ticket_flag_name_table[i]);
780 }
781 }
782}
783#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
784
Jerry Yud25cab02022-10-31 12:48:30 +0800785#endif /* MBEDTLS_DEBUG_C */
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
788 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800789{
790 ((void) ciphersuite_info);
791
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100792#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800794 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800796#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100797#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800799 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800801#endif
802 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800804 return;
805 }
806}
807
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100808int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100809 unsigned hs_type,
810 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100811{
812 unsigned char hs_hdr[4];
813
814 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
816 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
817 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
818 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100819
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100820 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100821}
822
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100823int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100824 unsigned hs_type,
825 unsigned char const *msg,
826 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100827{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100828 int ret;
829 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100830 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100831 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100832 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100833 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100834}
835
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100836int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800837{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100838#if defined(MBEDTLS_MD_CAN_SHA256) || \
839 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100840#if defined(MBEDTLS_USE_PSA_CRYPTO)
841 psa_status_t status;
842#else
843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
844#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100845#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800846 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100847#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100848#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100850 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
851 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200852 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100853 }
854 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
855 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200856 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100857 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100859 mbedtls_md_free(&ssl->handshake->fin_sha256);
860 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100861 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
862 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
863 0);
864 if (ret != 0) {
865 return ret;
866 }
867 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100868 if (ret != 0) {
869 return ret;
870 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800871#endif
872#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100873#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100875 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
876 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200877 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100878 }
879 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
880 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200881 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100882 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800883#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100884 mbedtls_md_free(&ssl->handshake->fin_sha384);
885 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100886 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
887 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
888 if (ret != 0) {
889 return ret;
890 }
891 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100892 if (ret != 0) {
893 return ret;
894 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800895#endif
896#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100897 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800898}
899
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100900static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100901 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800902{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100903#if defined(MBEDTLS_MD_CAN_SHA256) || \
904 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100905#if defined(MBEDTLS_USE_PSA_CRYPTO)
906 psa_status_t status;
907#else
908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
909#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100910#else /* SHA-256 or SHA-384 */
911 ((void) ssl);
912 (void) buf;
913 (void) len;
914#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100915#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800916#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100917 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
918 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200919 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100920 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800921#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100922 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100923 if (ret != 0) {
924 return ret;
925 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800926#endif
927#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100928#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800929#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100930 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
931 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200932 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100933 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800934#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100935 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100936 if (ret != 0) {
937 return ret;
938 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800939#endif
940#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100941 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800942}
943
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100944#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100945static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100946 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800947{
948#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200949 return mbedtls_md_error_from_psa(psa_hash_update(
950 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800951#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100952 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800953#endif
954}
955#endif
956
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100957#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100958static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100959 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800960{
961#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200962 return mbedtls_md_error_from_psa(psa_hash_update(
963 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800964#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100965 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800966#endif
967}
968#endif
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100974#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500975#if defined(MBEDTLS_USE_PSA_CRYPTO)
976 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500977#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100978 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500980#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100981#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500982#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500983 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500984#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100985 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200986#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500987#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988
989 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200993#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200994#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200995 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200997#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200998#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200999#if defined(MBEDTLS_USE_PSA_CRYPTO)
1000 handshake->psa_pake_ctx = psa_pake_operation_init();
1001 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1002#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001004#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001005#if defined(MBEDTLS_SSL_CLI_C)
1006 handshake->ecjpake_cache = NULL;
1007 handshake->ecjpake_cache_len = 0;
1008#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001009#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001010
Gilles Peskineeccd8882020-03-10 12:19:08 +01001011#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001013#endif
1014
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001015#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1016 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1017#endif
Hanno Becker75173122019-02-06 16:18:31 +00001018
1019#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1020 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001022#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001023}
1024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001026{
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001028
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
1030 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1031 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001032#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1034 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001035#endif
1036
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001037#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001038#if defined(MBEDTLS_USE_PSA_CRYPTO)
1039 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1040 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001041#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_md_init(&transform->md_ctx_enc);
1043 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001044#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001045#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001046}
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001049{
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001051}
1052
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001054static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001055{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1057
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001058 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (ssl->transform_negotiate) {
1061 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1062 }
Jerry Yu2e199812022-12-01 18:57:19 +08001063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (ssl->session_negotiate) {
1065 mbedtls_ssl_session_free(ssl->session_negotiate);
1066 }
1067 if (ssl->handshake) {
1068 mbedtls_ssl_handshake_free(ssl);
1069 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001070
Jerry Yu2e199812022-12-01 18:57:19 +08001071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001072 /*
1073 * Either the pointers are now NULL or cleared properly and can be freed.
1074 * Now allocate missing structures.
1075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (ssl->transform_negotiate == NULL) {
1077 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 }
Jerry Yu2e199812022-12-01 18:57:19 +08001079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (ssl->session_negotiate == NULL) {
1082 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001083 }
Paul Bakker48916f92012-09-16 19:57:18 +00001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (ssl->handshake == NULL) {
1086 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001087 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001088#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1089 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1092 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001093#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001094
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001095 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001098 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001099#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 ssl->session_negotiate == NULL) {
1101 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001104 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001105
1106#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001108 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001109#endif
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001112 ssl->session_negotiate = NULL;
1113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001115 }
1116
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001117#if defined(MBEDTLS_SSL_EARLY_DATA)
1118#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001119 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001120#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001121#if defined(MBEDTLS_SSL_SRV_C)
1122 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1123#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001124 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001125#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001126
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001127 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 mbedtls_ssl_session_init(ssl->session_negotiate);
1129 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001130
Jerry Yu2e199812022-12-01 18:57:19 +08001131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001133#endif
1134
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001135 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001136 ret = mbedtls_ssl_reset_checksum(ssl);
1137 if (ret != 0) {
1138 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1139 return ret;
1140 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001141
Jerry Yud0766ec2022-09-22 10:46:57 +08001142#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1143 defined(MBEDTLS_SSL_SRV_C) && \
1144 defined(MBEDTLS_SSL_SESSION_TICKETS)
1145 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001147#endif
1148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001151 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001154 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001156 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001160 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001161#endif
1162
Brett Warrene0edc842021-08-17 09:53:13 +01001163/*
1164 * curve_list is translated to IANA TLS group identifiers here because
1165 * mbedtls_ssl_conf_curves returns void and so can't return
1166 * any error codes.
1167 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001168#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001169#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1170 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001172 size_t length;
1173 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1174
Thomas Daubney850a0792023-05-22 12:05:03 +01001175 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
Brett Warrene0edc842021-08-17 09:53:13 +01001177
1178 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1180 if (group_list == NULL) {
1181 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1182 }
Brett Warrene0edc842021-08-17 09:53:13 +01001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001185 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 curve_list[i]);
1187 if (tls_id == 0) {
1188 mbedtls_free(group_list);
1189 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001190 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001191 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001192 }
1193
1194 group_list[length] = 0;
1195
1196 ssl->handshake->group_list = group_list;
1197 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001199 ssl->handshake->group_list = ssl->conf->group_list;
1200 ssl->handshake->group_list_heap_allocated = 0;
1201 }
1202#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001203#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001204
Ronald Crone68ab4f2022-10-05 12:46:29 +02001205#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001206#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001207#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001208 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1209 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1211 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001212 const int *md;
1213 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001214 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001215 uint16_t *p;
1216
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001217 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1218 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1219 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1222 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001223 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001225#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001227#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001228
Jerry Yub476a442022-01-21 18:14:45 +08001229#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001231#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1233 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1234 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1238 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1239 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1242 sizeof(uint16_t));
1243 if (ssl->handshake->sig_algs == NULL) {
1244 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1245 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 p = (uint16_t *) ssl->handshake->sig_algs;
1248 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1249 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1250 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001251 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001253#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001255 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001256#endif
1257#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001259 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001260#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001261 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001262 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001263 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001265#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001266 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001267 ssl->handshake->sig_algs_heap_allocated = 0;
1268 }
Jerry Yucc539102022-06-27 16:27:35 +08001269#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001270#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001272}
1273
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001274#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001275/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001276MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001277static int ssl_cookie_write_dummy(void *ctx,
1278 unsigned char **p, unsigned char *end,
1279 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001280{
1281 ((void) ctx);
1282 ((void) p);
1283 ((void) end);
1284 ((void) cli_id);
1285 ((void) cli_id_len);
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001288}
1289
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static int ssl_cookie_check_dummy(void *ctx,
1292 const unsigned char *cookie, size_t cookie_len,
1293 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001294{
1295 ((void) ctx);
1296 ((void) cookie);
1297 ((void) cookie_len);
1298 ((void) cli_id);
1299 ((void) cli_id_len);
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001302}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001303#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305/*
1306 * Initialize an SSL context
1307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001308void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001309{
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001311}
1312
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001313MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001314static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001315{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001316 const mbedtls_ssl_config *conf = ssl->conf;
1317
Ronald Cron6f135e12021-12-08 16:57:54 +01001318#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1320 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1321 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1322 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001323 }
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1326 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001327 }
1328#endif
1329
1330#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1332 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1333 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001334 }
1335#endif
1336
Ronald Cron6f135e12021-12-08 16:57:54 +01001337#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1339 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1340 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1341 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001342 }
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1345 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001346 }
1347#endif
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1350 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001351}
1352
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001353MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001354static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1355{
1356 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ret = ssl_conf_version_check(ssl);
1358 if (ret != 0) {
1359 return ret;
1360 }
Jerry Yu60835a82021-08-04 10:13:52 +08001361
Yanray Wangb422cab2023-12-01 16:18:10 +08001362 if (ssl->conf->f_rng == NULL) {
1363 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1364 return MBEDTLS_ERR_SSL_NO_RNG;
1365 }
1366
Jerry Yu60835a82021-08-04 10:13:52 +08001367 /* Space for further checks */
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001370}
1371
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001372/*
1373 * Setup an SSL context
1374 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1377 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001378{
Janos Follath865b3eb2019-12-16 11:46:15 +00001379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001380 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1381 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001383 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if ((ret = ssl_conf_check(ssl)) != 0) {
1386 return ret;
1387 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001388 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001389
Paul Bakker62f2dee2012-09-28 07:31:51 +00001390 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001391 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001392 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001393
1394 /* Set to NULL in case of an error condition */
1395 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001396
Darryl Greenb33cc762019-11-28 14:29:44 +00001397#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1398 ssl->in_buf_len = in_buf_len;
1399#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1401 if (ssl->in_buf == NULL) {
1402 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001403 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001404 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001405 }
1406
Darryl Greenb33cc762019-11-28 14:29:44 +00001407#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1408 ssl->out_buf_len = out_buf_len;
1409#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1411 if (ssl->out_buf == NULL) {
1412 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001413 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001414 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 }
1416
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001417 mbedtls_ssl_reset_in_pointers(ssl);
1418 mbedtls_ssl_reset_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001419
Johan Pascalb62bb512015-12-03 21:56:45 +01001420#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001422#endif
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001425 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001429
1430error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 mbedtls_free(ssl->in_buf);
1432 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001433
1434 ssl->conf = NULL;
1435
Darryl Greenb33cc762019-11-28 14:29:44 +00001436#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1437 ssl->in_buf_len = 0;
1438 ssl->out_buf_len = 0;
1439#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001440 ssl->in_buf = NULL;
1441 ssl->out_buf = NULL;
1442
1443 ssl->in_hdr = NULL;
1444 ssl->in_ctr = NULL;
1445 ssl->in_len = NULL;
1446 ssl->in_iv = NULL;
1447 ssl->in_msg = NULL;
1448
1449 ssl->out_hdr = NULL;
1450 ssl->out_ctr = NULL;
1451 ssl->out_len = NULL;
1452 ssl->out_iv = NULL;
1453 ssl->out_msg = NULL;
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001456}
1457
1458/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001459 * Reset an initialized and used SSL context for re-use while retaining
1460 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001461 *
1462 * If partial is non-zero, keep data in the input buffer and client ID.
1463 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1466 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001467{
Darryl Greenb33cc762019-11-28 14:29:44 +00001468#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1469 size_t in_buf_len = ssl->in_buf_len;
1470 size_t out_buf_len = ssl->out_buf_len;
1471#else
1472 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1473 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1474#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Hanno Beckerb0302c42021-08-03 09:39:42 +01001476#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1477 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001478#endif
1479
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001480 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001482
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001483 mbedtls_ssl_reset_in_pointers(ssl);
1484 mbedtls_ssl_reset_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001485
1486 /* Reset incoming message parsing */
1487 ssl->in_offt = NULL;
1488 ssl->nb_zero = 0;
1489 ssl->in_msgtype = 0;
1490 ssl->in_msglen = 0;
1491 ssl->in_hslen = 0;
1492 ssl->keep_current_message = 0;
1493 ssl->transform_in = NULL;
1494
Gilles Peskinecb72cd22025-02-17 16:36:36 +01001495 /* TLS: reset in_hsfraglen, which is part of message parsing.
1496 * DTLS: on a client reconnect, don't reset badmac_seen. */
1497 if (!partial) {
1498 ssl->badmac_seen_or_in_hsfraglen = 0;
1499 }
1500
Hanno Beckerb0302c42021-08-03 09:39:42 +01001501#if defined(MBEDTLS_SSL_PROTO_DTLS)
1502 ssl->next_record_offset = 0;
1503 ssl->in_epoch = 0;
1504#endif
1505
1506 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510 }
1511
Ronald Cronad8c17b2022-06-10 17:18:09 +02001512 ssl->send_alert = 0;
1513
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 /* Reset outgoing message writing */
1515 ssl->out_msgtype = 0;
1516 ssl->out_msglen = 0;
1517 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 memset(ssl->out_buf, 0, out_buf_len);
1519 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001520 ssl->transform_out = NULL;
1521
1522#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001524#endif
1525
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ssl->transform) {
1528 mbedtls_ssl_transform_free(ssl->transform);
1529 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001530 ssl->transform = NULL;
1531 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1533
XiaokangQian2b01dc32022-01-21 02:53:13 +00001534#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_ssl_transform_free(ssl->transform_application);
1536 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537 ssl->transform_application = NULL;
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001540#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1542 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001543 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001544#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1547 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001548 ssl->handshake->transform_handshake = NULL;
1549 }
1550
XiaokangQian2b01dc32022-01-21 02:53:13 +00001551#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001552}
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001555{
1556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1557
Gilles Peskine49f179d2025-03-07 15:09:32 +01001558 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST);
Ronald Cron195c0bc2024-02-08 08:51:20 +01001559 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001562
1563 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_SSL_RENEGOTIATION)
1565 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001566 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001567
1568 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1570 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001573
Hanno Beckerb0302c42021-08-03 09:39:42 +01001574 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001575 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (ssl->session) {
1577 mbedtls_ssl_session_free(ssl->session);
1578 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001579 ssl->session = NULL;
1580 }
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001583 ssl->alpn_chosen = NULL;
1584#endif
1585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001587 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001588#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001590#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (free_cli_id) {
1592 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001593 ssl->cli_id = NULL;
1594 ssl->cli_id_len = 0;
1595 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001596#endif
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((ret = ssl_handshake_init(ssl)) != 0) {
1599 return ret;
1600 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001603}
1604
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001605/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001606 * Reset an initialized and used SSL context for re-use while retaining
1607 * all application-set variables, function pointers and data.
1608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001610{
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001612}
1613
1614/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 * SSL set accessors
1616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001624 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001625}
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001628void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001630 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001631}
1632#endif
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001635{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001636 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1642 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001643{
1644 ssl->disable_datagram_packing = !allow_packing;
1645}
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1648 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->hs_timeout_min = min;
1651 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001652}
1653#endif
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001657 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658}
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1662 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1663 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001665 conf->f_vrfy = f_vrfy;
1666 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001674 conf->f_rng = f_rng;
1675 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676}
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1679 void (*f_dbg)(void *, int, const char *, int, const char *),
1680 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001682 conf->f_dbg = f_dbg;
1683 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684}
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1687 void *p_bio,
1688 mbedtls_ssl_send_t *f_send,
1689 mbedtls_ssl_recv_t *f_recv,
1690 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001691{
1692 ssl->p_bio = p_bio;
1693 ssl->f_send = f_send;
1694 ssl->f_recv = f_recv;
1695 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001696}
1697
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001699void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001700{
1701 ssl->mtu = mtu;
1702}
1703#endif
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001706{
1707 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001708}
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1711 void *p_timer,
1712 mbedtls_ssl_set_timer_t *f_set_timer,
1713 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001714{
1715 ssl->p_timer = p_timer;
1716 ssl->f_set_timer = f_set_timer;
1717 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001718
1719 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001721}
1722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001724void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1725 void *p_cache,
1726 mbedtls_ssl_cache_get_t *f_get_cache,
1727 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001728{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001729 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001730 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001731 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001736int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001737{
Janos Follath865b3eb2019-12-16 11:46:15 +00001738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001741 session == NULL ||
1742 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001745 }
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ssl->handshake->resume == 1) {
1748 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1749 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001750
Jerry Yu21092062022-10-10 21:21:31 +08001751#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron8d630842024-04-04 14:05:21 +02001753#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001754 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001758 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1760 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1761 session->ciphersuite));
1762 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001763 }
Ronald Cron8d630842024-04-04 14:05:21 +02001764#else
1765 /*
1766 * If session tickets are not enabled, it is not possible to resume a
1767 * TLS 1.3 session, thus do not make any change to the SSL context in
1768 * the first place.
1769 */
1770 return 0;
1771#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001772 }
Jerry Yu21092062022-10-10 21:21:31 +08001773#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1776 session)) != 0) {
1777 return ret;
1778 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001779
Paul Bakker0a597072012-09-25 21:55:46 +00001780 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1787 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001788{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001789 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790}
1791
Ronald Cron6f135e12021-12-08 16:57:54 +01001792#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001793void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1794 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001795{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001796 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001797}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001798
1799#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001800void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1801 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001802{
1803 conf->early_data_enabled = early_data_enabled;
1804}
Jerry Yucc4e0072022-11-22 17:22:22 +08001805
1806#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001807void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001809{
Jerry Yu39da9852022-12-06 16:58:36 +08001810 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001811}
1812#endif /* MBEDTLS_SSL_SRV_C */
1813
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001814#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001815#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001818void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1819 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001820{
1821 conf->cert_profile = profile;
1822}
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001825{
1826 mbedtls_ssl_key_cert *cur = key_cert, *next;
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001829 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001831 cur = next;
1832 }
1833}
1834
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001835/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001836MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001837static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1838 mbedtls_x509_crt *cert,
1839 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001840{
niisato8ee24222018-06-25 19:05:48 +09001841 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001844 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001846 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1851 if (new_cert == NULL) {
1852 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1853 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001854
niisato8ee24222018-06-25 19:05:48 +09001855 new_cert->cert = cert;
1856 new_cert->key = key;
1857 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001858
Glenn Strauss36872db2022-01-22 05:06:31 -05001859 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001861 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001863 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
niisato8ee24222018-06-25 19:05:48 +09001867 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001868 }
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001871}
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001874 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001876{
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001878}
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001881 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001883{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001884 conf->ca_chain = ca_chain;
1885 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001886
1887#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1888 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1889 * cannot be used together. */
1890 conf->f_ca_cb = NULL;
1891 conf->p_ca_cb = NULL;
1892#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001893}
Hanno Becker5adaad92019-03-27 16:54:37 +00001894
1895#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001896void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1897 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1898 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001899{
1900 conf->f_ca_cb = f_ca_cb;
1901 conf->p_ca_cb = p_ca_cb;
1902
1903 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1904 * cannot be used together. */
1905 conf->ca_chain = NULL;
1906 conf->ca_crl = NULL;
1907}
1908#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001910
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001912const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1913 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001914{
1915 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001917}
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1920 mbedtls_x509_crt *own_cert,
1921 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001922{
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1924 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001925}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1928 mbedtls_x509_crt *ca_chain,
1929 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001930{
1931 ssl->handshake->sni_ca_chain = ca_chain;
1932 ssl->handshake->sni_ca_crl = ca_crl;
1933}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001934
Glenn Strauss999ef702022-03-11 01:37:23 -05001935#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001936void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1937 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001938{
1939 ssl->handshake->dn_hints = crt;
1940}
1941#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1944 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001945{
1946 ssl->handshake->sni_authmode = authmode;
1947}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001948#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1949
Hanno Becker8927c832019-04-03 12:52:50 +01001950#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001951void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1952 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1953 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001954{
1955 ssl->f_vrfy = f_vrfy;
1956 ssl->p_vrfy = p_vrfy;
1957}
1958#endif
1959
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001960#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001961
Valerio Setti016f6822022-12-09 14:17:50 +01001962#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001963static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1964static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1965
Valerio Setti016f6822022-12-09 14:17:50 +01001966static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 mbedtls_ssl_context *ssl,
1968 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001969{
1970 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001971 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001972 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001973 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001974 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001975 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1977 psa_pake_cs_set_primitive(&cipher_suite,
1978 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1979 PSA_ECC_FAMILY_SECP_R1,
1980 256));
1981 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1984 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001985 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001989 user = jpake_server_id;
1990 user_len = sizeof(jpake_server_id);
1991 peer = jpake_client_id;
1992 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001994 user = jpake_client_id;
1995 user_len = sizeof(jpake_client_id);
1996 peer = jpake_server_id;
1997 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001999
Przemek Stekiel4cd20312023-03-01 11:11:28 +01002000 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2001 if (status != PSA_SUCCESS) {
2002 return status;
2003 }
2004
2005 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002007 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 }
Valerio Setti016f6822022-12-09 14:17:50 +01002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2011 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002012 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Valerio Setti016f6822022-12-09 14:17:50 +01002014
2015 ssl->handshake->psa_pake_ctx_is_ok = 1;
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002018}
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2021 const unsigned char *pw,
2022 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002023{
2024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2025 psa_status_t status;
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (ssl->handshake == NULL || ssl->conf == NULL) {
2028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002029 }
2030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 /* Empty password is not valid */
2032 if ((pw == NULL) || (pw_len == 0)) {
2033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2034 }
2035
2036 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2037 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2038 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2039
2040 status = psa_import_key(&attributes, pw, pw_len,
2041 &ssl->handshake->psa_pake_password);
2042 if (status != PSA_SUCCESS) {
2043 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2044 }
2045
2046 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2047 ssl->handshake->psa_pake_password);
2048 if (status != PSA_SUCCESS) {
2049 psa_destroy_key(ssl->handshake->psa_pake_password);
2050 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2051 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2052 }
2053
2054 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002055}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2058 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002059{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002060 psa_status_t status;
2061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (ssl->handshake == NULL || ssl->conf == NULL) {
2063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002064 }
2065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (mbedtls_svc_key_id_is_null(pwd)) {
2067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2068 }
2069
2070 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2071 if (status != PSA_SUCCESS) {
2072 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2073 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2074 }
2075
2076 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002077}
2078#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2080 const unsigned char *pw,
2081 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002082{
2083 mbedtls_ecjpake_role role;
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (ssl->handshake == NULL || ssl->conf == NULL) {
2086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2087 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002088
Valerio Settic689ed82022-12-07 14:40:38 +01002089 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if ((pw == NULL) || (pw_len == 0)) {
2091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2092 }
Valerio Settic689ed82022-12-07 14:40:38 +01002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002095 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002097 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2101 role,
2102 MBEDTLS_MD_SHA256,
2103 MBEDTLS_ECP_DP_SECP256R1,
2104 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002105}
Valerio Setti02c25b52022-11-15 14:08:42 +01002106#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002107#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002108
Ronald Cron73fe8df2022-10-05 14:31:43 +02002109#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002110int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002111{
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if (conf->psk_identity == NULL ||
2113 conf->psk_identity_len == 0) {
2114 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002115 }
2116
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2119 return 1;
2120 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002121#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (conf->psk != NULL && conf->psk_len != 0) {
2124 return 1;
2125 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002128}
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131{
2132 /* Remove reference to existing PSK, if any. */
2133#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002135 /* The maintenance of the PSK key slot is the
2136 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002137 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002138 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002139#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002141 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002142 conf->psk = NULL;
2143 conf->psk_len = 0;
2144 }
2145
2146 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (conf->psk_identity != NULL) {
2148 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002149 conf->psk_identity = NULL;
2150 conf->psk_identity_len = 0;
2151 }
2152}
2153
Hanno Becker7390c712018-11-15 13:33:04 +00002154/* This function assumes that PSK identity in the SSL config is unset.
2155 * It checks that the provided identity is well-formed and attempts
2156 * to make a copy of it in the SSL config.
2157 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002159static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2160 unsigned char const *psk_identity,
2161 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002162{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002163 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002165 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 (psk_identity_len >> 16) != 0 ||
2167 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2168 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002169 }
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2172 if (conf->psk_identity == NULL) {
2173 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2174 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002175
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002176 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002180}
2181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2183 const unsigned char *psk, size_t psk_len,
2184 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002185{
Janos Follath865b3eb2019-12-16 11:46:15 +00002186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002187
2188 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2190 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2191 }
Hanno Becker7390c712018-11-15 13:33:04 +00002192
2193 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (psk == NULL) {
2195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2196 }
2197 if (psk_len == 0) {
2198 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2199 }
2200 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2201 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2202 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2205 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2206 }
Hanno Becker7390c712018-11-15 13:33:04 +00002207 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002209
2210 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2212 if (ret != 0) {
2213 ssl_conf_remove_psk(conf);
2214 }
Hanno Becker7390c712018-11-15 13:33:04 +00002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002217}
2218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002220{
2221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002223 /* The maintenance of the external PSK key slot is the
2224 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (ssl->handshake->psk_opaque_is_internal) {
2226 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002227 ssl->handshake->psk_opaque_is_internal = 0;
2228 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002229 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002230 }
Neil Armstronge952a302022-05-03 10:22:14 +02002231#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002233 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002235 ssl->handshake->psk_len = 0;
lhuang046d4d94f2024-06-11 12:37:02 -07002236 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002237 }
Neil Armstronge952a302022-05-03 10:22:14 +02002238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002239}
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2242 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002243{
Neil Armstrong501c9322022-05-03 09:35:09 +02002244#if defined(MBEDTLS_USE_PSA_CRYPTO)
2245 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002247 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002249#endif /* MBEDTLS_USE_PSA_CRYPTO */
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (psk == NULL || ssl->handshake == NULL) {
2252 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2253 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2256 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2257 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002260
Neil Armstrong501c9322022-05-03 09:35:09 +02002261#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2264 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2265 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2266 } else {
2267 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2268 }
2269 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002270 }
2271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002272
Jerry Yu568ec252022-07-22 21:27:34 +08002273#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2275 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2276 psa_set_key_usage_flags(&key_attributes,
2277 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002278 }
2279#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 psa_set_key_algorithm(&key_attributes, alg);
2282 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2285 if (status != PSA_SUCCESS) {
2286 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2287 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002288
2289 /* Allow calling psa_destroy_key() on psk remove */
2290 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002292#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2294 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2295 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002296
2297 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002301#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002302}
2303
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002305int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2306 mbedtls_svc_key_id_t psk,
2307 const unsigned char *psk_identity,
2308 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002309{
Janos Follath865b3eb2019-12-16 11:46:15 +00002310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002311
2312 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2314 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2315 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002316
Hanno Becker7390c712018-11-15 13:33:04 +00002317 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (mbedtls_svc_key_id_is_null(psk)) {
2319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2320 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002321 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002322
2323 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2325 psk_identity_len);
2326 if (ret != 0) {
2327 ssl_conf_remove_psk(conf);
2328 }
Hanno Becker7390c712018-11-15 13:33:04 +00002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2334 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002335{
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if ((mbedtls_svc_key_id_is_null(psk)) ||
2337 (ssl->handshake == NULL)) {
2338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2339 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002342 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002344}
2345#endif /* MBEDTLS_USE_PSA_CRYPTO */
2346
Jerry Yu8897c072022-08-12 13:56:53 +08002347#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002348void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2349 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2350 size_t),
2351 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002352{
2353 conf->f_psk = f_psk;
2354 conf->p_psk = p_psk;
2355}
Jerry Yu8897c072022-08-12 13:56:53 +08002356#endif /* MBEDTLS_SSL_SRV_C */
2357
Ronald Cron73fe8df2022-10-05 14:31:43 +02002358#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002359
2360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002361static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002363{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002364#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (alg == PSA_ALG_CBC_NO_PADDING) {
2366 return MBEDTLS_SSL_MODE_CBC;
2367 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002368#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (PSA_ALG_IS_AEAD(alg)) {
2370 return MBEDTLS_SSL_MODE_AEAD;
2371 }
2372 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002373}
2374
2375#else /* MBEDTLS_USE_PSA_CRYPTO */
2376
2377static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002379{
2380#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (mode == MBEDTLS_MODE_CBC) {
2382 return MBEDTLS_SSL_MODE_CBC;
2383 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002384#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2385
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002386#if defined(MBEDTLS_GCM_C) || \
2387 defined(MBEDTLS_CCM_C) || \
2388 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002390 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 mode == MBEDTLS_MODE_CHACHAPOLY) {
2392 return MBEDTLS_SSL_MODE_AEAD;
2393 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002394#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002397}
Gilles Peskine301711e2022-04-26 16:57:05 +02002398#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002399
Gilles Peskinee108d982022-04-26 16:50:40 +02002400static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2401 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002403{
2404#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2406 base_mode == MBEDTLS_SSL_MODE_CBC) {
2407 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002408 }
2409#else
2410 (void) encrypt_then_mac;
2411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002413}
2414
Neil Armstrongab555e02022-04-04 11:07:59 +02002415mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002417{
Gilles Peskinee108d982022-04-26 16:50:40 +02002418 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002419#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002421#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002423#endif
2424 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002425
Gilles Peskinee108d982022-04-26 16:50:40 +02002426 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002428 encrypt_then_mac = transform->encrypt_then_mac;
2429#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002431}
2432
Neil Armstrongab555e02022-04-04 11:07:59 +02002433mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002434#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002436#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002438{
Gilles Peskinee108d982022-04-26 16:50:40 +02002439 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2440
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002441#if defined(MBEDTLS_USE_PSA_CRYPTO)
2442 psa_status_t status;
2443 psa_algorithm_t alg;
2444 psa_key_type_t type;
2445 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002446 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2447 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (status == PSA_SUCCESS) {
2449 base_mode = mbedtls_ssl_get_base_mode(alg);
2450 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002451#else
2452 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002453 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002455 base_mode =
2456 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002458 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002459#endif /* MBEDTLS_USE_PSA_CRYPTO */
2460
Gilles Peskinee108d982022-04-26 16:50:40 +02002461#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2462 int encrypt_then_mac = 0;
2463#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002465}
2466
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002467#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2470 size_t taglen,
2471 psa_algorithm_t *alg,
2472 psa_key_type_t *key_type,
2473 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002474{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002475#if !defined(MBEDTLS_SSL_HAVE_CCM)
2476 (void) taglen;
2477#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002479#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002480 case MBEDTLS_CIPHER_AES_128_CBC:
2481 *alg = PSA_ALG_CBC_NO_PADDING;
2482 *key_type = PSA_KEY_TYPE_AES;
2483 *key_size = 128;
2484 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002485#endif
2486#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002487 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002489 *key_type = PSA_KEY_TYPE_AES;
2490 *key_size = 128;
2491 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002492#endif
2493#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002494 case MBEDTLS_CIPHER_AES_128_GCM:
2495 *alg = PSA_ALG_GCM;
2496 *key_type = PSA_KEY_TYPE_AES;
2497 *key_size = 128;
2498 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002499#endif
2500#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002501 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002503 *key_type = PSA_KEY_TYPE_AES;
2504 *key_size = 192;
2505 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002506#endif
2507#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002508 case MBEDTLS_CIPHER_AES_192_GCM:
2509 *alg = PSA_ALG_GCM;
2510 *key_type = PSA_KEY_TYPE_AES;
2511 *key_size = 192;
2512 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002513#endif
2514#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002515 case MBEDTLS_CIPHER_AES_256_CBC:
2516 *alg = PSA_ALG_CBC_NO_PADDING;
2517 *key_type = PSA_KEY_TYPE_AES;
2518 *key_size = 256;
2519 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002520#endif
2521#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002522 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002524 *key_type = PSA_KEY_TYPE_AES;
2525 *key_size = 256;
2526 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002527#endif
2528#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002529 case MBEDTLS_CIPHER_AES_256_GCM:
2530 *alg = PSA_ALG_GCM;
2531 *key_type = PSA_KEY_TYPE_AES;
2532 *key_size = 256;
2533 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002534#endif
2535#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002536 case MBEDTLS_CIPHER_ARIA_128_CBC:
2537 *alg = PSA_ALG_CBC_NO_PADDING;
2538 *key_type = PSA_KEY_TYPE_ARIA;
2539 *key_size = 128;
2540 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002541#endif
2542#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002543 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002545 *key_type = PSA_KEY_TYPE_ARIA;
2546 *key_size = 128;
2547 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002548#endif
2549#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002550 case MBEDTLS_CIPHER_ARIA_128_GCM:
2551 *alg = PSA_ALG_GCM;
2552 *key_type = PSA_KEY_TYPE_ARIA;
2553 *key_size = 128;
2554 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002555#endif
2556#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002557 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002559 *key_type = PSA_KEY_TYPE_ARIA;
2560 *key_size = 192;
2561 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002562#endif
2563#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002564 case MBEDTLS_CIPHER_ARIA_192_GCM:
2565 *alg = PSA_ALG_GCM;
2566 *key_type = PSA_KEY_TYPE_ARIA;
2567 *key_size = 192;
2568 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002569#endif
2570#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002571 case MBEDTLS_CIPHER_ARIA_256_CBC:
2572 *alg = PSA_ALG_CBC_NO_PADDING;
2573 *key_type = PSA_KEY_TYPE_ARIA;
2574 *key_size = 256;
2575 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002576#endif
2577#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002578 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002580 *key_type = PSA_KEY_TYPE_ARIA;
2581 *key_size = 256;
2582 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002583#endif
2584#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002585 case MBEDTLS_CIPHER_ARIA_256_GCM:
2586 *alg = PSA_ALG_GCM;
2587 *key_type = PSA_KEY_TYPE_ARIA;
2588 *key_size = 256;
2589 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002590#endif
2591#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002592 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2593 *alg = PSA_ALG_CBC_NO_PADDING;
2594 *key_type = PSA_KEY_TYPE_CAMELLIA;
2595 *key_size = 128;
2596 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002597#endif
2598#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002599 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002601 *key_type = PSA_KEY_TYPE_CAMELLIA;
2602 *key_size = 128;
2603 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002604#endif
2605#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002606 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2607 *alg = PSA_ALG_GCM;
2608 *key_type = PSA_KEY_TYPE_CAMELLIA;
2609 *key_size = 128;
2610 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002611#endif
2612#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002613 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002615 *key_type = PSA_KEY_TYPE_CAMELLIA;
2616 *key_size = 192;
2617 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002618#endif
2619#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002620 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2621 *alg = PSA_ALG_GCM;
2622 *key_type = PSA_KEY_TYPE_CAMELLIA;
2623 *key_size = 192;
2624 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002625#endif
2626#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002627 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2628 *alg = PSA_ALG_CBC_NO_PADDING;
2629 *key_type = PSA_KEY_TYPE_CAMELLIA;
2630 *key_size = 256;
2631 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002632#endif
2633#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002634 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002636 *key_type = PSA_KEY_TYPE_CAMELLIA;
2637 *key_size = 256;
2638 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002639#endif
2640#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002641 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2642 *alg = PSA_ALG_GCM;
2643 *key_type = PSA_KEY_TYPE_CAMELLIA;
2644 *key_size = 256;
2645 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002646#endif
2647#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002648 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2649 *alg = PSA_ALG_CHACHA20_POLY1305;
2650 *key_type = PSA_KEY_TYPE_CHACHA20;
2651 *key_size = 256;
2652 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002653#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002654 case MBEDTLS_CIPHER_NULL:
2655 *alg = MBEDTLS_SSL_NULL_CIPHER;
2656 *key_type = 0;
2657 *key_size = 0;
2658 break;
2659 default:
2660 return PSA_ERROR_NOT_SUPPORTED;
2661 }
2662
2663 return PSA_SUCCESS;
2664}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002665#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002666
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002667#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002668int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2669 const unsigned char *dhm_P, size_t P_len,
2670 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002671{
Janos Follath865b3eb2019-12-16 11:46:15 +00002672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 mbedtls_mpi_free(&conf->dhm_P);
2675 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2678 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2679 mbedtls_mpi_free(&conf->dhm_P);
2680 mbedtls_mpi_free(&conf->dhm_G);
2681 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002682 }
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002685}
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002688{
Janos Follath865b3eb2019-12-16 11:46:15 +00002689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 mbedtls_mpi_free(&conf->dhm_P);
2692 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2695 &conf->dhm_P)) != 0 ||
2696 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2697 &conf->dhm_G)) != 0) {
2698 mbedtls_mpi_free(&conf->dhm_P);
2699 mbedtls_mpi_free(&conf->dhm_G);
2700 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002701 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002704}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002705#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002706
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002707#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2708/*
2709 * Set the minimum length for Diffie-Hellman parameters
2710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002711void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2712 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002713{
2714 conf->dhm_min_bitlen = bitlen;
2715}
2716#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2717
Ronald Crone68ab4f2022-10-05 12:46:29 +02002718#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002719#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002720/*
2721 * Set allowed/preferred hashes for handshake signatures
2722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2724 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002725{
2726 conf->sig_hashes = hashes;
2727}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002728#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002729
Jerry Yuf017ee42022-01-12 15:49:48 +08002730/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2732 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002733{
Jerry Yuf017ee42022-01-12 15:49:48 +08002734#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2735 conf->sig_hashes = NULL;
2736#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2737 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002738}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002739#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002740
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002741#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002742#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002743/*
2744 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002745 *
2746 * mbedtls_ssl_setup() takes the provided list
2747 * and translates it to a list of IANA TLS group identifiers,
2748 * stored in ssl->handshake->group_list.
2749 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002750 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002751void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2752 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002754 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002755 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002756}
Brett Warrene0edc842021-08-17 09:53:13 +01002757#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002758#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002759
Brett Warrene0edc842021-08-17 09:53:13 +01002760/*
2761 * Set the allowed groups
2762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2764 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002765{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002766#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002767 conf->curve_list = NULL;
2768#endif
2769 conf->group_list = group_list;
2770}
2771
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002772#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskinee61852e2025-02-12 23:28:48 +01002773
Gilles Peskine6b885942025-02-12 23:53:25 +01002774/* A magic value for `ssl->hostname` indicating that
2775 * mbedtls_ssl_set_hostname() has been called with `NULL`.
2776 * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then
2777 * `ssl->hostname == NULL`. */
2778static const char *const ssl_hostname_skip_cn_verification = "";
2779
Gilles Peskinee61852e2025-02-12 23:28:48 +01002780#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2781/** Whether mbedtls_ssl_set_hostname() has been called.
2782 *
2783 * \param[in] ssl SSL context
2784 *
2785 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
2786 * (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
2787 * otherwise \c 0.
2788 */
2789static int mbedtls_ssl_has_set_hostname_been_called(
2790 const mbedtls_ssl_context *ssl)
2791{
Gilles Peskinee61852e2025-02-12 23:28:48 +01002792 return ssl->hostname != NULL;
2793}
2794#endif
2795
2796/* Micro-optimization: don't export this function if it isn't needed outside
2797 * of this source file. */
2798#if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2799static
2800#endif
2801const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
2802{
Gilles Peskine6b885942025-02-12 23:53:25 +01002803 if (ssl->hostname == ssl_hostname_skip_cn_verification) {
2804 return NULL;
2805 }
Gilles Peskinee61852e2025-02-12 23:28:48 +01002806 return ssl->hostname;
2807}
2808
2809static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
2810{
Gilles Peskine6b885942025-02-12 23:53:25 +01002811 if (ssl->hostname != NULL &&
2812 ssl->hostname != ssl_hostname_skip_cn_verification) {
Gilles Peskinee61852e2025-02-12 23:28:48 +01002813 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
2814 }
2815 ssl->hostname = NULL;
2816}
2817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002819{
Hanno Becker947194e2017-04-07 13:25:49 +01002820 /* Initialize to suppress unnecessary compiler warning */
2821 size_t hostname_len = 0;
2822
2823 /* Check if new hostname is valid before
2824 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 if (hostname != NULL) {
2826 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2829 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2830 }
Hanno Becker947194e2017-04-07 13:25:49 +01002831 }
2832
2833 /* Now it's clear that we will overwrite the old hostname,
2834 * so we can free it safely */
Gilles Peskinee61852e2025-02-12 23:28:48 +01002835 mbedtls_ssl_free_hostname(ssl);
Hanno Becker947194e2017-04-07 13:25:49 +01002836
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 if (hostname == NULL) {
Gilles Peskine6b885942025-02-12 23:53:25 +01002838 /* Passing NULL as hostname clears the old one, but leaves a
2839 * special marker to indicate that mbedtls_ssl_set_hostname()
2840 * has been called. */
2841 /* ssl->hostname should be const, but isn't. We won't actually
2842 * write to the buffer, so it's ok to cast away the const. */
2843 ssl->hostname = (char *) ssl_hostname_skip_cn_verification;
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 } else {
2845 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2846 if (ssl->hostname == NULL) {
Gilles Peskine6b885942025-02-12 23:53:25 +01002847 /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully.
2848 * Leave ssl->hostname in the same state as if the function had
2849 * not been called, i.e. a null pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2851 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002854
Hanno Becker947194e2017-04-07 13:25:49 +01002855 ssl->hostname[hostname_len] = '\0';
2856 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002859}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002860#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002862#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002863void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2864 int (*f_sni)(void *, mbedtls_ssl_context *,
2865 const unsigned char *, size_t),
2866 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002868 conf->f_sni = f_sni;
2869 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002874int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002875{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002876 size_t cur_len, tot_len;
2877 const char **p;
2878
2879 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002880 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2881 * MUST NOT be truncated."
2882 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002883 */
2884 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 for (p = protos; *p != NULL; p++) {
2886 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002887 tot_len += cur_len;
2888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if ((cur_len == 0) ||
2890 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2891 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2892 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2893 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002894 }
2895
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002896 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002899}
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002902{
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002904}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002906
Johan Pascalb62bb512015-12-03 21:56:45 +01002907#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002908void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2909 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002910{
2911 conf->dtls_srtp_mki_support = support_mki_value;
2912}
2913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2915 unsigned char *mki_value,
2916 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002917{
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2919 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002920 }
Ron Eldor591f1622018-01-22 12:30:04 +02002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2923 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002924 }
Ron Eldor591f1622018-01-22 12:30:04 +02002925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002927 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002929}
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2932 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002933{
Johan Pascal253d0262020-09-22 13:04:45 +02002934 const mbedtls_ssl_srtp_profile *p;
2935 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002936
Johan Pascal253d0262020-09-22 13:04:45 +02002937 /* check the profiles list: all entry must be valid,
2938 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2940 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2941 p++) {
2942 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002943 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002945 /* unsupported value, stop parsing and set the size to an error value */
2946 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002947 }
2948 }
2949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2951 conf->dtls_srtp_profile_list = NULL;
2952 conf->dtls_srtp_profile_list_len = 0;
2953 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002954 }
2955
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002956 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002957 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002960}
2961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2963 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002964{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002965 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2966 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002968 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002970 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2972 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002973 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002974}
Johan Pascalb62bb512015-12-03 21:56:45 +01002975#endif /* MBEDTLS_SSL_DTLS_SRTP */
2976
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302977#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002978void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002979{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002980 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002981}
2982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002984{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002985 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002986}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302987#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002988
Janos Follath088ce432017-04-10 12:42:31 +01002989#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002990void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2991 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002992{
2993 conf->cert_req_ca_list = cert_req_ca_list;
2994}
2995#endif
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002998void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002999{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003000 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003001}
3002#endif
3003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003006{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003007 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003008}
3009#endif
3010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003012int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003013{
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3015 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003017 }
3018
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003019 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003023#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003026{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003027 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003028}
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003031void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003032{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003033 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003034}
3035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003037{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003038 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003039}
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3042 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003043{
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003049#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003052{
Ronald Crond67f8012024-08-28 07:45:57 +02003053 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3054 conf->session_tickets |= (use_tickets != 0) <<
3055 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003056}
Ronald Crond67f8012024-08-28 07:45:57 +02003057
Ronald Cronbedddd72024-08-27 14:18:50 +02003058#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003059void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3060 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003061{
Ronald Crond67f8012024-08-28 07:45:57 +02003062 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003063 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003064 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3065}
Ronald Cronbedddd72024-08-27 14:18:50 +02003066#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3067#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003068
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003069#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003070
Jerry Yud0766ec2022-09-22 10:46:57 +08003071#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003072void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3073 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003074{
Jerry Yud0766ec2022-09-22 10:46:57 +08003075 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003076}
3077#endif
3078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3080 mbedtls_ssl_ticket_write_t *f_ticket_write,
3081 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3082 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003083{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003084 conf->f_ticket_write = f_ticket_write;
3085 conf->f_ticket_parse = f_ticket_parse;
3086 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003087}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3092 mbedtls_ssl_export_keys_t *f_export_keys,
3093 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003094{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003095 ssl->f_export_keys = f_export_keys;
3096 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003097}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003098
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003099#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003100void mbedtls_ssl_conf_async_private_cb(
3101 mbedtls_ssl_config *conf,
3102 mbedtls_ssl_async_sign_t *f_async_sign,
3103 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3104 mbedtls_ssl_async_resume_t *f_async_resume,
3105 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003107{
3108 conf->f_async_sign_start = f_async_sign;
3109 conf->f_async_decrypt_start = f_async_decrypt;
3110 conf->f_async_resume = f_async_resume;
3111 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003112 conf->p_async_config_data = async_config_data;
3113}
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003116{
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003118}
3119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003121{
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (ssl->handshake == NULL) {
3123 return NULL;
3124 } else {
3125 return ssl->handshake->user_async_ctx;
3126 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003127}
3128
Gilles Peskine449bd832023-01-11 14:50:10 +01003129void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3130 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003131{
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003133 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003135}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003136#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003137
Paul Bakker5121ce52009-01-03 21:22:43 +00003138/*
3139 * SSL get accessors
3140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003142{
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ssl->session != NULL) {
3144 return ssl->session->verify_result;
3145 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if (ssl->session_negotiate != NULL) {
3148 return ssl->session_negotiate->verify_result;
3149 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003152}
3153
Gilles Peskine449bd832023-01-11 14:50:10 +01003154int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003155{
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 if (ssl == NULL || ssl->session == NULL) {
3157 return 0;
3158 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003161}
3162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003164{
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 if (ssl == NULL || ssl->session == NULL) {
3166 return NULL;
3167 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003170}
3171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3176 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003177 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003179 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003181 }
3182 }
3183#endif
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003186 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003188 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003190 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003192 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003193}
3194
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003195#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3196
3197size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3198{
3199 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3200 size_t record_size_limit = max_len;
3201
3202 if (ssl->session != NULL &&
3203 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3204 ssl->session->record_size_limit < max_len) {
3205 record_size_limit = ssl->session->record_size_limit;
3206 }
3207
3208 // TODO: this is currently untested
3209 /* During a handshake, use the value being negotiated */
3210 if (ssl->session_negotiate != NULL &&
3211 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3212 ssl->session_negotiate->record_size_limit < max_len) {
3213 record_size_limit = ssl->session_negotiate->record_size_limit;
3214 }
3215
3216 return record_size_limit;
3217}
3218#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3219
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003220#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003221size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003222{
David Horstmann95d516f2021-05-04 18:36:56 +01003223 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003224 size_t read_mfl;
3225
Jerry Yuddda0502022-12-01 19:43:12 +08003226#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003227 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3229 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3230 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003231 }
Jerry Yuddda0502022-12-01 19:43:12 +08003232#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003233
3234 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 if (ssl->session_out != NULL) {
3236 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3237 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003238 max_len = read_mfl;
3239 }
3240 }
3241
Jerry Yuddda0502022-12-01 19:43:12 +08003242 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (ssl->session_negotiate != NULL) {
3244 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3245 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003246 max_len = read_mfl;
3247 }
3248 }
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003251}
3252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003254{
3255 size_t max_len;
3256
3257 /*
3258 * Assume mfl_code is correct since it was checked when set
3259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003261
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003262 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->session_out != NULL &&
3264 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3265 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003266 }
3267
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003268 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (ssl->session_negotiate != NULL &&
3270 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3271 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003272 }
3273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003275}
3276#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3277
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003278#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003279size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003280{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003281 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3283 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3284 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3285 return 0;
3286 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003287
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3289 return ssl->mtu;
3290 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ssl->mtu == 0) {
3293 return ssl->handshake->mtu;
3294 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 return ssl->mtu < ssl->handshake->mtu ?
3297 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003298}
3299#endif /* MBEDTLS_SSL_PROTO_DTLS */
3300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302{
3303 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3304
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003305#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003306 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003307 !defined(MBEDTLS_SSL_PROTO_DTLS)
3308 (void) ssl;
3309#endif
3310
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003311#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003315 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003317#endif
3318
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003319#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3320 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3321
3322 if (max_len > record_size_limit) {
3323 max_len = record_size_limit;
3324 }
3325#endif
3326
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003327 if (ssl->transform_out != NULL &&
3328 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003329 /*
3330 * In TLS 1.3 case, when records are protected, `max_len` as computed
3331 * above is the maximum length of the TLSInnerPlaintext structure that
3332 * along the plaintext payload contains the inner content type (one byte)
3333 * and some zero padding. Given the algorithm used for padding
3334 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3335 * the plaintext payload. Round down to a multiple of
3336 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3337 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003338 */
3339 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3340 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3341 }
3342
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3345 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3346 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003347 const size_t overhead = (size_t) ret;
3348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ret < 0) {
3350 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003351 }
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 if (mtu <= overhead) {
3354 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3355 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3356 }
3357
3358 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003359 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003361 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003362#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003363
Hanno Becker0defedb2018-08-10 12:35:02 +01003364#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003365 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3366 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003367 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003368#endif
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003371}
3372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003374{
3375 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3376
3377#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3378 (void) ssl;
3379#endif
3380
3381#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003385 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003387#endif
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003390}
3391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003393const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003394{
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (ssl == NULL || ssl->session == NULL) {
3396 return NULL;
3397 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003398
Hanno Beckere6824572019-02-07 13:18:46 +00003399#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003401#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003403#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003404}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003408int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3409 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003410{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003411 int ret;
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003414 dst == NULL ||
3415 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003418 }
3419
Hanno Beckere810bbc2021-05-14 16:01:05 +01003420 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3421 * idempotent: Each session can only be exported once.
3422 *
3423 * (This is in preparation for TLS 1.3 support where we will
3424 * need the ability to export multiple sessions (aka tickets),
3425 * which will be achieved by calling mbedtls_ssl_get_session()
3426 * multiple times until it fails.)
3427 *
3428 * Check whether we have already exported the current session,
3429 * and fail if so.
3430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 if (ssl->session->exported == 1) {
3432 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3433 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3436 if (ret != 0) {
3437 return ret;
3438 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003439
3440 /* Remember that we've exported the session. */
3441 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003445
David Horstmanncb01b362024-02-29 17:31:46 +00003446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3447
3448/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003449 *
David Horstmanncb01b362024-02-29 17:31:46 +00003450 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003451 */
3452static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3453 unsigned char *buf,
3454 size_t buf_len)
3455{
3456 unsigned char *p = buf;
3457 size_t used = 0;
3458
3459#if defined(MBEDTLS_HAVE_TIME)
3460 uint64_t start;
3461#endif
3462#if defined(MBEDTLS_X509_CRT_PARSE_C)
3463#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3464 size_t cert_len;
3465#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3466#endif /* MBEDTLS_X509_CRT_PARSE_C */
3467
3468 /*
3469 * Time
3470 */
3471#if defined(MBEDTLS_HAVE_TIME)
3472 used += 8;
3473
3474 if (used <= buf_len) {
3475 start = (uint64_t) session->start;
3476
3477 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3478 p += 8;
3479 }
3480#endif /* MBEDTLS_HAVE_TIME */
3481
3482 /*
3483 * Basic mandatory fields
3484 */
3485 used += 1 /* id_len */
3486 + sizeof(session->id)
3487 + sizeof(session->master)
3488 + 4; /* verify_result */
3489
3490 if (used <= buf_len) {
3491 *p++ = MBEDTLS_BYTE_0(session->id_len);
3492 memcpy(p, session->id, 32);
3493 p += 32;
3494
3495 memcpy(p, session->master, 48);
3496 p += 48;
3497
3498 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3499 p += 4;
3500 }
3501
3502 /*
3503 * Peer's end-entity certificate
3504 */
3505#if defined(MBEDTLS_X509_CRT_PARSE_C)
3506#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3507 if (session->peer_cert == NULL) {
3508 cert_len = 0;
3509 } else {
3510 cert_len = session->peer_cert->raw.len;
3511 }
3512
3513 used += 3 + cert_len;
3514
3515 if (used <= buf_len) {
3516 *p++ = MBEDTLS_BYTE_2(cert_len);
3517 *p++ = MBEDTLS_BYTE_1(cert_len);
3518 *p++ = MBEDTLS_BYTE_0(cert_len);
3519
3520 if (session->peer_cert != NULL) {
3521 memcpy(p, session->peer_cert->raw.p, cert_len);
3522 p += cert_len;
3523 }
3524 }
3525#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3526 if (session->peer_cert_digest != NULL) {
3527 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3528 if (used <= buf_len) {
3529 *p++ = (unsigned char) session->peer_cert_digest_type;
3530 *p++ = (unsigned char) session->peer_cert_digest_len;
3531 memcpy(p, session->peer_cert_digest,
3532 session->peer_cert_digest_len);
3533 p += session->peer_cert_digest_len;
3534 }
3535 } else {
3536 used += 2;
3537 if (used <= buf_len) {
3538 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3539 *p++ = 0;
3540 }
3541 }
3542#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3543#endif /* MBEDTLS_X509_CRT_PARSE_C */
3544
3545 /*
3546 * Session ticket if any, plus associated data
3547 */
3548#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3549#if defined(MBEDTLS_SSL_CLI_C)
3550 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3551 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3552
3553 if (used <= buf_len) {
3554 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3555 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3556 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3557
3558 if (session->ticket != NULL) {
3559 memcpy(p, session->ticket, session->ticket_len);
3560 p += session->ticket_len;
3561 }
3562
3563 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3564 p += 4;
3565 }
3566 }
3567#endif /* MBEDTLS_SSL_CLI_C */
3568#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3569 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3570 used += 8;
3571
3572 if (used <= buf_len) {
3573 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3574 p += 8;
3575 }
3576 }
3577#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3578#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3579
3580 /*
3581 * Misc extension-related info
3582 */
3583#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3584 used += 1;
3585
3586 if (used <= buf_len) {
3587 *p++ = session->mfl_code;
3588 }
3589#endif
3590
3591#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3592 used += 1;
3593
3594 if (used <= buf_len) {
3595 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3596 }
3597#endif
3598
3599 return used;
3600}
3601
3602MBEDTLS_CHECK_RETURN_CRITICAL
3603static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3604 const unsigned char *buf,
3605 size_t len)
3606{
3607#if defined(MBEDTLS_HAVE_TIME)
3608 uint64_t start;
3609#endif
3610#if defined(MBEDTLS_X509_CRT_PARSE_C)
3611#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3612 size_t cert_len;
3613#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3614#endif /* MBEDTLS_X509_CRT_PARSE_C */
3615
3616 const unsigned char *p = buf;
3617 const unsigned char * const end = buf + len;
3618
3619 /*
3620 * Time
3621 */
3622#if defined(MBEDTLS_HAVE_TIME)
3623 if (8 > (size_t) (end - p)) {
3624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3625 }
3626
3627 start = MBEDTLS_GET_UINT64_BE(p, 0);
3628 p += 8;
3629
3630 session->start = (time_t) start;
3631#endif /* MBEDTLS_HAVE_TIME */
3632
3633 /*
3634 * Basic mandatory fields
3635 */
3636 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3638 }
3639
3640 session->id_len = *p++;
3641 memcpy(session->id, p, 32);
3642 p += 32;
3643
3644 memcpy(session->master, p, 48);
3645 p += 48;
3646
3647 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3648 p += 4;
3649
3650 /* Immediately clear invalid pointer values that have been read, in case
3651 * we exit early before we replaced them with valid ones. */
3652#if defined(MBEDTLS_X509_CRT_PARSE_C)
3653#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3654 session->peer_cert = NULL;
3655#else
3656 session->peer_cert_digest = NULL;
3657#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3658#endif /* MBEDTLS_X509_CRT_PARSE_C */
3659#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3660 session->ticket = NULL;
3661#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3662
3663 /*
3664 * Peer certificate
3665 */
3666#if defined(MBEDTLS_X509_CRT_PARSE_C)
3667#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3668 /* Deserialize CRT from the end of the ticket. */
3669 if (3 > (size_t) (end - p)) {
3670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3671 }
3672
3673 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3674 p += 3;
3675
3676 if (cert_len != 0) {
3677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3678
3679 if (cert_len > (size_t) (end - p)) {
3680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3681 }
3682
3683 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3684
3685 if (session->peer_cert == NULL) {
3686 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3687 }
3688
3689 mbedtls_x509_crt_init(session->peer_cert);
3690
3691 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3692 p, cert_len)) != 0) {
3693 mbedtls_x509_crt_free(session->peer_cert);
3694 mbedtls_free(session->peer_cert);
3695 session->peer_cert = NULL;
3696 return ret;
3697 }
3698
3699 p += cert_len;
3700 }
3701#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3702 /* Deserialize CRT digest from the end of the ticket. */
3703 if (2 > (size_t) (end - p)) {
3704 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3705 }
3706
3707 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3708 session->peer_cert_digest_len = (size_t) *p++;
3709
3710 if (session->peer_cert_digest_len != 0) {
3711 const mbedtls_md_info_t *md_info =
3712 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3713 if (md_info == NULL) {
3714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3715 }
3716 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3718 }
3719
3720 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3722 }
3723
3724 session->peer_cert_digest =
3725 mbedtls_calloc(1, session->peer_cert_digest_len);
3726 if (session->peer_cert_digest == NULL) {
3727 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3728 }
3729
3730 memcpy(session->peer_cert_digest, p,
3731 session->peer_cert_digest_len);
3732 p += session->peer_cert_digest_len;
3733 }
3734#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3735#endif /* MBEDTLS_X509_CRT_PARSE_C */
3736
3737 /*
3738 * Session ticket and associated data
3739 */
3740#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3741#if defined(MBEDTLS_SSL_CLI_C)
3742 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3743 if (3 > (size_t) (end - p)) {
3744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3745 }
3746
3747 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3748 p += 3;
3749
3750 if (session->ticket_len != 0) {
3751 if (session->ticket_len > (size_t) (end - p)) {
3752 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3753 }
3754
3755 session->ticket = mbedtls_calloc(1, session->ticket_len);
3756 if (session->ticket == NULL) {
3757 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3758 }
3759
3760 memcpy(session->ticket, p, session->ticket_len);
3761 p += session->ticket_len;
3762 }
3763
3764 if (4 > (size_t) (end - p)) {
3765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3766 }
3767
3768 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3769 p += 4;
3770 }
3771#endif /* MBEDTLS_SSL_CLI_C */
3772#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3773 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3774 if (8 > (size_t) (end - p)) {
3775 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3776 }
3777 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3778 p += 8;
3779 }
3780#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3781#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3782
3783 /*
3784 * Misc extension-related info
3785 */
3786#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3787 if (1 > (size_t) (end - p)) {
3788 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3789 }
3790
3791 session->mfl_code = *p++;
3792#endif
3793
3794#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3795 if (1 > (size_t) (end - p)) {
3796 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3797 }
3798
3799 session->encrypt_then_mac = *p++;
3800#endif
3801
3802 /* Done, should have consumed entire buffer */
3803 if (p != end) {
3804 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3805 }
3806
3807 return 0;
3808}
3809
3810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3811
3812#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3813/* Serialization of TLS 1.3 sessions:
3814 *
David Horstmanncb01b362024-02-29 17:31:46 +00003815 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003816 */
3817#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3818MBEDTLS_CHECK_RETURN_CRITICAL
3819static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3820 unsigned char *buf,
3821 size_t buf_len,
3822 size_t *olen)
3823{
3824 unsigned char *p = buf;
3825#if defined(MBEDTLS_SSL_CLI_C) && \
3826 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3827 size_t hostname_len = (session->hostname == NULL) ?
3828 0 : strlen(session->hostname) + 1;
3829#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003830
3831#if defined(MBEDTLS_SSL_SRV_C) && \
3832 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003833 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003834 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003835#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003836 size_t needed = 4 /* ticket_age_add */
3837 + 1 /* ticket_flags */
3838 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003839
David Horstmanne59f9702024-02-29 16:08:57 +00003840 *olen = 0;
3841
3842 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3844 }
3845 needed += session->resumption_key_len; /* resumption_key */
3846
3847#if defined(MBEDTLS_SSL_EARLY_DATA)
3848 needed += 4; /* max_early_data_size */
3849#endif
3850#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3851 needed += 2; /* record_size_limit */
3852#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3853
3854#if defined(MBEDTLS_HAVE_TIME)
3855 needed += 8; /* ticket_creation_time or ticket_reception_time */
3856#endif
3857
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003858#if defined(MBEDTLS_SSL_SRV_C)
3859 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3860#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003861 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003862 + alpn_len; /* alpn */
3863#endif
3864 }
3865#endif /* MBEDTLS_SSL_SRV_C */
3866
David Horstmanne59f9702024-02-29 16:08:57 +00003867#if defined(MBEDTLS_SSL_CLI_C)
3868 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3869#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3870 needed += 2 /* hostname_len */
3871 + hostname_len; /* hostname */
3872#endif
3873
3874 needed += 4 /* ticket_lifetime */
3875 + 2; /* ticket_len */
3876
3877 /* Check size_t overflow */
3878 if (session->ticket_len > SIZE_MAX - needed) {
3879 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3880 }
3881
3882 needed += session->ticket_len; /* ticket */
3883 }
3884#endif /* MBEDTLS_SSL_CLI_C */
3885
3886 *olen = needed;
3887 if (needed > buf_len) {
3888 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3889 }
3890
3891 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3892 p[4] = session->ticket_flags;
3893
3894 /* save resumption_key */
3895 p[5] = session->resumption_key_len;
3896 p += 6;
3897 memcpy(p, session->resumption_key, session->resumption_key_len);
3898 p += session->resumption_key_len;
3899
3900#if defined(MBEDTLS_SSL_EARLY_DATA)
3901 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3902 p += 4;
3903#endif
3904#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3905 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3906 p += 2;
3907#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3908
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003909#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003910 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003911#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003912 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3913 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003914#endif /* MBEDTLS_HAVE_TIME */
3915
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003916#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003917 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3918 p += 2;
3919
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003920 if (alpn_len > 0) {
3921 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003922 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003923 p += alpn_len;
3924 }
3925#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3926 }
3927#endif /* MBEDTLS_SSL_SRV_C */
3928
David Horstmanne59f9702024-02-29 16:08:57 +00003929#if defined(MBEDTLS_SSL_CLI_C)
3930 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3931#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3932 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3933 p += 2;
3934 if (hostname_len > 0) {
3935 /* save host name */
3936 memcpy(p, session->hostname, hostname_len);
3937 p += hostname_len;
3938 }
3939#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3940
3941#if defined(MBEDTLS_HAVE_TIME)
3942 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3943 p += 8;
3944#endif
3945 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3946 p += 4;
3947
3948 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3949 p += 2;
3950
3951 if (session->ticket != NULL && session->ticket_len > 0) {
3952 memcpy(p, session->ticket, session->ticket_len);
3953 p += session->ticket_len;
3954 }
3955 }
3956#endif /* MBEDTLS_SSL_CLI_C */
3957 return 0;
3958}
3959
3960MBEDTLS_CHECK_RETURN_CRITICAL
3961static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3962 const unsigned char *buf,
3963 size_t len)
3964{
3965 const unsigned char *p = buf;
3966 const unsigned char *end = buf + len;
3967
3968 if (end - p < 6) {
3969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3970 }
3971 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3972 session->ticket_flags = p[4];
3973
3974 /* load resumption_key */
3975 session->resumption_key_len = p[5];
3976 p += 6;
3977
3978 if (end - p < session->resumption_key_len) {
3979 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3980 }
3981
3982 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3983 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3984 }
3985 memcpy(session->resumption_key, p, session->resumption_key_len);
3986 p += session->resumption_key_len;
3987
3988#if defined(MBEDTLS_SSL_EARLY_DATA)
3989 if (end - p < 4) {
3990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3991 }
3992 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3993 p += 4;
3994#endif
3995#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3996 if (end - p < 2) {
3997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3998 }
3999 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
4000 p += 2;
4001#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4002
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004003#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00004004 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004005#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00004006 if (end - p < 8) {
4007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4008 }
4009 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
4010 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00004011#endif /* MBEDTLS_HAVE_TIME */
4012
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004013#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004014 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004015
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004016 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4018 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004019
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004020 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
4021 p += 2;
4022
4023 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004024 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4025 }
4026
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004027 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004028 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
4029 if (ret != 0) {
4030 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004031 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004032 p += alpn_len;
4033 }
4034#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
4035 }
4036#endif /* MBEDTLS_SSL_SRV_C */
4037
David Horstmanne59f9702024-02-29 16:08:57 +00004038#if defined(MBEDTLS_SSL_CLI_C)
4039 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4040#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4041 size_t hostname_len;
4042 /* load host name */
4043 if (end - p < 2) {
4044 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4045 }
4046 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
4047 p += 2;
4048
4049 if (end - p < (long int) hostname_len) {
4050 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4051 }
4052 if (hostname_len > 0) {
4053 session->hostname = mbedtls_calloc(1, hostname_len);
4054 if (session->hostname == NULL) {
4055 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4056 }
4057 memcpy(session->hostname, p, hostname_len);
4058 p += hostname_len;
4059 }
4060#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4061
4062#if defined(MBEDTLS_HAVE_TIME)
4063 if (end - p < 8) {
4064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4065 }
4066 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4067 p += 8;
4068#endif
4069 if (end - p < 4) {
4070 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4071 }
4072 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4073 p += 4;
4074
4075 if (end - p < 2) {
4076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4077 }
4078 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4079 p += 2;
4080
4081 if (end - p < (long int) session->ticket_len) {
4082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4083 }
4084 if (session->ticket_len > 0) {
4085 session->ticket = mbedtls_calloc(1, session->ticket_len);
4086 if (session->ticket == NULL) {
4087 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4088 }
4089 memcpy(session->ticket, p, session->ticket_len);
4090 p += session->ticket_len;
4091 }
4092 }
4093#endif /* MBEDTLS_SSL_CLI_C */
4094
4095 return 0;
4096
4097}
4098#else /* MBEDTLS_SSL_SESSION_TICKETS */
4099MBEDTLS_CHECK_RETURN_CRITICAL
4100static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4101 unsigned char *buf,
4102 size_t buf_len,
4103 size_t *olen)
4104{
4105 ((void) session);
4106 ((void) buf);
4107 ((void) buf_len);
4108 *olen = 0;
4109 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4110}
4111
4112static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004113 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004114 size_t buf_len)
4115{
4116 ((void) session);
4117 ((void) buf);
4118 ((void) buf_len);
4119 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4120}
4121#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4122#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4123
Paul Bakker5121ce52009-01-03 21:22:43 +00004124/*
Hanno Beckera835da52019-05-16 12:39:07 +01004125 * Define ticket header determining Mbed TLS version
4126 * and structure of the ticket.
4127 */
4128
Hanno Becker94ef3b32019-05-16 12:50:45 +01004129/*
Hanno Becker50b59662019-05-28 14:30:45 +01004130 * Define bitflag determining compile-time settings influencing
4131 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004132 */
4133
Hanno Becker50b59662019-05-28 14:30:45 +01004134#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004135#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004136#else
Hanno Becker3e088662019-05-29 11:10:18 +01004137#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004138#endif /* MBEDTLS_HAVE_TIME */
4139
4140#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004141#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004142#else
Hanno Becker3e088662019-05-29 11:10:18 +01004143#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004144#endif /* MBEDTLS_X509_CRT_PARSE_C */
4145
David Horstmann5c5a32f2024-02-13 17:53:35 +00004146#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004147#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004148#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004149#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004150#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4151
Hanno Becker94ef3b32019-05-16 12:50:45 +01004152#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004153#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004154#else
Hanno Becker3e088662019-05-29 11:10:18 +01004155#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004156#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4157
4158#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004159#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004160#else
Hanno Becker3e088662019-05-29 11:10:18 +01004161#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004162#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4163
Hanno Becker94ef3b32019-05-16 12:50:45 +01004164#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004165#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004166#else
Hanno Becker3e088662019-05-29 11:10:18 +01004167#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004168#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4169
Hanno Becker94ef3b32019-05-16 12:50:45 +01004170#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4171#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4172#else
4173#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4174#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4175
David Horstmann92b258b2024-02-13 17:23:34 +00004176#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4177#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4178#else
4179#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4180#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4181
4182#if defined(MBEDTLS_SSL_EARLY_DATA)
4183#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4184#else
4185#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4186#endif /* MBEDTLS_SSL_EARLY_DATA */
4187
4188#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4189#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4190#else
4191#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4192#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4193
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004194#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4195 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004196#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4197#else
4198#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4199#endif /* MBEDTLS_SSL_ALPN */
4200
Hanno Becker3e088662019-05-29 11:10:18 +01004201#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4202#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4203#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4204#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004205#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4206#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004207#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004208#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4209#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4210#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004211#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004212
Hanno Becker50b59662019-05-28 14:30:45 +01004213#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 ((uint16_t) ( \
4215 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4216 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4217 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4218 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4219 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4220 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004221 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004222 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4223 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004224 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4225 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4226 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4227 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004228 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004229 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004230 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004231
Chien Wong4e9683e2023-12-28 17:07:43 +08004232static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004233 MBEDTLS_VERSION_MAJOR,
4234 MBEDTLS_VERSION_MINOR,
4235 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4237 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004238};
Hanno Beckera835da52019-05-16 12:39:07 +01004239
4240/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004241 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004242 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004243 *
David Horstmanncb01b362024-02-29 17:31:46 +00004244 * TLS 1.2 session:
4245 *
4246 * struct {
4247 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4248 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4249 * uint32 ticket_lifetime;
4250 * #endif
4251 * } ClientOnlyData;
4252 *
4253 * struct {
4254 * #if defined(MBEDTLS_HAVE_TIME)
4255 * uint64 start_time;
4256 * #endif
4257 * uint8 session_id_len; // at most 32
4258 * opaque session_id[32];
4259 * opaque master[48]; // fixed length in the standard
4260 * uint32 verify_result;
4261 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4262 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4263 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004264 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004265 * opaque peer_cert_digest<0..2^8-1>
4266 * #endif
4267 * select (endpoint) {
4268 * case client: ClientOnlyData;
4269 * case server: uint64 ticket_creation_time;
4270 * };
4271 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4272 * uint8 mfl_code; // up to 255 according to standard
4273 * #endif
4274 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4275 * uint8 encrypt_then_mac; // 0 or 1
4276 * #endif
4277 * } serialized_session_tls12;
4278 *
4279 *
4280 * TLS 1.3 Session:
4281 *
4282 * struct {
4283 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4284 * opaque hostname<0..2^16-1>;
4285 * #endif
4286 * #if defined(MBEDTLS_HAVE_TIME)
4287 * uint64 ticket_reception_time;
4288 * #endif
4289 * uint32 ticket_lifetime;
4290 * opaque ticket<1..2^16-1>;
4291 * } ClientOnlyData;
4292 *
4293 * struct {
4294 * uint32 ticket_age_add;
4295 * uint8 ticket_flags;
4296 * opaque resumption_key<0..255>;
4297 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4298 * uint32 max_early_data_size;
4299 * #endif
4300 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4301 * uint16 record_size_limit;
4302 * #endif
4303 * select ( endpoint ) {
4304 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004305 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004306 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004307 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004308 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004309 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004310 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004311 * #endif
4312 * };
4313 * } serialized_session_tls13;
4314 *
4315 *
4316 * SSL session:
4317 *
4318 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004319 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004320 * opaque mbedtls_version[3]; // library version: major, minor, patch
4321 * opaque session_format[2]; // library-version specific 16-bit field
4322 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004323 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004324 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004325 * Note: When updating the format, remember to keep
4326 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004327 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004328 * // In this version, `session_format` determines
4329 * // the setting of those compile-time
4330 * // configuration options which influence
4331 * // the structure of mbedtls_ssl_session.
4332 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004333 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004334 * // - TLS 1.2 (0x0303)
4335 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004336 * uint8_t endpoint;
4337 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004338 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004339 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004340 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004341 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004342 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004343 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004344 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004345 *
4346 * };
4347 *
4348 * } serialized_session;
4349 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004350 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004351
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004352MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004353static int ssl_session_save(const mbedtls_ssl_session *session,
4354 unsigned char omit_header,
4355 unsigned char *buf,
4356 size_t buf_len,
4357 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004358{
4359 unsigned char *p = buf;
4360 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004361 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004362#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4363 size_t out_len;
4364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4365#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (session == NULL) {
4367 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4368 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004371 /*
4372 * Add Mbed TLS version identifier
4373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 if (used <= buf_len) {
4377 memcpy(p, ssl_serialized_session_header,
4378 sizeof(ssl_serialized_session_header));
4379 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004380 }
4381 }
4382
4383 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004384 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004385 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004386 used += 1 /* TLS version */
4387 + 1 /* endpoint */
4388 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 if (used <= buf_len) {
4390 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004391 *p++ = session->endpoint;
4392 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4393 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004394 }
4395
4396 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004397 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004399#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 case MBEDTLS_SSL_VERSION_TLS1_2:
4401 used += ssl_tls12_session_save(session, p, remaining_len);
4402 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004403#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4404
Jerry Yu251a12e2022-07-13 15:15:48 +08004405#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 case MBEDTLS_SSL_VERSION_TLS1_3:
4407 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4408 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4409 return ret;
4410 }
4411 used += out_len;
4412 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004413#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 default:
4416 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004417 }
4418
4419 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (used > buf_len) {
4421 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4422 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004423
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004425}
4426
4427/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004428 * Public wrapper for ssl_session_save()
4429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004430int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4431 unsigned char *buf,
4432 size_t buf_len,
4433 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004434{
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004436}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004437
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004438/*
4439 * Deserialize session, see mbedtls_ssl_session_save() for format.
4440 *
4441 * This internal version is wrapped by a public function that cleans up in
4442 * case of error, and has an extra option omit_header.
4443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004445static int ssl_session_load(mbedtls_ssl_session *session,
4446 unsigned char omit_header,
4447 const unsigned char *buf,
4448 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004449{
4450 const unsigned char *p = buf;
4451 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004452 size_t remaining_len;
4453
4454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (session == NULL) {
4456 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4457 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004460 /*
4461 * Check Mbed TLS version identifier
4462 */
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4465 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004466 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004467
4468 if (memcmp(p, ssl_serialized_session_header,
4469 sizeof(ssl_serialized_session_header)) != 0) {
4470 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4471 }
4472 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004473 }
4474
4475 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004476 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004477 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004478 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4480 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004481 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004482 session->endpoint = *p++;
4483 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4484 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004485
4486 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004487 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 case MBEDTLS_SSL_VERSION_TLS1_2:
4491 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004492#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4493
Jerry Yu438ddd82022-07-07 06:55:50 +00004494#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 case MBEDTLS_SSL_VERSION_TLS1_3:
4496 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004497#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 default:
4500 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004501 }
4502}
4503
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004504/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004505 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004507int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4508 const unsigned char *buf,
4509 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004510{
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (ret != 0) {
4514 mbedtls_ssl_session_free(session);
4515 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004518}
4519
4520/*
Paul Bakker1961b702013-01-25 14:49:24 +01004521 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004522 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004523MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004524static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004525{
4526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4527
Ronald Cron66dbf912022-02-02 15:33:46 +01004528 /*
4529 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004530 * that were written into the output buffer by the previous handshake step,
4531 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004532 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4533 * We proceed to the next handshake step only when all data from the
4534 * previous one have been sent to the peer, thus we make sure that this is
4535 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4536 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4537 * we have to wait before to go ahead.
4538 * In the case of TLS 1.3, handshake step handlers do not send data to the
4539 * peer. Data are only sent here and through
4540 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004541 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4544 return ret;
4545 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004546
4547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4549 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4550 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4551 return ret;
4552 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004553 }
4554#endif /* MBEDTLS_SSL_PROTO_DTLS */
4555
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004557}
4558
Gilles Peskine449bd832023-01-11 14:50:10 +01004559int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004560{
Hanno Becker41934dd2021-08-07 19:13:43 +01004561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004562
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004564 ssl->conf == NULL ||
4565 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004568 }
4569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ret = ssl_prepare_handshake_step(ssl);
4571 if (ret != 0) {
4572 return ret;
4573 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 ret = mbedtls_ssl_handle_pending_alert(ssl);
4576 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004577 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 }
Jerry Yue7047812021-09-13 19:26:39 +08004579
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004580 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4581 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4582 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4586 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004587 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004590 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine49f179d2025-03-07 15:09:32 +01004591 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004592 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004593 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004594
Ronald Cron9f0fba32022-02-10 16:45:15 +01004595 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004597 break;
4598
4599 default:
4600#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4602 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4603 } else {
4604 ret = mbedtls_ssl_handshake_client_step(ssl);
4605 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004606#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004607 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004608#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004610#endif
4611 }
Jerry Yub9930e72021-08-06 17:11:51 +08004612 }
Ronald Cron6291b232023-03-08 15:51:25 +01004613#endif /* MBEDTLS_SSL_CLI_C */
4614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004617#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4618 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004620 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 ret = mbedtls_ssl_handshake_server_step(ssl);
4622 }
Ronald Cron6291b232023-03-08 15:51:25 +01004623#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4624 ret = mbedtls_ssl_handshake_server_step(ssl);
4625#else
4626 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004627#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004628 }
4629#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004632 /* handshake_step return error. And it is same
4633 * with alert_reason.
4634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (ssl->send_alert) {
4636 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004637 goto cleanup;
4638 }
4639 }
4640
4641cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004643}
4644
4645/*
4646 * Perform the SSL handshake
4647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004648int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004649{
4650 int ret = 0;
4651
Hanno Beckera817ea42020-10-20 15:20:23 +01004652 /* Sanity checks */
4653
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (ssl == NULL || ssl->conf == NULL) {
4655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4656 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004657
Hanno Beckera817ea42020-10-20 15:20:23 +01004658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4660 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4661 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4662 "mbedtls_ssl_set_timer_cb() for DTLS"));
4663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004664 }
4665#endif /* MBEDTLS_SSL_PROTO_DTLS */
4666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004668
Hanno Beckera817ea42020-10-20 15:20:23 +01004669 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4671 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004674 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 }
Paul Bakker1961b702013-01-25 14:49:24 +01004676 }
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004681}
4682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683#if defined(MBEDTLS_SSL_RENEGOTIATION)
4684#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004685/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004686 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004687 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004689static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004690{
Janos Follath865b3eb2019-12-16 11:46:15 +00004691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004694
4695 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4697 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4700 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4701 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004702 }
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004709
4710/*
4711 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 * - any side: calling mbedtls_ssl_renegotiate(),
4713 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4714 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004715 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004716 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004719int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004720{
Janos Follath865b3eb2019-12-16 11:46:15 +00004721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004724
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 if ((ret = ssl_handshake_init(ssl)) != 0) {
4726 return ret;
4727 }
Paul Bakker48916f92012-09-16 19:57:18 +00004728
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004729 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4730 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4733 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4734 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004735 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004737 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004739 }
4740#endif
4741
Gilles Peskine49f179d2025-03-07 15:09:32 +01004742 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_REQUEST);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004743 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004744
Gilles Peskine449bd832023-01-11 14:50:10 +01004745 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4746 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4747 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004748 }
4749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004753}
4754
4755/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004756 * Renegotiate current connection on client,
4757 * or request renegotiation on server
4758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004759int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004760{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (ssl == NULL || ssl->conf == NULL) {
4764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4765 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004768 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4770 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4771 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4772 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004775
4776 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 if (ssl->out_left != 0) {
4778 return mbedtls_ssl_flush_output(ssl);
4779 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004783#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004786 /*
4787 * On client, either start the renegotiation process or,
4788 * if already in progress, continue the handshake
4789 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4791 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4792 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004793 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004794
4795 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4796 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4797 return ret;
4798 }
4799 } else {
4800 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4801 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4802 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004803 }
4804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004812{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004813 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4814
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004816 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004818
Valerio Setti6f0441d2023-07-03 12:11:36 +02004819#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004820#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (ssl->handshake->group_list_heap_allocated) {
4822 mbedtls_free((void *) handshake->group_list);
4823 }
Brett Warrene0edc842021-08-17 09:53:13 +01004824 handshake->group_list = NULL;
4825#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004826#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004827
Ronald Crone68ab4f2022-10-05 12:46:29 +02004828#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004829#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (ssl->handshake->sig_algs_heap_allocated) {
4831 mbedtls_free((void *) handshake->sig_algs);
4832 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004833 handshake->sig_algs = NULL;
4834#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004835#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (ssl->handshake->certificate_request_context) {
4837 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004838 }
4839#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004840#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004841
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004842#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4844 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004845 handshake->async_in_progress = 0;
4846 }
4847#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4848
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004849#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004850#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004852#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004853 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004854#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004855#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004856#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004857#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004859#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004860 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004861#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004862#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004866#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004867#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004868 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004870#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004871
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004872#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004875 /*
4876 * Opaque keys are not stored in the handshake's data and it's the user
4877 * responsibility to destroy them. Clear ones, instead, are created by
4878 * the TLS library and should be destroyed at the same level
4879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4881 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004882 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004883 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4884#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004886#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004887#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004889 handshake->ecjpake_cache = NULL;
4890 handshake->ecjpake_cache_len = 0;
4891#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004892#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004893
Valerio Setti7aeec542023-07-05 18:57:21 +02004894#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004895 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004896 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004897 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004899#endif
4900
Ronald Cron73fe8df2022-10-05 14:31:43 +02004901#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004902#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004904 /* The maintenance of the external PSK key slot is the
4905 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (ssl->handshake->psk_opaque_is_internal) {
4907 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004908 ssl->handshake->psk_opaque_is_internal = 0;
4909 }
4910 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4911 }
Neil Armstronge952a302022-05-03 10:22:14 +02004912#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004914 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004915 }
Neil Armstronge952a302022-05-03 10:22:14 +02004916#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004917#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4920 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004921 /*
4922 * Free only the linked list wrapper, not the keys themselves
4923 * since the belong to the SNI callback
4924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004926#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004927
Gilles Peskineeccd8882020-03-10 12:19:08 +01004928#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4930 if (handshake->ecrs_peer_cert != NULL) {
4931 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4932 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004933 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004934#endif
4935
Hanno Becker75173122019-02-06 16:18:31 +00004936#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4937 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004939#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4940
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004941#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004942 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4943 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004944#endif /* MBEDTLS_SSL_CLI_C &&
4945 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004946
4947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 mbedtls_ssl_flight_free(handshake->flight);
4949 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004950#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004951
Valerio Settiea59c432023-07-25 11:14:03 +02004952#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004953 if (handshake->xxdh_psa_privkey_is_external == 0) {
4954 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 }
Valerio Settiea59c432023-07-25 11:14:03 +02004956#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004957
Ronald Cron6f135e12021-12-08 16:57:54 +01004958#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 mbedtls_ssl_transform_free(handshake->transform_handshake);
4960 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004961#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4963 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004964#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004965#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004966
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004967
4968#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4969 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4970 * processes datagrams and the fact that a datagram is allowed to have
4971 * several records in it, it is possible that the I/O buffers are not
4972 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4974 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004975#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004976
Jerry Yuba9c7272021-10-30 11:54:10 +08004977 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 mbedtls_platform_zeroize(handshake,
4979 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004980}
4981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004983{
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004985 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004990#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004991
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004992#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004993#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4994 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004996#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004998#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004999
Waleed Elmelegy2824a202024-02-23 17:51:47 +00005000#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
5001 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00005002 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00005003#endif
5004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00005006}
5007
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005008#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005009
5010#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5011#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
5012#else
5013#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
5014#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5015
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005016#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005017
5018#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5019#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
5020#else
5021#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
5022#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5023
5024#if defined(MBEDTLS_SSL_ALPN)
5025#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
5026#else
5027#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
5028#endif /* MBEDTLS_SSL_ALPN */
5029
5030#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
5031#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
5032#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
5033#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
5034
5035#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 ((uint32_t) ( \
5037 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
5038 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
5039 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
5040 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
5041 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
5042 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
5043 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
5044 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005045
Chien Wong4e9683e2023-12-28 17:07:43 +08005046static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005047 MBEDTLS_VERSION_MAJOR,
5048 MBEDTLS_VERSION_MINOR,
5049 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5051 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5052 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5053 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5054 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005055};
5056
Paul Bakker5121ce52009-01-03 21:22:43 +00005057/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005058 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005059 *
5060 * The format of the serialized data is:
5061 * (in the presentation language of TLS, RFC 8446 section 3)
5062 *
5063 * // header
5064 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005065 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005066 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005067 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005068 * Note: When updating the format, remember to keep these
5069 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005070 *
5071 * // session sub-structure
5072 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5073 * // transform sub-structure
5074 * uint8 random[64]; // ServerHello.random+ClientHello.random
5075 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5076 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5077 * // fields from ssl_context
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005078 * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005079 * uint64 in_window_top; // DTLS: last validated record seq_num
5080 * uint64 in_window; // DTLS: bitmask for replay protection
5081 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5082 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5083 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5084 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5085 *
5086 * Note that many fields of the ssl_context or sub-structures are not
5087 * serialized, as they fall in one of the following categories:
5088 *
5089 * 1. forced value (eg in_left must be 0)
5090 * 2. pointer to dynamically-allocated memory (eg session, transform)
5091 * 3. value can be re-derived from other data (eg session keys from MS)
5092 * 4. value was temporary (eg content of input buffer)
5093 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005095int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5096 unsigned char *buf,
5097 size_t buf_len,
5098 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005099{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005100 unsigned char *p = buf;
5101 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005102 size_t session_len;
5103 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005104
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005105 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005106 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5107 * this function's documentation.
5108 *
5109 * These are due to assumptions/limitations in the implementation. Some of
5110 * them are likely to stay (no handshake in progress) some might go away
5111 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005112 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005113 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5115 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5116 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 if (ssl->handshake != NULL) {
5119 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005121 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005122 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005123 if (ssl->transform == NULL || ssl->session == NULL) {
5124 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5125 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005126 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005127 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 if (mbedtls_ssl_check_pending(ssl) != 0) {
5129 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005131 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (ssl->out_left != 0) {
5133 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005135 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005136 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5138 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5139 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005140 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005141 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5143 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005145 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005146 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5148 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005150 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005151 /* Renegotiation must not be enabled */
5152#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5154 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005156 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005157#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005158
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005159 /*
5160 * Version and format identifier
5161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (used <= buf_len) {
5165 memcpy(p, ssl_serialized_context_header,
5166 sizeof(ssl_serialized_context_header));
5167 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005168 }
5169
5170 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005171 * Session (length + data)
5172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5174 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5175 return ret;
5176 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005177
5178 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (used <= buf_len) {
5180 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005181 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005182
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 ret = ssl_session_save(ssl->session, 1,
5184 p, session_len, &session_len);
5185 if (ret != 0) {
5186 return ret;
5187 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005188
5189 p += session_len;
5190 }
5191
5192 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005193 * Transform
5194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 used += sizeof(ssl->transform->randbytes);
5196 if (used <= buf_len) {
5197 memcpy(p, ssl->transform->randbytes,
5198 sizeof(ssl->transform->randbytes));
5199 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005200 }
5201
5202#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005203 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005205 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005207 p += ssl->transform->in_cid_len;
5208
5209 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005211 p += ssl->transform->out_cid_len;
5212 }
5213#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5214
5215 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005216 * Saved fields from top-level ssl_context structure
5217 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005218 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (used <= buf_len) {
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005220 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005221 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005222 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005223
5224#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5225 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 if (used <= buf_len) {
5227 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005228 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005231 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005232 }
5233#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5234
5235#if defined(MBEDTLS_SSL_PROTO_DTLS)
5236 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005238 *p++ = ssl->disable_datagram_packing;
5239 }
5240#endif /* MBEDTLS_SSL_PROTO_DTLS */
5241
Jerry Yuae0b2e22021-10-08 15:21:19 +08005242 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 if (used <= buf_len) {
5244 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005245 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005246 }
5247
5248#if defined(MBEDTLS_SSL_PROTO_DTLS)
5249 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (used <= buf_len) {
5251 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005252 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005253 }
5254#endif /* MBEDTLS_SSL_PROTO_DTLS */
5255
5256#if defined(MBEDTLS_SSL_ALPN)
5257 {
5258 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005260 : 0;
5261
5262 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005264 *p++ = alpn_len;
5265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (ssl->alpn_chosen != NULL) {
5267 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005268 p += alpn_len;
5269 }
5270 }
5271 }
5272#endif /* MBEDTLS_SSL_ALPN */
5273
5274 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005275 * Done
5276 */
5277 *olen = used;
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (used > buf_len) {
5280 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5281 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005282
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005286}
5287
5288/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005289 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005290 *
5291 * This internal version is wrapped by a public function that cleans up in
5292 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005293 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005294MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005295static int ssl_context_load(mbedtls_ssl_context *ssl,
5296 const unsigned char *buf,
5297 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005298{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005299 const unsigned char *p = buf;
5300 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005301 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005303#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005304 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005305#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005306
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005307 /*
5308 * The context should have been freshly setup or reset.
5309 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005310 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005311 * renegotiating, or if the user mistakenly loaded a session first.)
5312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5314 ssl->session != NULL) {
5315 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005316 }
5317
5318 /*
5319 * We can't check that the config matches the initial one, but we can at
5320 * least check it matches the requirements for serializing.
5321 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005322 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005323#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005324 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005325#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005326 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5327 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5328 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5329 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005331 }
5332
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005334
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005335 /*
5336 * Check version identifier
5337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005340 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005341
5342 if (memcmp(p, ssl_serialized_context_header,
5343 sizeof(ssl_serialized_context_header)) != 0) {
5344 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5345 }
5346 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005347
5348 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005349 * Session
5350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 if ((size_t) (end - p) < 4) {
5352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5353 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005354
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005355 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005356 p += 4;
5357
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005358 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005359 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005360 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005361 ssl->session_in = ssl->session;
5362 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005363 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 if ((size_t) (end - p) < session_len) {
5366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5367 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 ret = ssl_session_load(ssl->session, 1, p, session_len);
5370 if (ret != 0) {
5371 mbedtls_ssl_session_free(ssl->session);
5372 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005373 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005374
5375 p += session_len;
5376
5377 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005378 * Transform
5379 */
5380
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005381 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005382 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005384 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005385 ssl->transform_in = ssl->transform;
5386 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005387 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005388#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005389
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5392 if (prf_func == NULL) {
5393 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5394 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005395
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005396 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5399 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 ret = ssl_tls12_populate_transform(ssl->transform,
5402 ssl->session->ciphersuite,
5403 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005404#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005406#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 prf_func,
5408 p, /* currently pointing to randbytes */
5409 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5410 ssl->conf->endpoint,
5411 ssl);
5412 if (ret != 0) {
5413 return ret;
5414 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005417
5418#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5419 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if ((size_t) (end - p) < 1) {
5421 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5422 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005423
5424 ssl->transform->in_cid_len = *p++;
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5427 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5428 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005429
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005431 p += ssl->transform->in_cid_len;
5432
5433 ssl->transform->out_cid_len = *p++;
5434
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5436 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5437 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005440 p += ssl->transform->out_cid_len;
5441#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5442
5443 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005444 * Saved fields from top-level ssl_context structure
5445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 if ((size_t) (end - p) < 4) {
5447 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5448 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005449
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005450 ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005451 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005452
5453#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 if ((size_t) (end - p) < 16) {
5455 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5456 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005457
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005458 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005459 p += 8;
5460
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005461 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005462 p += 8;
5463#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5464
5465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if ((size_t) (end - p) < 1) {
5467 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5468 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005469
5470 ssl->disable_datagram_packing = *p++;
5471#endif /* MBEDTLS_SSL_PROTO_DTLS */
5472
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5474 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5475 }
5476 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5477 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005478
5479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 if ((size_t) (end - p) < 2) {
5481 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5482 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005483
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005484 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005485 p += 2;
5486#endif /* MBEDTLS_SSL_PROTO_DTLS */
5487
5488#if defined(MBEDTLS_SSL_ALPN)
5489 {
5490 uint8_t alpn_len;
5491 const char **cur;
5492
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 if ((size_t) (end - p) < 1) {
5494 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5495 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005496
5497 alpn_len = *p++;
5498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005500 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5502 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005503 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005504 ssl->alpn_chosen = *cur;
5505 break;
5506 }
5507 }
5508 }
5509
5510 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5512 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5513 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005514
5515 p += alpn_len;
5516 }
5517#endif /* MBEDTLS_SSL_ALPN */
5518
5519 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005520 * Forced fields from top-level ssl_context structure
5521 *
5522 * Most of them already set to the correct value by mbedtls_ssl_init() and
5523 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5524 */
Gilles Peskine49f179d2025-03-07 15:09:32 +01005525 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Glenn Strauss60bfe602022-03-14 19:04:24 -04005526 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005527
Hanno Becker361b10d2019-08-30 10:42:49 +01005528 /* Adjust pointers for header fields of outgoing records to
5529 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005531
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005532#if defined(MBEDTLS_SSL_PROTO_DTLS)
5533 ssl->in_epoch = 1;
5534#endif
5535
5536 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5537 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005538 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5539 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 if (ssl->handshake != NULL) {
5541 mbedtls_ssl_handshake_free(ssl);
5542 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005543 ssl->handshake = NULL;
5544 }
5545
5546 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005547 * Done - should have consumed entire buffer
5548 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (p != end) {
5550 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5551 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005552
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005554}
5555
5556/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005557 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005559int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5560 const unsigned char *buf,
5561 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005562{
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 if (ret != 0) {
5566 mbedtls_ssl_free(context);
5567 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005570}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005571#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005572
5573/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005574 * Free an SSL context
5575 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005576void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005577{
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005579 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005583
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005585#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5586 size_t out_buf_len = ssl->out_buf_len;
5587#else
5588 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5589#endif
5590
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005591 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005592 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 }
5594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005596#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5597 size_t in_buf_len = ssl->in_buf_len;
5598#else
5599 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5600#endif
5601
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005602 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005603 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005604 }
5605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 if (ssl->transform) {
5607 mbedtls_ssl_transform_free(ssl->transform);
5608 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005609 }
5610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 if (ssl->handshake) {
5612 mbedtls_ssl_handshake_free(ssl);
5613 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005614
5615#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5617 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005618#endif
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 mbedtls_ssl_session_free(ssl->session_negotiate);
5621 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005622 }
5623
Ronald Cron6f135e12021-12-08 16:57:54 +01005624#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 mbedtls_ssl_transform_free(ssl->transform_application);
5626 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005627#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005628
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 if (ssl->session) {
5630 mbedtls_ssl_session_free(ssl->session);
5631 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005632 }
5633
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005634#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskinee61852e2025-02-12 23:28:48 +01005635 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02005636#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005637
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005638#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005640#endif
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005643
Paul Bakker86f04f42013-02-14 11:20:09 +01005644 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005646}
5647
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005648/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005649 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005651void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005652{
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005654}
5655
Gilles Peskineae270bf2021-06-02 00:05:29 +02005656/* The selection should be the same as mbedtls_x509_crt_profile_default in
5657 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005658 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005659 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005660 * about this list.
5661 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005662static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005663#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005664 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005665#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005666#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005667 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005668#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005669#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005670 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005671#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005672#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005673 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005674#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005675#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005676 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005677#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005678#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005679 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005680#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005681#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005682 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005683#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005684#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005685 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005686#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005687#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005688 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5689 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5690 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5691 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5692 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5693#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005694 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005695};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005696
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005697static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005698 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5699 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5700 0
5701};
5702
Ronald Crone68ab4f2022-10-05 12:46:29 +02005703#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005704
Jerry Yu909df7b2022-01-22 11:56:27 +08005705/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005706 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005707 * rules SHOULD be upheld.
5708 * - No duplicate entries.
5709 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005710 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005711 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005712 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005713static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005714
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005715#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005716 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005717 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005718 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005719 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5720#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005721
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005722#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005723 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005724 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005725 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005726 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5727#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005728
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005729#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005730 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005731 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005732 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005733 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5734#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005735
Yanray Wang1136fad2023-11-22 16:54:31 +08005736#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005737 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005738#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005739
Yanray Wang1136fad2023-11-22 16:54:31 +08005740#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005741 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005742#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005743
Yanray Wang1136fad2023-11-22 16:54:31 +08005744#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005745 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005746#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005747
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005748#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005749 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005750#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005751
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005752#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005753 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005754#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005755
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005756#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005757 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005758#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005759
Gabor Mezei15b95a62022-05-09 16:37:58 +02005760 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005761};
5762
5763/* NOTICE: see above */
5764#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Deomid rojer Ryabkove3aaf822024-03-18 12:32:30 +00005765static const uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005766
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005767#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005768#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005770#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005771#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5772 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005773#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005774#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005776#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005777#endif /* MBEDTLS_MD_CAN_SHA512 */
5778
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005779#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005780#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005782#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005783#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5784 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005785#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005786#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005788#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005789#endif /* MBEDTLS_MD_CAN_SHA384 */
5790
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005791#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005792#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005794#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005795#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5796 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005797#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005798#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005800#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005801#endif /* MBEDTLS_MD_CAN_SHA256 */
5802
Gabor Mezei15b95a62022-05-09 16:37:58 +02005803 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005804};
5805#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005806
Jerry Yu909df7b2022-01-22 11:56:27 +08005807/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005808static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005809
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005810#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005811 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005812 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005813 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005814 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5815#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005816
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005817#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005818 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005819 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005820 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005821 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5822#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005823
Gabor Mezei15b95a62022-05-09 16:37:58 +02005824 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005825};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005826
Jerry Yu909df7b2022-01-22 11:56:27 +08005827/* NOTICE: see above */
5828#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Deomid rojer Ryabkovfb5e2e52024-03-20 00:43:34 +00005829static const uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005830
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005831#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005832#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005834#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005835#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005836
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005837#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005838#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005839 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005840#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005841#endif /* MBEDTLS_MD_CAN_SHA384 */
5842
Gabor Mezei15b95a62022-05-09 16:37:58 +02005843 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005844};
Jerry Yu909df7b2022-01-22 11:56:27 +08005845#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5846
Ronald Crone68ab4f2022-10-05 12:46:29 +02005847#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005848
Chien Wong4e9683e2023-12-28 17:07:43 +08005849static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005850#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005851 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005852#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005853#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005854 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005855#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005856 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005857};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005858
Ronald Crone68ab4f2022-10-05 12:46:29 +02005859#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005860/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005861 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005862MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005863static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005864{
5865 size_t i, j;
5866 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005867
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5869 for (j = 0; j < i; j++) {
5870 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005871 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 }
5873 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5874 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5875 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005876 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005877 }
5878 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005880}
5881
Ronald Crone68ab4f2022-10-05 12:46:29 +02005882#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005883
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005884/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005885 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005887int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5888 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005889{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005890#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005892#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005893
Ronald Crone68ab4f2022-10-05 12:46:29 +02005894#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5896 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5897 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005898 }
5899
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5901 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5902 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005903 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005904
5905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5907 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5908 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005909 }
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5912 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5913 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005914 }
5915#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005916#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005917
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005918 /* Use the functions here so that they are covered in tests,
5919 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 mbedtls_ssl_conf_endpoint(conf, endpoint);
5921 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005922
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005923 /*
5924 * Things that are common to all presets
5925 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005926#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005928 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5929#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005930 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005931#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005932 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5933 * handling is disabled by default in Mbed TLS 3.6.x for backward
5934 * compatibility with client applications developed using Mbed TLS 3.5
5935 * or earlier with the default configuration.
5936 *
5937 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5938 * disabled, and a Mbed TLS client with the default configuration would
5939 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5940 * server.
5941 *
5942 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5943 * an Mbed TLS client with the default configuration establishes a
5944 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5945 * following the handshake the TLS 1.3 server sends NewSessionTicket
5946 * messages and the Mbed TLS client processes them, this results in
5947 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5948 * mbedtls_ssl_handshake(), ...) to eventually return an
5949 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5950 * (see the documentation of mbedtls_ssl_read() for more information on
5951 * that error code). Applications unaware of that TLS 1.3 specific non
5952 * fatal error code are then failing.
5953 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005954 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5955 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005956#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005957#endif
5958 }
5959#endif
5960
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005961#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5962 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5963#endif
5964
5965#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5966 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5967#endif
5968
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005969#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005970 conf->f_cookie_write = ssl_cookie_write_dummy;
5971 conf->f_cookie_check = ssl_cookie_check_dummy;
5972#endif
5973
5974#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5975 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5976#endif
5977
Janos Follath088ce432017-04-10 12:42:31 +01005978#if defined(MBEDTLS_SSL_SRV_C)
5979 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005980 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005981#endif
5982
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005983#if defined(MBEDTLS_SSL_PROTO_DTLS)
5984 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5985 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5986#endif
5987
5988#if defined(MBEDTLS_SSL_RENEGOTIATION)
5989 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 memset(conf->renego_period, 0x00, 2);
5991 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005992#endif
5993
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005994#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005996 const unsigned char dhm_p[] =
5997 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5998 const unsigned char dhm_g[] =
5999 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01006000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6002 dhm_p, sizeof(dhm_p),
6003 dhm_g, sizeof(dhm_g))) != 0) {
6004 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01006005 }
6006 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006007#endif
6008
Ronald Cron6f135e12021-12-08 16:57:54 +01006009#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006010
6011#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08006012 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006013#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08006014 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006015#endif
6016#endif /* MBEDTLS_SSL_EARLY_DATA */
6017
Jerry Yud0766ec2022-09-22 10:46:57 +08006018#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08006019 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08006021#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01006022 /*
6023 * Allow all TLS 1.3 key exchange modes by default.
6024 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00006025 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01006026#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006029#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00006030 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00006031 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006032#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00006034#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00006036#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01006037 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6038 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006039#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
6040 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6041 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6042#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
6043 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6044 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6045#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006047#endif
6048 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006049
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006050 /*
6051 * Preset-specific defaults
6052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006054 /*
6055 * NSA Suite B
6056 */
6057 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006058
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006059 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006060
6061#if defined(MBEDTLS_X509_CRT_PARSE_C)
6062 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006063#endif
6064
Ronald Crone68ab4f2022-10-05 12:46:29 +02006065#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006066#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006068 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006070#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006072#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006073
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006074#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006075 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006076#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006077 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006078 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006079
6080 /*
6081 * Default
6082 */
6083 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006084
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006085 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006086
6087#if defined(MBEDTLS_X509_CRT_PARSE_C)
6088 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6089#endif
6090
Ronald Crone68ab4f2022-10-05 12:46:29 +02006091#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006094 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006096#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006098#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006099
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006100#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006101 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006102#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006103 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006104
6105#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6106 conf->dhm_min_bitlen = 1024;
6107#endif
6108 }
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006111}
6112
6113/*
6114 * Free mbedtls_ssl_config
6115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006116void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006117{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006118 if (conf == NULL) {
6119 return;
6120 }
6121
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006122#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 mbedtls_mpi_free(&conf->dhm_P);
6124 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006125#endif
6126
Ronald Cron73fe8df2022-10-05 14:31:43 +02006127#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006130 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6131 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006132#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006134 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006135 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006136 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006137 }
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006140 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006141 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006142 conf->psk_identity_len = 0;
6143 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006144#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006145
6146#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006148#endif
6149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006151}
6152
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006153#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006154 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006155/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006156 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006158unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006159{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6162 return MBEDTLS_SSL_SIG_RSA;
6163 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006164#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006165#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6167 return MBEDTLS_SSL_SIG_ECDSA;
6168 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006169#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006171}
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006174{
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006176 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006178 case MBEDTLS_PK_ECDSA:
6179 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006181 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006183 }
6184}
6185
Gilles Peskine449bd832023-01-11 14:50:10 +01006186mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006187{
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189#if defined(MBEDTLS_RSA_C)
6190 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006192#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006193#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006196#endif
6197 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006199 }
6200}
Valerio Settie9646ec2023-08-02 20:02:28 +02006201#endif /* MBEDTLS_PK_C &&
6202 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006203
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006204/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006205 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006207mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006208{
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006210#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006213#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006214#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006217#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006218#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006221#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006222#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006225#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006226#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006229#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006230#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006233#endif
6234 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006236 }
6237}
6238
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006239/*
6240 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006242unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006243{
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006245#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006246 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006248#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006249#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006250 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006252#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006253#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006254 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006256#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006257#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006258 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006260#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006261#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006262 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006264#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006265#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006266 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006268#endif
6269 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006271 }
6272}
6273
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006274/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006275 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006276 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006278int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006279{
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (group_list == NULL) {
6283 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006284 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006285
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 for (; *group_list != 0; group_list++) {
6287 if (*group_list == tls_id) {
6288 return 0;
6289 }
6290 }
6291
6292 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006293}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006294
Valerio Setti49e69072023-06-27 17:27:51 +02006295#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006296/*
6297 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006299int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006300{
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006304 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006308}
Valerio Setti49e69072023-06-27 17:27:51 +02006309#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006310
Valerio Setti18c9fed2022-12-30 17:44:24 +01006311static const struct {
6312 uint16_t tls_id;
6313 mbedtls_ecp_group_id ecp_group_id;
6314 psa_ecc_family_t psa_family;
6315 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006316} tls_id_match_table[] =
6317{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006318#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006319 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006320#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006321#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006322 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006323#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006324#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006325 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006326#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006327#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006328 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006329#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006330#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006331 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006332#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006333#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006334 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006335#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006336#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006337 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006338#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006339#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006340 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006341#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006342#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006343 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006344#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006345#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006346 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006347#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006348#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006349 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006350#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006351#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006352 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006353#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006354#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006355 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006356#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006357 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006358};
6359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006361 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006363{
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6365 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006366 if (type != NULL) {
6367 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 }
6369 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006370 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006371 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006372 return PSA_SUCCESS;
6373 }
6374 }
6375
6376 return PSA_ERROR_NOT_SUPPORTED;
6377}
6378
Gilles Peskine449bd832023-01-11 14:50:10 +01006379mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006380{
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6382 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006383 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006385 }
6386
6387 return MBEDTLS_ECP_DP_NONE;
6388}
6389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006391{
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6393 i++) {
6394 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006395 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006397 }
6398
6399 return 0;
6400}
6401
Valerio Setti67419f02023-01-04 16:12:42 +01006402#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006403static const struct {
6404 uint16_t tls_id;
6405 const char *name;
6406} tls_id_curve_name_table[] =
6407{
Valerio Setti54e23792023-07-07 10:49:27 +02006408 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6409 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6410 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6411 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6412 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6413 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6414 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6415 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6416 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6417 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6418 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6419 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6420 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006421 { 0, NULL },
6422};
6423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006425{
Valerio Settiacd32c02023-06-29 18:06:29 +02006426 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6427 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6428 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006430 }
6431
6432 return NULL;
6433}
Valerio Setti67419f02023-01-04 16:12:42 +01006434#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006435
Jerry Yu148165c2021-09-24 23:20:59 +08006436#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006437int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6438 const mbedtls_md_type_t md,
6439 unsigned char *dst,
6440 size_t dst_len,
6441 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006442{
Ronald Cronf6893e12022-01-07 22:09:01 +01006443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6444 psa_hash_operation_t *hash_operation_to_clone;
6445 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6446
Jerry Yu148165c2021-09-24 23:20:59 +08006447 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006450#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 case MBEDTLS_MD_SHA384:
6452 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6453 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006454#endif
6455
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006456#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 case MBEDTLS_MD_SHA256:
6458 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6459 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006460#endif
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 default:
6463 goto exit;
6464 }
6465
6466 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6467 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006468 goto exit;
6469 }
6470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6472 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006473 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006475
6476exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006477#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6478 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006479 (void) ssl;
6480#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006481 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006482}
6483#else /* MBEDTLS_USE_PSA_CRYPTO */
6484
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006485#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006486MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006487static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6488 unsigned char *dst,
6489 size_t dst_len,
6490 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006491{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006492 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006493 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 if (dst_len < 48) {
6496 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6497 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006498
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006499 mbedtls_md_init(&sha384);
6500 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006501 if (ret != 0) {
6502 goto exit;
6503 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006504 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006505 if (ret != 0) {
6506 goto exit;
6507 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006508
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006509 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006510 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006511 goto exit;
6512 }
6513
6514 *olen = 48;
6515
6516exit:
6517
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006518 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006520}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006521#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006522
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006523#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006524MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006525static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6526 unsigned char *dst,
6527 size_t dst_len,
6528 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006529{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006530 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006531 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 if (dst_len < 32) {
6534 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6535 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006536
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006537 mbedtls_md_init(&sha256);
6538 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6539 if (ret != 0) {
6540 goto exit;
6541 }
6542 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6543 if (ret != 0) {
6544 goto exit;
6545 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006546
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006547 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6548 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006549 goto exit;
6550 }
6551
6552 *olen = 32;
6553
6554exit:
6555
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006556 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006558}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006559#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6562 const mbedtls_md_type_t md,
6563 unsigned char *dst,
6564 size_t dst_len,
6565 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006566{
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006568
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006569#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 case MBEDTLS_MD_SHA384:
6571 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006572#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006573
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006574#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 case MBEDTLS_MD_SHA256:
6576 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006577#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006580#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6581 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 (void) ssl;
6583 (void) dst;
6584 (void) dst_len;
6585 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006586#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006588 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006590}
XiaokangQian647719a2021-12-07 09:16:29 +00006591
Jerry Yu148165c2021-09-24 23:20:59 +08006592#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006593
Ronald Crone68ab4f2022-10-05 12:46:29 +02006594#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006595/* mbedtls_ssl_parse_sig_alg_ext()
6596 *
6597 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6598 * value (TLS 1.3 RFC8446):
6599 * enum {
6600 * ....
6601 * ecdsa_secp256r1_sha256( 0x0403 ),
6602 * ecdsa_secp384r1_sha384( 0x0503 ),
6603 * ecdsa_secp521r1_sha512( 0x0603 ),
6604 * ....
6605 * } SignatureScheme;
6606 *
6607 * struct {
6608 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6609 * } SignatureSchemeList;
6610 *
6611 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6612 * value (TLS 1.2 RFC5246):
6613 * enum {
6614 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6615 * sha512(6), (255)
6616 * } HashAlgorithm;
6617 *
6618 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6619 * SignatureAlgorithm;
6620 *
6621 * struct {
6622 * HashAlgorithm hash;
6623 * SignatureAlgorithm signature;
6624 * } SignatureAndHashAlgorithm;
6625 *
6626 * SignatureAndHashAlgorithm
6627 * supported_signature_algorithms<2..2^16-2>;
6628 *
6629 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6630 * generalization of the TLS 1.2 signature algorithm extension.
6631 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6632 * `SignatureScheme` field of TLS 1.3
6633 *
6634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006635int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6636 const unsigned char *buf,
6637 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006638{
6639 const unsigned char *p = buf;
6640 size_t supported_sig_algs_len = 0;
6641 const unsigned char *supported_sig_algs_end;
6642 uint16_t sig_alg;
6643 uint32_t common_idx = 0;
6644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6646 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006647 p += 2;
6648
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 memset(ssl->handshake->received_sig_algs, 0,
6650 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006653 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 while (p < supported_sig_algs_end) {
6655 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6656 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006657 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6659 sig_alg,
6660 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006661#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6663 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6664 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006665 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006666 }
6667#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6670 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006673 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6674 common_idx += 1;
6675 }
6676 }
6677 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 if (p != end) {
6679 MBEDTLS_SSL_DEBUG_MSG(1,
6680 ("Signature algorithms extension length misaligned"));
6681 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6682 MBEDTLS_ERR_SSL_DECODE_ERROR);
6683 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006684 }
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 if (common_idx == 0) {
6687 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6688 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6689 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6690 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006691 }
6692
Gabor Mezei15b95a62022-05-09 16:37:58 +02006693 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006695}
6696
Ronald Crone68ab4f2022-10-05 12:46:29 +02006697#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006698
Jerry Yudc7bd172022-02-17 13:44:15 +08006699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6700
6701#if defined(MBEDTLS_USE_PSA_CRYPTO)
6702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6704 mbedtls_svc_key_id_t key,
6705 psa_algorithm_t alg,
6706 const unsigned char *raw_psk, size_t raw_psk_length,
6707 const unsigned char *seed, size_t seed_length,
6708 const unsigned char *label, size_t label_length,
6709 const unsigned char *other_secret,
6710 size_t other_secret_length,
6711 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006712{
6713 psa_status_t status;
6714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 status = psa_key_derivation_setup(derivation, alg);
6716 if (status != PSA_SUCCESS) {
6717 return status;
6718 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6721 status = psa_key_derivation_input_bytes(derivation,
6722 PSA_KEY_DERIVATION_INPUT_SEED,
6723 seed, seed_length);
6724 if (status != PSA_SUCCESS) {
6725 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006726 }
6727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 if (other_secret != NULL) {
6729 status = psa_key_derivation_input_bytes(derivation,
6730 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6731 other_secret, other_secret_length);
6732 if (status != PSA_SUCCESS) {
6733 return status;
6734 }
6735 }
6736
6737 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006738 status = psa_key_derivation_input_bytes(
6739 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 raw_psk, raw_psk_length);
6741 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006742 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 if (status != PSA_SUCCESS) {
6746 return status;
6747 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 status = psa_key_derivation_input_bytes(derivation,
6750 PSA_KEY_DERIVATION_INPUT_LABEL,
6751 label, label_length);
6752 if (status != PSA_SUCCESS) {
6753 return status;
6754 }
6755 } else {
6756 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006757 }
6758
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 status = psa_key_derivation_set_capacity(derivation, capacity);
6760 if (status != PSA_SUCCESS) {
6761 return status;
6762 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006763
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006765}
6766
Andrzej Kurek57d10632022-10-24 10:32:01 -04006767#if defined(PSA_WANT_ALG_SHA_384) || \
6768 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006769MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006770static int tls_prf_generic(mbedtls_md_type_t md_type,
6771 const unsigned char *secret, size_t slen,
Max Fillinger76077e22024-10-23 15:47:23 +02006772 const char *label, size_t label_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 const unsigned char *random, size_t rlen,
6774 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006775{
6776 psa_status_t status;
6777 psa_algorithm_t alg;
6778 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6779 psa_key_derivation_operation_t derivation =
6780 PSA_KEY_DERIVATION_OPERATION_INIT;
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006783 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006785 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006787
6788 /* Normally a "secret" should be long enough to be impossible to
6789 * find by brute force, and in particular should not be empty. But
6790 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6791 * and for this use case it makes sense to have a 0-length "secret".
6792 * Since the key API doesn't allow importing a key of length 0,
6793 * keep master_key=0, which setup_psa_key_derivation() understands
6794 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006796 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6798 psa_set_key_algorithm(&key_attributes, alg);
6799 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006800
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6802 if (status != PSA_SUCCESS) {
6803 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6804 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006805 }
6806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 status = setup_psa_key_derivation(&derivation,
6808 master_key, alg,
6809 NULL, 0,
6810 random, rlen,
6811 (unsigned char const *) label,
Max Fillinger76077e22024-10-23 15:47:23 +02006812 label_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 NULL, 0,
6814 dlen);
6815 if (status != PSA_SUCCESS) {
6816 psa_key_derivation_abort(&derivation);
6817 psa_destroy_key(master_key);
6818 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006819 }
6820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6822 if (status != PSA_SUCCESS) {
6823 psa_key_derivation_abort(&derivation);
6824 psa_destroy_key(master_key);
6825 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006826 }
6827
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 status = psa_key_derivation_abort(&derivation);
6829 if (status != PSA_SUCCESS) {
6830 psa_destroy_key(master_key);
6831 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006832 }
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (!mbedtls_svc_key_id_is_null(master_key)) {
6835 status = psa_destroy_key(master_key);
6836 }
6837 if (status != PSA_SUCCESS) {
6838 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6839 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006840
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006842}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006843#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006844#else /* MBEDTLS_USE_PSA_CRYPTO */
6845
Andrzej Kurek57d10632022-10-24 10:32:01 -04006846#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006847 (defined(MBEDTLS_MD_CAN_SHA256) || \
6848 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006849MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006850static int tls_prf_generic(mbedtls_md_type_t md_type,
6851 const unsigned char *secret, size_t slen,
Max Fillinger40c20242025-04-15 21:18:20 +02006852 const char *label, size_t label_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 const unsigned char *random, size_t rlen,
6854 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006855{
6856 size_t nb;
6857 size_t i, j, k, md_len;
6858 unsigned char *tmp;
6859 size_t tmp_len = 0;
6860 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6861 const mbedtls_md_info_t *md_info;
6862 mbedtls_md_context_t md_ctx;
6863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6868 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6869 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006872
Max Fillinger40c20242025-04-15 21:18:20 +02006873 tmp_len = md_len + label_len + rlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 tmp = mbedtls_calloc(1, tmp_len);
6875 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006876 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6877 goto exit;
6878 }
6879
Max Fillinger40c20242025-04-15 21:18:20 +02006880 nb = label_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 memcpy(tmp + md_len, label, nb);
6882 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006883 nb += rlen;
6884
6885 /*
6886 * Compute P_<hash>(secret, label + random)[0..dlen]
6887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006889 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6893 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006894 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 }
6896 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6897 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006898 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 }
6900 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6901 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006902 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 for (i = 0; i < dlen; i += md_len) {
6906 ret = mbedtls_md_hmac_reset(&md_ctx);
6907 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006908 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 }
6910 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6911 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006912 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 }
6914 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6915 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006916 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 ret = mbedtls_md_hmac_reset(&md_ctx);
6920 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006921 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 }
6923 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6924 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006925 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 }
6927 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6928 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006929 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006933
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006935 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006937 }
6938
6939exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006941
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 if (tmp != NULL) {
6943 mbedtls_platform_zeroize(tmp, tmp_len);
6944 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006951}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006952#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006953#endif /* MBEDTLS_USE_PSA_CRYPTO */
6954
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006955#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006956MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006957static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6958 const char *label,
6959 const unsigned char *random, size_t rlen,
6960 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006961{
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
Max Fillinger76077e22024-10-23 15:47:23 +02006963 label, strlen(label), random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006964}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006965#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006966
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006967#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006968MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006969static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6970 const char *label,
6971 const unsigned char *random, size_t rlen,
6972 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006973{
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
Max Fillinger76077e22024-10-23 15:47:23 +02006975 label, strlen(label), random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006976}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006977#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006978
Jerry Yuf009d862022-02-17 14:01:37 +08006979/*
6980 * Set appropriate PRF function and other SSL / TLS1.2 functions
6981 *
6982 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006983 * - hash associated with the ciphersuite (only used by TLS 1.2)
6984 *
6985 * Outputs:
6986 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6987 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006988MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006989static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6990 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006991{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006992#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006994 handshake->tls_prf = tls_prf_sha384;
6995 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6996 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006998#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006999#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08007000 {
Ronald Cron81591aa2022-03-07 09:05:51 +01007001 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08007002 handshake->tls_prf = tls_prf_sha256;
7003 handshake->calc_verify = ssl_calc_verify_tls_sha256;
7004 handshake->calc_finished = ssl_calc_finished_tls_sha256;
7005 }
Ronald Cron81591aa2022-03-07 09:05:51 +01007006#else
Jerry Yuf009d862022-02-17 14:01:37 +08007007 {
Jerry Yuf009d862022-02-17 14:01:37 +08007008 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01007009 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08007011 }
Ronald Cron81591aa2022-03-07 09:05:51 +01007012#endif
Jerry Yuf009d862022-02-17 14:01:37 +08007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08007015}
Jerry Yud6ab2352022-02-17 14:03:43 +08007016
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007017/*
7018 * Compute master secret if needed
7019 *
7020 * Parameters:
7021 * [in/out] handshake
7022 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
7023 * (PSA-PSK) ciphersuite_info, psk_opaque
7024 * [out] premaster (cleared)
7025 * [out] master
7026 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
7027 * debug: conf->f_dbg, conf->p_dbg
7028 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01007029 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007030 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007031MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007032static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
7033 unsigned char *master,
7034 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007035{
7036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7037
7038 /* cf. RFC 5246, Section 8.1:
7039 * "The master secret is always exactly 48 bytes in length." */
7040 size_t const master_secret_len = 48;
7041
7042#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7043 unsigned char session_hash[48];
7044#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7045
7046 /* The label for the KDF used for key expansion.
7047 * This is either "master secret" or "extended master secret"
7048 * depending on whether the Extended Master Secret extension
7049 * is used. */
7050 char const *lbl = "master secret";
7051
Przemek Stekielae4ed302022-04-05 17:15:55 +02007052 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007053 * - If the Extended Master Secret extension is not used,
7054 * this is ClientHello.Random + ServerHello.Random
7055 * (see Sect. 8.1 in RFC 5246).
7056 * - If the Extended Master Secret extension is used,
7057 * this is the transcript of the handshake so far.
7058 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007059 unsigned char const *seed = handshake->randbytes;
7060 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007061
7062#if !defined(MBEDTLS_DEBUG_C) && \
7063 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7064 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007066 ssl = NULL; /* make sure we don't use it except for those cases */
7067 (void) ssl;
7068#endif
7069
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 if (handshake->resume != 0) {
7071 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7072 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007073 }
7074
7075#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007076 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007077 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007078 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007079 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7080 if (ret != 0) {
7081 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7082 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007083
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7085 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007086 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007087#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007088
Przemek Stekiel99114f32022-04-22 11:20:09 +02007089#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007090 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007092 /* Perform PSK-to-MS expansion in a single step. */
7093 psa_status_t status;
7094 psa_algorithm_t alg;
7095 mbedtls_svc_key_id_t psk;
7096 psa_key_derivation_operation_t derivation =
7097 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007098 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007105 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007107 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007109
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007110 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007114 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007115 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007116 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007117 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007118 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007119 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007120 other_secret_len = 48;
7121 other_secret = handshake->premaster + 2;
7122 break;
7123 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007124 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007125 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7126 other_secret = handshake->premaster + 2;
7127 break;
7128 default:
7129 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007130 }
7131
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 status = setup_psa_key_derivation(&derivation, psk, alg,
7133 ssl->conf->psk, ssl->conf->psk_len,
7134 seed, seed_len,
7135 (unsigned char const *) lbl,
7136 (size_t) strlen(lbl),
7137 other_secret, other_secret_len,
7138 master_secret_len);
7139 if (status != PSA_SUCCESS) {
7140 psa_key_derivation_abort(&derivation);
7141 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007142 }
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 status = psa_key_derivation_output_bytes(&derivation,
7145 master,
7146 master_secret_len);
7147 if (status != PSA_SUCCESS) {
7148 psa_key_derivation_abort(&derivation);
7149 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007150 }
7151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 status = psa_key_derivation_abort(&derivation);
7153 if (status != PSA_SUCCESS) {
7154 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7155 }
7156 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007157#endif
7158 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007159#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7161 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007162 psa_status_t status;
7163 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7164 psa_key_derivation_operation_t derivation =
7165 PSA_KEY_DERIVATION_OPERATION_INIT;
7166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007168
7169 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 status = psa_key_derivation_setup(&derivation, alg);
7172 if (status != PSA_SUCCESS) {
7173 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007174 }
7175
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 status = psa_key_derivation_set_capacity(&derivation,
7177 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7178 if (status != PSA_SUCCESS) {
7179 psa_key_derivation_abort(&derivation);
7180 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007181 }
7182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7184 &derivation);
7185 if (status != PSA_SUCCESS) {
7186 psa_key_derivation_abort(&derivation);
7187 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007188 }
7189
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 status = psa_key_derivation_output_bytes(&derivation,
7191 handshake->premaster,
7192 handshake->pmslen);
7193 if (status != PSA_SUCCESS) {
7194 psa_key_derivation_abort(&derivation);
7195 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7196 }
7197
7198 status = psa_key_derivation_abort(&derivation);
7199 if (status != PSA_SUCCESS) {
7200 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007201 }
7202 }
7203#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7205 lbl, seed, seed_len,
7206 master,
7207 master_secret_len);
7208 if (ret != 0) {
7209 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7210 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007211 }
7212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7214 handshake->premaster,
7215 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007216
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 mbedtls_platform_zeroize(handshake->premaster,
7218 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007219 }
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007222}
7223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007225{
7226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7227 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7228 ssl->handshake->ciphersuite_info;
7229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007231
7232 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007234 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 if (ret != 0) {
7236 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7237 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007238 }
7239
7240 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 ret = ssl_compute_master(ssl->handshake,
7242 ssl->session_negotiate->master,
7243 ssl);
7244 if (ret != 0) {
7245 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7246 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007247 }
7248
7249 /* Swap the client and server random values:
7250 * - MS derivation wanted client+server (RFC 5246 8.1)
7251 * - key derivation wants server+client (RFC 5246 6.3) */
7252 {
7253 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 memcpy(tmp, ssl->handshake->randbytes, 64);
7255 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7256 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7257 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007258 }
7259
7260 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7262 ssl->session_negotiate->ciphersuite,
7263 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007264#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007266#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 ssl->handshake->tls_prf,
7268 ssl->handshake->randbytes,
7269 ssl->tls_version,
7270 ssl->conf->endpoint,
7271 ssl);
7272 if (ret != 0) {
7273 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7274 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007275 }
7276
7277 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7279 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007280
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007282
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007284}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007285
Gilles Peskine449bd832023-01-11 14:50:10 +01007286int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007287{
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007289#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007290 case MBEDTLS_SSL_HASH_SHA384:
7291 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7292 break;
7293#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007294#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007295 case MBEDTLS_SSL_HASH_SHA256:
7296 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7297 break;
7298#endif
7299 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007301 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007302#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7303 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007304 (void) ssl;
7305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007307}
7308
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007309#if defined(MBEDTLS_USE_PSA_CRYPTO)
7310static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7311 const psa_hash_operation_t *hs_op,
7312 size_t buffer_size,
7313 unsigned char *hash,
7314 size_t *hlen)
7315{
7316 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007317 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007318
7319#if !defined(MBEDTLS_DEBUG_C)
7320 (void) ssl;
7321#endif
7322 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007323 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007324 if (status != PSA_SUCCESS) {
7325 goto exit;
7326 }
7327
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007328 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007329 if (status != PSA_SUCCESS) {
7330 goto exit;
7331 }
7332
7333 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7334 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7335
7336exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007337 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007338 return mbedtls_md_error_from_psa(status);
7339}
7340#else
7341static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7342 const mbedtls_md_context_t *hs_ctx,
7343 unsigned char *hash,
7344 size_t *hlen)
7345{
7346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007347 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007348
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007349 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007350
7351#if !defined(MBEDTLS_DEBUG_C)
7352 (void) ssl;
7353#endif
7354 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7355
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007356 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007357 if (ret != 0) {
7358 goto exit;
7359 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007360 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007361 if (ret != 0) {
7362 goto exit;
7363 }
7364
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007365 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007366 if (ret != 0) {
7367 goto exit;
7368 }
7369
7370 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7371
7372 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7373 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7374
7375exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007376 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007377 return ret;
7378}
7379#endif /* MBEDTLS_USE_PSA_CRYPTO */
7380
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007381#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007382int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7383 unsigned char *hash,
7384 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007385{
7386#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007387 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7388 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007389#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007390 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7391 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007392#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007393}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007394#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007395
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007396#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007397int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7398 unsigned char *hash,
7399 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007400{
7401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007402 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7403 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007404#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007405 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7406 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007407#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007408}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007409#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007410
Neil Armstrong80f6f322022-05-03 17:56:38 +02007411#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7412 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007413int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007414{
7415 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007417 const unsigned char *psk = NULL;
7418 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007420
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007422 /*
7423 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007424 * checked before calling this function.
7425 *
7426 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007427 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007428 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7430 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7431 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007432 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007433 }
7434
7435 /*
7436 * PMS = struct {
7437 * opaque other_secret<0..2^16-1>;
7438 * opaque psk<0..2^16-1>;
7439 * };
7440 * with "other_secret" depending on the particular key exchange
7441 */
7442#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7444 if (end - p < 2) {
7445 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7446 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007449 p += 2;
7450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if (end < p || (size_t) (end - p) < psk_len) {
7452 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7453 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007456 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007458#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7459#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007461 /*
7462 * other_secret already set by the ClientKeyExchange message,
7463 * and is 48 bytes long
7464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if (end - p < 2) {
7466 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7467 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007468
7469 *p++ = 0;
7470 *p++ = 48;
7471 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007473#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7474#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7477 size_t len;
7478
7479 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007481 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7483 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7484 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007485 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007487 p += 2 + len;
7488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7490 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007491#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007492#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7495 size_t zlen;
7496
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007498 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7500 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7501 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007502 }
7503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007505 p += 2 + zlen;
7506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7508 MBEDTLS_DEBUG_ECDH_Z);
7509 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007510#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007511 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7513 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007514 }
7515
7516 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (end - p < 2) {
7518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7519 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007522 p += 2;
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 if (end < p || (size_t) (end - p) < psk_len) {
7525 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7526 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007527
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007529 p += psk_len;
7530
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007531 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007532
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007534}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007535#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007536
7537#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007538MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007539static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007540
7541#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007542int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007543{
7544 /* If renegotiation is not enforced, retransmit until we would reach max
7545 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007547 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7548 unsigned char doublings = 1;
7549
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007551 ++doublings;
7552 ratio >>= 1;
7553 }
7554
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 if (++ssl->renego_records_seen > doublings) {
7556 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7557 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007558 }
7559 }
7560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007562}
7563#endif
7564#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007565
Jerry Yud9526692022-02-17 14:23:47 +08007566/*
7567 * Handshake functions
7568 */
7569#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7570/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007571int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007572{
7573 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7574 ssl->handshake->ciphersuite_info;
7575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007577
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7579 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01007580 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007582 }
7583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7585 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007586}
7587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007589{
7590 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7591 ssl->handshake->ciphersuite_info;
7592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7596 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01007597 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007599 }
7600
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7602 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007603}
7604
7605#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7606/* Some certificate support -> implement write and parse */
7607
Gilles Peskine449bd832023-01-11 14:50:10 +01007608int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007609{
7610 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7611 size_t i, n;
7612 const mbedtls_x509_crt *crt;
7613 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7614 ssl->handshake->ciphersuite_info;
7615
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7619 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01007620 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007622 }
7623
7624#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7626 if (ssl->handshake->client_auth == 0) {
7627 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Gilles Peskine49f179d2025-03-07 15:09:32 +01007628 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007630 }
7631 }
7632#endif /* MBEDTLS_SSL_CLI_C */
7633#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7635 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007636 /* Should never happen because we shouldn't have picked the
7637 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007639 }
7640 }
7641#endif
7642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007644
7645 /*
7646 * 0 . 0 handshake type
7647 * 1 . 3 handshake length
7648 * 4 . 6 length of all certs
7649 * 7 . 9 length of cert. 1
7650 * 10 . n-1 peer certificate
7651 * n . n+2 length of cert. 2
7652 * n+3 . ... upper level cert, etc.
7653 */
7654 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007658 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7660 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7661 " > %" MBEDTLS_PRINTF_SIZET,
7662 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7663 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007664 }
7665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7667 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7668 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007671 i += n; crt = crt->next;
7672 }
7673
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7675 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7676 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007677
7678 ssl->out_msglen = i;
7679 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7680 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7681
Gilles Peskine49f179d2025-03-07 15:09:32 +01007682 mbedtls_ssl_handshake_increment_state(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7685 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7686 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007687 }
7688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007692}
7693
7694#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7695
7696#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007698static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7699 unsigned char *crt_buf,
7700 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007701{
7702 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7703
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 if (peer_crt == NULL) {
7705 return -1;
7706 }
Jerry Yud9526692022-02-17 14:23:47 +08007707
Gilles Peskine449bd832023-01-11 14:50:10 +01007708 if (peer_crt->raw.len != crt_buf_len) {
7709 return -1;
7710 }
Jerry Yud9526692022-02-17 14:23:47 +08007711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007713}
7714#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007716static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7717 unsigned char *crt_buf,
7718 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007719{
7720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7721 unsigned char const * const peer_cert_digest =
7722 ssl->session->peer_cert_digest;
7723 mbedtls_md_type_t const peer_cert_digest_type =
7724 ssl->session->peer_cert_digest_type;
7725 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007727 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7728 size_t digest_len;
7729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 if (peer_cert_digest == NULL || digest_info == NULL) {
7731 return -1;
7732 }
Jerry Yud9526692022-02-17 14:23:47 +08007733
Gilles Peskine449bd832023-01-11 14:50:10 +01007734 digest_len = mbedtls_md_get_size(digest_info);
7735 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7736 return -1;
7737 }
Jerry Yud9526692022-02-17 14:23:47 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7740 if (ret != 0) {
7741 return -1;
7742 }
Jerry Yud9526692022-02-17 14:23:47 +08007743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007745}
7746#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7747#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7748
7749/*
7750 * Once the certificate message is read, parse it into a cert chain and
7751 * perform basic checks, but leave actual verification to the caller
7752 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007753MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007754static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7755 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007756{
7757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7758#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007760#endif
7761 size_t i, n;
7762 uint8_t alert;
7763
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7765 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7766 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7767 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7768 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007769 }
7770
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7772 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7773 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7774 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007775 }
7776
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7778 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7779 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7780 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7781 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007782 }
7783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007785
7786 /*
7787 * Same message structure as in mbedtls_ssl_write_certificate()
7788 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007789 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007790
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 if (ssl->in_msg[i] != 0 ||
7792 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7793 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7794 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7795 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7796 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007797 }
7798
7799 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7800 i += 3;
7801
7802 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007804 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 if (i + 3 > ssl->in_hslen) {
7806 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7807 mbedtls_ssl_send_alert_message(ssl,
7808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7809 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7810 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007811 }
7812 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7813 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 if (ssl->in_msg[i] != 0) {
7815 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7816 mbedtls_ssl_send_alert_message(ssl,
7817 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7818 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7819 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007820 }
7821
7822 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007823 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007824 i += 3;
7825
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 if (n < 128 || i + n > ssl->in_hslen) {
7827 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7828 mbedtls_ssl_send_alert_message(ssl,
7829 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7830 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7831 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007832 }
7833
7834 /* Check if we're handling the first CRT in the chain. */
7835#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007837 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007839 /* During client-side renegotiation, check that the server's
7840 * end-CRTs hasn't changed compared to the initial handshake,
7841 * mitigating the triple handshake attack. On success, reuse
7842 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7844 if (ssl_check_peer_crt_unchanged(ssl,
7845 &ssl->in_msg[i],
7846 n) != 0) {
7847 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7848 mbedtls_ssl_send_alert_message(ssl,
7849 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7850 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7851 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007852 }
7853
7854 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007856 }
7857#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7858
7859 /* Parse the next certificate in the chain. */
7860#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007862#else
7863 /* If we don't need to store the CRT chain permanently, parse
7864 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007866#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007868 case 0: /*ok*/
7869 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7870 /* Ignore certificate with an unknown algorithm: maybe a
7871 prior certificate was already trusted. */
7872 break;
7873
7874 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7875 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7876 goto crt_parse_der_failed;
7877
7878 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7879 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7880 goto crt_parse_der_failed;
7881
7882 default:
7883 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007884crt_parse_der_failed:
7885 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7886 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7887 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007888 }
7889
7890 i += n;
7891 }
7892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7894 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007895}
7896
7897#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007899static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007900{
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7902 return -1;
7903 }
Jerry Yud9526692022-02-17 14:23:47 +08007904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007906 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7907 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7909 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7910 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007911 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007913}
7914#endif /* MBEDTLS_SSL_SRV_C */
7915
7916/* Check if a certificate message is expected.
7917 * Return either
7918 * - SSL_CERTIFICATE_EXPECTED, or
7919 * - SSL_CERTIFICATE_SKIP
7920 * indicating whether a Certificate message is expected or not.
7921 */
7922#define SSL_CERTIFICATE_EXPECTED 0
7923#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007924MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007925static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7926 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007927{
7928 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7929 ssl->handshake->ciphersuite_info;
7930
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7932 return SSL_CERTIFICATE_SKIP;
7933 }
Jerry Yud9526692022-02-17 14:23:47 +08007934
7935#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7937 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7938 return SSL_CERTIFICATE_SKIP;
7939 }
Jerry Yud9526692022-02-17 14:23:47 +08007940
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007942 ssl->session_negotiate->verify_result =
7943 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007945 }
7946 }
7947#else
7948 ((void) authmode);
7949#endif /* MBEDTLS_SSL_SRV_C */
7950
Gilles Peskine449bd832023-01-11 14:50:10 +01007951 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007952}
7953
Jerry Yud9526692022-02-17 14:23:47 +08007954#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007955MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007956static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7957 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007958{
7959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7960 /* Remember digest of the peer's end-CRT. */
7961 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7963 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7964 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7965 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7966 mbedtls_ssl_send_alert_message(ssl,
7967 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7968 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007969
Gilles Peskine449bd832023-01-11 14:50:10 +01007970 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007971 }
7972
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 ret = mbedtls_md(mbedtls_md_info_from_type(
7974 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7975 start, len,
7976 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007977
7978 ssl->session_negotiate->peer_cert_digest_type =
7979 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7980 ssl->session_negotiate->peer_cert_digest_len =
7981 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7982
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007984}
7985
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007986MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007987static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7988 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007989{
7990 unsigned char *end = start + len;
7991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7992
7993 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7995 ret = mbedtls_pk_parse_subpubkey(&start, end,
7996 &ssl->handshake->peer_pubkey);
7997 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007998 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08008000 }
8001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08008003}
8004#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8005
Gilles Peskine449bd832023-01-11 14:50:10 +01008006int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08008007{
8008 int ret = 0;
8009 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02008010 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08008011#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8012 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
8013 ? ssl->handshake->sni_authmode
8014 : ssl->conf->authmode;
8015#else
8016 const int authmode = ssl->conf->authmode;
8017#endif
8018 void *rs_ctx = NULL;
8019 mbedtls_x509_crt *chain = NULL;
8020
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
8024 if (crt_expected == SSL_CERTIFICATE_SKIP) {
8025 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008026 goto exit;
8027 }
8028
8029#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 if (ssl->handshake->ecrs_enabled &&
8031 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08008032 chain = ssl->handshake->ecrs_peer_cert;
8033 ssl->handshake->ecrs_peer_cert = NULL;
8034 goto crt_verify;
8035 }
8036#endif
8037
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008039 /* mbedtls_ssl_read_record may have sent an alert already. We
8040 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08008042 goto exit;
8043 }
8044
8045#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008046 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008047 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8048
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08008050 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008051 }
Jerry Yud9526692022-02-17 14:23:47 +08008052
8053 goto exit;
8054 }
8055#endif /* MBEDTLS_SSL_SRV_C */
8056
8057 /* Clear existing peer CRT structure in case we tried to
8058 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008060
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8062 if (chain == NULL) {
8063 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8064 sizeof(mbedtls_x509_crt)));
8065 mbedtls_ssl_send_alert_message(ssl,
8066 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8067 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008068
8069 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8070 goto exit;
8071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008073
Gilles Peskine449bd832023-01-11 14:50:10 +01008074 ret = ssl_parse_certificate_chain(ssl, chain);
8075 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008076 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 }
Jerry Yud9526692022-02-17 14:23:47 +08008078
8079#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008080 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008081 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008082 }
Jerry Yud9526692022-02-17 14:23:47 +08008083
8084crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008086 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 }
Jerry Yud9526692022-02-17 14:23:47 +08008088#endif
8089
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008090 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8091 ssl->handshake->ciphersuite_info,
8092 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 }
Jerry Yud9526692022-02-17 14:23:47 +08008096
8097#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8098 {
8099 unsigned char *crt_start, *pk_start;
8100 size_t crt_len, pk_len;
8101
8102 /* We parse the CRT chain without copying, so
8103 * these pointers point into the input buffer,
8104 * and are hence still valid after freeing the
8105 * CRT chain. */
8106
8107 crt_start = chain->raw.p;
8108 crt_len = chain->raw.len;
8109
8110 pk_start = chain->pk_raw.p;
8111 pk_len = chain->pk_raw.len;
8112
8113 /* Free the CRT structures before computing
8114 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 mbedtls_x509_crt_free(chain);
8116 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008117 chain = NULL;
8118
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8120 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 }
Jerry Yud9526692022-02-17 14:23:47 +08008123
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8125 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 }
Jerry Yud9526692022-02-17 14:23:47 +08008128 }
8129#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8130 /* Pass ownership to session structure. */
8131 ssl->session_negotiate->peer_cert = chain;
8132 chain = NULL;
8133#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8134
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008136
8137exit:
8138
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 if (ret == 0) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008140 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 }
Jerry Yud9526692022-02-17 14:23:47 +08008142
8143#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008145 ssl->handshake->ecrs_peer_cert = chain;
8146 chain = NULL;
8147 }
8148#endif
8149
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 if (chain != NULL) {
8151 mbedtls_x509_crt_free(chain);
8152 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008153 }
8154
Gilles Peskine449bd832023-01-11 14:50:10 +01008155 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008156}
8157#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8158
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008159static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8160 unsigned char *padbuf, size_t hlen,
8161 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008162{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008163 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008164 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008165#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008166 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008167 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008168 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008169 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008170#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008172 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008173 mbedtls_md_context_t cloned_ctx;
8174 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008175#endif
8176
8177 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008179 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008181
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008183 ? "client finished"
8184 : "server finished";
8185
8186#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008187 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008188
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008189 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008191 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008192 }
8193
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008194 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008196 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008197 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008198 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008199#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008200 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008201
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008202 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008203 if (ret != 0) {
8204 goto exit;
8205 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008206 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008207 if (ret != 0) {
8208 goto exit;
8209 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008210
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008211 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008212 if (ret != 0) {
8213 goto exit;
8214 }
8215#endif /* MBEDTLS_USE_PSA_CRYPTO */
8216
8217 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8218
Jerry Yu615bd6f2022-02-17 14:25:15 +08008219 /*
8220 * TLSv1.2:
8221 * hash = PRF( master, finished_label,
8222 * Hash( handshake ) )[0.11]
8223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008225 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008226
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008228
Paul Elliottecb95be2023-08-11 16:41:04 +01008229 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008230
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008231 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008232
8233exit:
8234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008235 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008236 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008237#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008238 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008239 return ret;
8240#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008241}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008242
8243#if defined(MBEDTLS_MD_CAN_SHA256)
8244static int ssl_calc_finished_tls_sha256(
8245 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8246{
8247 unsigned char padbuf[32];
8248 return ssl_calc_finished_tls_generic(ssl,
8249#if defined(MBEDTLS_USE_PSA_CRYPTO)
8250 &ssl->handshake->fin_sha256_psa,
8251#else
8252 &ssl->handshake->fin_sha256,
8253#endif
8254 padbuf, sizeof(padbuf),
8255 buf, from);
8256}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008257#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008258
Jerry Yub7ba49e2022-02-17 14:25:53 +08008259
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008260#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008261static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008263{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008264 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008265 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008266#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008267 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008268#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008269 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008270#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008271 padbuf, sizeof(padbuf),
8272 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008273}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008274#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008277{
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008279
8280 /*
8281 * Free our handshake params
8282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 mbedtls_ssl_handshake_free(ssl);
8284 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008285 ssl->handshake = NULL;
8286
8287 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008288 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 if (ssl->transform) {
8291 mbedtls_ssl_transform_free(ssl->transform);
8292 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008293 }
8294 ssl->transform = ssl->transform_negotiate;
8295 ssl->transform_negotiate = NULL;
8296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008298}
8299
Gilles Peskine449bd832023-01-11 14:50:10 +01008300void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008301{
8302 int resume = ssl->handshake->resume;
8303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008305
8306#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008308 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8309 ssl->renego_records_seen = 0;
8310 }
8311#endif
8312
8313 /*
8314 * Free the previous session and switch in the current one
8315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8318 /* RFC 7366 3.1: keep the EtM state */
8319 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008321#endif
8322
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 mbedtls_ssl_session_free(ssl->session);
8324 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008325 }
8326 ssl->session = ssl->session_negotiate;
8327 ssl->session_negotiate = NULL;
8328
8329 /*
8330 * Add cache entry
8331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008333 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 resume == 0) {
8335 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8336 ssl->session->id,
8337 ssl->session->id_len,
8338 ssl->session) != 0) {
8339 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8340 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008341 }
8342
8343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8345 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008346 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008348
8349 /* Keep last flight around in case we need to resend it:
8350 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8352 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008353#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008355
Gilles Peskine49f179d2025-03-07 15:09:32 +01008356 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008359}
8360
Gilles Peskine449bd832023-01-11 14:50:10 +01008361int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008362{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008363 int ret;
8364 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008369
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008370 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8371 if (ret != 0) {
8372 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
David Horstmannb5ef7da2025-03-07 17:20:59 +00008373 return ret;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008374 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008375
8376 /*
8377 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8378 * may define some other value. Currently (early 2016), no defined
8379 * ciphersuite does this (and this is unlikely to change as activity has
8380 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8381 */
8382 hash_len = 12;
8383
8384#if defined(MBEDTLS_SSL_RENEGOTIATION)
8385 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008387#endif
8388
8389 ssl->out_msglen = 4 + hash_len;
8390 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8391 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8392
8393 /*
8394 * In case of session resuming, invert the client and server
8395 * ChangeCipherSpec messages order.
8396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008398#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008400 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008402#endif
8403#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008405 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC);
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 } else {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008409 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008411
8412 /*
8413 * Switch to our negotiated transform and session parameters for outbound
8414 * data.
8415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008417
8418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008420 unsigned char i;
8421
8422 /* Remember current epoch settings for resending */
8423 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8425 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008426
8427 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008429
8430
8431 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 for (i = 2; i > 0; i--) {
8433 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008434 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 }
8436 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008437
8438 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 if (i == 0) {
8440 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8441 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008444#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008446
8447 ssl->transform_out = ssl->transform_negotiate;
8448 ssl->session_out = ssl->session_negotiate;
8449
8450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8452 mbedtls_ssl_send_flight_completed(ssl);
8453 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008454#endif
8455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8457 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8458 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008459 }
8460
8461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8463 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8464 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8465 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008466 }
8467#endif
8468
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008472}
8473
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008474#define SSL_MAX_HASH_LEN 12
8475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008477{
8478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8479 unsigned int hash_len = 12;
8480 unsigned char buf[SSL_MAX_HASH_LEN];
8481
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008483
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008484 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8485 if (ret != 0) {
8486 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
David Horstmannb5ef7da2025-03-07 17:20:59 +00008487 return ret;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008488 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008489
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8491 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008492 goto exit;
8493 }
8494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8496 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8497 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8498 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008499 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8500 goto exit;
8501 }
8502
Gilles Peskine449bd832023-01-11 14:50:10 +01008503 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8504 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8505 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008506 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8507 goto exit;
8508 }
8509
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8511 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8512 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8513 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008514 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8515 goto exit;
8516 }
8517
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8519 buf, hash_len) != 0) {
8520 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8521 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8522 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008523 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8524 goto exit;
8525 }
8526
8527#if defined(MBEDTLS_SSL_RENEGOTIATION)
8528 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008530#endif
8531
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008533#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008535 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC);
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008537#endif
8538#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008540 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008542#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 } else {
Gilles Peskine49f179d2025-03-07 15:09:32 +01008544 mbedtls_ssl_handshake_increment_state(ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008546
8547#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8549 mbedtls_ssl_recv_flight_completed(ssl);
8550 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008551#endif
8552
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008554
8555exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 mbedtls_platform_zeroize(buf, hash_len);
8557 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008558}
8559
Jerry Yu392112c2022-02-17 14:34:10 +08008560#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8561/*
8562 * Helper to get TLS 1.2 PRF from ciphersuite
8563 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008565static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008566{
Jerry Yu392112c2022-02-17 14:34:10 +08008567 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008569#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8571 return tls_prf_sha384;
8572 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008573#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008574#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008575 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8577 return tls_prf_sha256;
8578 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008579 }
8580#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008581#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8582 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008583 (void) ciphersuite_info;
8584#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008587}
8588#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008589
Gilles Peskine449bd832023-01-11 14:50:10 +01008590static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008591{
8592 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008593#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 if (tls_prf == tls_prf_sha384) {
8595 return MBEDTLS_SSL_TLS_PRF_SHA384;
8596 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008597#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008598#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 if (tls_prf == tls_prf_sha256) {
8600 return MBEDTLS_SSL_TLS_PRF_SHA256;
8601 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008602#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008604}
8605
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008606/*
8607 * Populate a transform structure with session keys and all the other
8608 * necessary information.
8609 *
8610 * Parameters:
8611 * - [in/out]: transform: structure to populate
8612 * [in] must be just initialised with mbedtls_ssl_transform_init()
8613 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8614 * - [in] ciphersuite
8615 * - [in] master
8616 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008617 * - [in] tls_prf: pointer to PRF to use for key derivation
8618 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008619 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008620 * - [in] endpoint: client or server
8621 * - [in] ssl: used for:
8622 * - ssl->conf->{f,p}_export_keys
8623 * [in] optionally used for:
8624 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8625 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008626MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008627static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8628 int ciphersuite,
8629 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008630#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008632#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 ssl_tls_prf_t tls_prf,
8634 const unsigned char randbytes[64],
8635 mbedtls_ssl_protocol_version tls_version,
8636 unsigned endpoint,
8637 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008638{
8639 int ret = 0;
8640 unsigned char keyblk[256];
8641 unsigned char *key1;
8642 unsigned char *key2;
8643 unsigned char *mac_enc;
8644 unsigned char *mac_dec;
8645 size_t mac_key_len = 0;
8646 size_t iv_copy_len;
8647 size_t keylen;
8648 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008649 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008650#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008651 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008652 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008653#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008654
8655#if defined(MBEDTLS_USE_PSA_CRYPTO)
8656 psa_key_type_t key_type;
8657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8658 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008659 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008660 size_t key_bits;
8661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8662#endif
8663
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008664 /*
8665 * Some data just needs copying into the structure
8666 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008667#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008668 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008669#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008670 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008671
Max Fillinger951b8862024-10-25 00:52:24 +02008672#if defined(MBEDTLS_SSL_KEEP_RANDBYTES)
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008674#endif
8675
8676#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008678 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8679 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008681 }
8682#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8683
8684 /*
8685 * Get various info structures
8686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8688 if (ciphersuite_info == NULL) {
8689 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8690 ciphersuite));
8691 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008692 }
8693
Neil Armstrongab555e02022-04-04 11:07:59 +02008694 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008695#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008697#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008699
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008701 transform->taglen =
8702 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008704
8705#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008706 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 transform->taglen,
8708 &alg,
8709 &key_type,
8710 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008711 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008713 goto end;
8714 }
8715#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008716 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 if (cipher_info == NULL) {
8718 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8719 ciphersuite_info->cipher));
8720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008721 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008722#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008723
Neil Armstronge4512952022-03-08 09:08:22 +01008724#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008725 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008727 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 (unsigned) ciphersuite_info->mac));
8729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008730 }
8731#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008732 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 if (md_info == NULL) {
8734 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8735 (unsigned) ciphersuite_info->mac));
8736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008737 }
Neil Armstronge4512952022-03-08 09:08:22 +01008738#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008739
8740#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8741 /* Copy own and peer's CID if the use of the CID
8742 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8744 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008745
8746 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8748 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8749 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008750
8751 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8753 ssl->handshake->peer_cid_len);
8754 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8755 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008756 }
8757#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8758
8759 /*
8760 * Compute key block using the PRF
8761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8763 if (ret != 0) {
8764 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8765 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766 }
8767
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8769 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8770 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8771 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8772 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008773
8774 /*
8775 * Determine the appropriate key, IV and MAC length.
8776 */
8777
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008778#if defined(MBEDTLS_USE_PSA_CRYPTO)
8779 keylen = PSA_BITS_TO_BYTES(key_bits);
8780#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008782#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008783
Valerio Settie5707042023-10-11 11:54:42 +02008784#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008786 size_t explicit_ivlen;
8787
8788 transform->maclen = 0;
8789 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008790
8791 /* All modes haves 96-bit IVs, but the length of the static parts vary
8792 * with mode and version:
8793 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8794 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8795 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8796 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8797 * sequence number).
8798 */
8799 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008800
8801 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008802#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008803 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008804#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8806 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008807#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008810 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008812 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008814
8815 /* Minimum length of encrypted record */
8816 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8817 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 } else
Valerio Settie5707042023-10-11 11:54:42 +02008819#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008820#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008822 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008824#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008826#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008827 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008828#endif /* MBEDTLS_USE_PSA_CRYPTO */
8829
8830#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008831 /* Get MAC length */
8832 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8833#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008834 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8836 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8837 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008838 goto end;
8839 }
8840
8841 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008843#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008844 transform->maclen = mac_key_len;
8845
8846 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008849#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008850 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008851#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008852
8853 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008855 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008857 /*
8858 * GenericBlockCipher:
8859 * 1. if EtM is in use: one block plus MAC
8860 * otherwise: * first multiple of blocklen greater than maclen
8861 * 2. IV
8862 */
8863#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008865 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 + block_size;
8867 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008868#endif
8869 {
8870 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 + block_size
8872 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008873 }
8874
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008876 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 } else {
8878 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008879 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8880 goto end;
8881 }
8882 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008883 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008884#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8885 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8887 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008888 }
8889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8891 (unsigned) keylen,
8892 (unsigned) transform->minlen,
8893 (unsigned) transform->ivlen,
8894 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008895
8896 /*
8897 * Finally setup the cipher contexts, IVs and MAC secrets.
8898 */
8899#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008901 key1 = keyblk + mac_key_len * 2;
8902 key2 = keyblk + mac_key_len * 2 + keylen;
8903
8904 mac_enc = keyblk;
8905 mac_dec = keyblk + mac_key_len;
8906
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 iv_copy_len = (transform->fixed_ivlen) ?
8908 transform->fixed_ivlen : transform->ivlen;
8909 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8910 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8911 iv_copy_len);
8912 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008913#endif /* MBEDTLS_SSL_CLI_C */
8914#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008916 key1 = keyblk + mac_key_len * 2 + keylen;
8917 key2 = keyblk + mac_key_len * 2;
8918
8919 mac_enc = keyblk + mac_key_len;
8920 mac_dec = keyblk;
8921
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 iv_copy_len = (transform->fixed_ivlen) ?
8923 transform->fixed_ivlen : transform->ivlen;
8924 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8925 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8926 iv_copy_len);
8927 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008928#endif /* MBEDTLS_SSL_SRV_C */
8929 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008931 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8932 goto end;
8933 }
8934
Paul Elliott4fb19552023-10-18 12:15:30 +01008935 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 ssl->f_export_keys(ssl->p_export_keys,
8937 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8938 master, 48,
8939 randbytes + 32,
8940 randbytes,
8941 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008942 }
8943
8944#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008945 transform->psa_alg = alg;
8946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8948 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8949 psa_set_key_algorithm(&attributes, alg);
8950 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 if ((status = psa_import_key(&attributes,
8953 key1,
8954 PSA_BITS_TO_BYTES(key_bits),
8955 &transform->psa_key_enc)) != PSA_SUCCESS) {
8956 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008957 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008959 goto end;
8960 }
8961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008963
Gilles Peskine449bd832023-01-11 14:50:10 +01008964 if ((status = psa_import_key(&attributes,
8965 key2,
8966 PSA_BITS_TO_BYTES(key_bits),
8967 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008968 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008969 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008970 goto end;
8971 }
8972 }
8973#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8975 cipher_info)) != 0) {
8976 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008977 goto end;
8978 }
8979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8981 cipher_info)) != 0) {
8982 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008983 goto end;
8984 }
8985
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8987 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8988 MBEDTLS_ENCRYPT)) != 0) {
8989 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008990 goto end;
8991 }
8992
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8994 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8995 MBEDTLS_DECRYPT)) != 0) {
8996 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008997 goto end;
8998 }
8999
9000#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
9002 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
9003 MBEDTLS_PADDING_NONE)) != 0) {
9004 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009005 goto end;
9006 }
9007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
9009 MBEDTLS_PADDING_NONE)) != 0) {
9010 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009011 goto end;
9012 }
9013 }
9014#endif /* MBEDTLS_CIPHER_MODE_CBC */
9015#endif /* MBEDTLS_USE_PSA_CRYPTO */
9016
Neil Armstrong29c0c042022-03-17 17:47:28 +01009017#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
9018 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
9019 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009021#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009023
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
9025 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
9026 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 if ((status = psa_import_key(&attributes,
9029 mac_enc, mac_key_len,
9030 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009031 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009033 goto end;
9034 }
9035
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
9037 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04009038#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04009040#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009042 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01009043 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
9044 PSA_KEY_USAGE_VERIFY_HASH);
9045 } else {
9046 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
9047 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009048
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 if ((status = psa_import_key(&attributes,
9050 mac_dec, mac_key_len,
9051 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009052 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009054 goto end;
9055 }
9056#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9058 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009059 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 }
9061 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9062 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009063 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009065#endif /* MBEDTLS_USE_PSA_CRYPTO */
9066 }
9067#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9068
9069 ((void) mac_dec);
9070 ((void) mac_enc);
9071
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009072end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9074 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009075}
9076
Valerio Settia08b1a42022-11-17 15:10:02 +01009077#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9078 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009079int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 psa_pake_operation_t *pake_ctx,
9081 const unsigned char *buf,
9082 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009083{
9084 psa_status_t status;
9085 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009086 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009087 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9088 * At round two perform a single cycle
9089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 for (; remaining_steps > 0; remaining_steps--) {
9093 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009094 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009096 /* Length is stored at the first byte */
9097 size_t length = buf[input_offset];
9098 input_offset += 1;
9099
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009101 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9102 }
9103
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 status = psa_pake_input(pake_ctx, step,
9105 buf + input_offset, length);
9106 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009107 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009108 }
9109
9110 input_offset += length;
9111 }
9112 }
9113
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009115 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009117
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009119}
9120
Valerio Setti6b3dab02022-11-17 17:14:54 +01009121int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 psa_pake_operation_t *pake_ctx,
9123 unsigned char *buf,
9124 size_t len, size_t *olen,
9125 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009126{
9127 psa_status_t status;
9128 size_t output_offset = 0;
9129 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009130 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009131 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9132 * At round two perform a single cycle
9133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 for (; remaining_steps > 0; remaining_steps--) {
9137 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9138 step <= PSA_PAKE_STEP_ZK_PROOF;
9139 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009140 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009141 * For each step, prepend 1 byte with the length of the data as
9142 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 status = psa_pake_output(pake_ctx, step,
9145 buf + output_offset + 1,
9146 len - output_offset - 1,
9147 &output_len);
9148 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009149 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009150 }
9151
Valerio Setti99d88c12022-11-22 16:03:43 +01009152 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009153
9154 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009155 }
9156 }
9157
9158 *olen = output_offset;
9159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009161}
Valerio Settia08b1a42022-11-17 15:10:02 +01009162#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9163
Jerry Yuee40f9d2022-02-17 14:55:16 +08009164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009165int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9166 unsigned char *hash, size_t *hashlen,
9167 unsigned char *data, size_t data_len,
9168 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009169{
9170 psa_status_t status;
9171 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009172 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009175
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 if ((status = psa_hash_setup(&hash_operation,
9177 hash_alg)) != PSA_SUCCESS) {
9178 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009179 goto exit;
9180 }
9181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9183 64)) != PSA_SUCCESS) {
9184 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009185 goto exit;
9186 }
9187
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 if ((status = psa_hash_update(&hash_operation,
9189 data, data_len)) != PSA_SUCCESS) {
9190 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009191 goto exit;
9192 }
9193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9195 hashlen)) != PSA_SUCCESS) {
9196 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9197 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009198 }
9199
9200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 if (status != PSA_SUCCESS) {
9202 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9203 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9204 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009205 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009207 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9208 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009210 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009211 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009212 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009214 }
9215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009217}
9218
9219#else
9220
Gilles Peskine449bd832023-01-11 14:50:10 +01009221int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9222 unsigned char *hash, size_t *hashlen,
9223 unsigned char *data, size_t data_len,
9224 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009225{
9226 int ret = 0;
9227 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9229 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009234
9235 /*
9236 * digitally-signed struct {
9237 * opaque client_random[32];
9238 * opaque server_random[32];
9239 * ServerDHParams params;
9240 * };
9241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9243 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009244 goto exit;
9245 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9247 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009248 goto exit;
9249 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9251 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009252 goto exit;
9253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9255 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009256 goto exit;
9257 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009260 goto exit;
9261 }
9262
9263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009265
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 if (ret != 0) {
9267 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9268 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9269 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009270
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009272}
9273#endif /* MBEDTLS_USE_PSA_CRYPTO */
9274
Jerry Yud9d91da2022-02-17 14:57:06 +08009275#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9276
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009277/* Find the preferred hash for a given signature algorithm. */
9278unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 mbedtls_ssl_context *ssl,
9280 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009281{
Gabor Mezei078e8032022-04-27 21:17:56 +02009282 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009283 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009284
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9286 return MBEDTLS_SSL_HASH_NONE;
9287 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009288
Gilles Peskine449bd832023-01-11 14:50:10 +01009289 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009290 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9292 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009293 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9295 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009296
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009297 mbedtls_md_type_t md_alg =
9298 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9299 if (md_alg == MBEDTLS_MD_NONE) {
9300 continue;
9301 }
9302
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009306 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009307 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009308
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9310 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9311 PSA_ALG_ECDSA(psa_hash_alg),
9312 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009313 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009315
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9317 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9318 PSA_ALG_RSA_PKCS1V15_SIGN(
9319 psa_hash_alg),
9320 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009321 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009323 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009324#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009325
Gilles Peskine449bd832023-01-11 14:50:10 +01009326 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009327 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009328 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009331}
9332
9333#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9334
Jerry Yudc7bd172022-02-17 13:44:15 +08009335#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9336
XiaokangQian75d40ef2022-04-20 11:05:24 +00009337int mbedtls_ssl_validate_ciphersuite(
9338 const mbedtls_ssl_context *ssl,
9339 const mbedtls_ssl_ciphersuite_t *suite_info,
9340 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009342{
9343 (void) ssl;
9344
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 if (suite_info == NULL) {
9346 return -1;
9347 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009348
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 if ((suite_info->min_tls_version > max_tls_version) ||
9350 (suite_info->max_tls_version < min_tls_version)) {
9351 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009352 }
9353
XiaokangQian060d8672022-04-21 09:24:56 +00009354#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009355#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009356#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009357 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9358 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009359#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9361 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009362#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009363 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009364 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009365 }
9366#endif
9367
9368 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9369#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9371 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9372 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009373 }
9374#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9375#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009378}
9379
Ronald Crone68ab4f2022-10-05 12:46:29 +02009380#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009381/*
9382 * Function for writing a signature algorithm extension.
9383 *
9384 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9385 * value (TLS 1.3 RFC8446):
9386 * enum {
9387 * ....
9388 * ecdsa_secp256r1_sha256( 0x0403 ),
9389 * ecdsa_secp384r1_sha384( 0x0503 ),
9390 * ecdsa_secp521r1_sha512( 0x0603 ),
9391 * ....
9392 * } SignatureScheme;
9393 *
9394 * struct {
9395 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9396 * } SignatureSchemeList;
9397 *
9398 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9399 * value (TLS 1.2 RFC5246):
9400 * enum {
9401 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9402 * sha512(6), (255)
9403 * } HashAlgorithm;
9404 *
9405 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9406 * SignatureAlgorithm;
9407 *
9408 * struct {
9409 * HashAlgorithm hash;
9410 * SignatureAlgorithm signature;
9411 * } SignatureAndHashAlgorithm;
9412 *
9413 * SignatureAndHashAlgorithm
9414 * supported_signature_algorithms<2..2^16-2>;
9415 *
9416 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9417 * generalization of the TLS 1.2 signature algorithm extension.
9418 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9419 * `SignatureScheme` field of TLS 1.3
9420 *
9421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009422int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9423 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009424{
9425 unsigned char *p = buf;
9426 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9427 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9428
9429 *out_len = 0;
9430
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009432
9433 /* Check if we have space for header and length field:
9434 * - extension_type (2 bytes)
9435 * - extension_data_length (2 bytes)
9436 * - supported_signature_algorithms_length (2 bytes)
9437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009439 p += 6;
9440
9441 /*
9442 * Write supported_signature_algorithms
9443 */
9444 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9446 if (sig_alg == NULL) {
9447 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9448 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9451 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9452 *sig_alg,
9453 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9454 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009455 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 }
9457 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9458 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009459 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9461 *sig_alg,
9462 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009463 }
9464
9465 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009466 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 if (supported_sig_alg_len == 0) {
9468 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9469 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009470 }
9471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9473 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9474 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009475
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009476 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009477
9478#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009480#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009483}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009484#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009485
XiaokangQian40a35232022-05-07 09:02:40 +00009486#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009487/*
9488 * mbedtls_ssl_parse_server_name_ext
9489 *
9490 * Structure of server_name extension:
9491 *
9492 * enum {
9493 * host_name(0), (255)
9494 * } NameType;
9495 * opaque HostName<1..2^16-1>;
9496 *
9497 * struct {
9498 * NameType name_type;
9499 * select (name_type) {
9500 * case host_name: HostName;
9501 * } name;
9502 * } ServerName;
9503 * struct {
9504 * ServerName server_name_list<1..2^16-1>
9505 * } ServerNameList;
9506 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009508int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9509 const unsigned char *buf,
9510 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009511{
9512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9513 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009514 size_t server_name_list_len, hostname_len;
9515 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009516
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009518
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9520 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009521 p += 2;
9522
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009524 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 while (p < server_name_list_end) {
9526 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9527 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9529 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009532 /* sni_name is intended to be used only during the parsing of the
9533 * ClientHello message (it is reset to NULL before the end of
9534 * the message parsing). Thus it is ok to just point to the
9535 * reception buffer and not make a copy of it.
9536 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009537 ssl->handshake->sni_name = p + 3;
9538 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 if (ssl->conf->f_sni == NULL) {
9540 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9543 ssl, p + 3, hostname_len);
9544 if (ret != 0) {
9545 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9546 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9547 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9548 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9549 }
9550 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009551 }
9552
9553 p += hostname_len + 3;
9554 }
9555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009557}
9558#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9559
XiaokangQianacb39922022-06-17 10:18:48 +00009560#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009561MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009562int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9563 const unsigned char *buf,
9564 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009565{
9566 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009567 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009568 const unsigned char *protocol_name_list;
9569 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009570 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009571
9572 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 if (ssl->conf->alpn_list == NULL) {
9574 return 0;
9575 }
XiaokangQianacb39922022-06-17 10:18:48 +00009576
9577 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009578 * RFC7301, section 3.1
9579 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009580 *
XiaokangQianc7403452022-06-23 03:24:12 +00009581 * struct {
9582 * ProtocolName protocol_name_list<2..2^16-1>
9583 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009584 */
9585
XiaokangQianc7403452022-06-23 03:24:12 +00009586 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009587 * protocol_name_list_len 2 bytes
9588 * protocol_name_len 1 bytes
9589 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009592
Gilles Peskine449bd832023-01-11 14:50:10 +01009593 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009594 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009596 protocol_name_list = p;
9597 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009598
9599 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009601 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9603 protocol_name_len);
9604 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009605 MBEDTLS_SSL_PEND_FATAL_ALERT(
9606 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9608 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009609 }
9610
9611 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009612 }
9613
9614 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9616 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009617 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009619 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 if (protocol_name_len == alpn_len &&
9621 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009622 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009624 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009625
9626 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009627 }
9628 }
9629
XiaokangQian95d5f542022-06-24 02:29:26 +00009630 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009631 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9633 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9634 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009635}
9636
Gilles Peskine449bd832023-01-11 14:50:10 +01009637int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9638 unsigned char *buf,
9639 unsigned char *end,
9640 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009641{
9642 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009643 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009644 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009645
Gilles Peskine449bd832023-01-11 14:50:10 +01009646 if (ssl->alpn_chosen == NULL) {
9647 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009648 }
9649
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 protocol_name_len = strlen(ssl->alpn_chosen);
9651 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009652
Gilles Peskine449bd832023-01-11 14:50:10 +01009653 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009654 /*
9655 * 0 . 1 ext identifier
9656 * 2 . 3 ext length
9657 * 4 . 5 protocol list length
9658 * 6 . 6 protocol name length
9659 * 7 . 7+n protocol name
9660 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009661 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009662
XiaokangQian95d5f542022-06-24 02:29:26 +00009663 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9666 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009667 /* Note: the length of the chosen protocol has been checked to be less
9668 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009673
9674#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009676#endif
9677
Gilles Peskine449bd832023-01-11 14:50:10 +01009678 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009679}
9680#endif /* MBEDTLS_SSL_ALPN */
9681
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009682#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009683 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009684 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009685 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009686int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9687 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009688{
9689 /* Initialize to suppress unnecessary compiler warning */
9690 size_t hostname_len = 0;
9691
9692 /* Check if new hostname is valid before
9693 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009694 if (hostname != NULL) {
9695 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009696
Gilles Peskine449bd832023-01-11 14:50:10 +01009697 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9698 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9699 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009700 }
9701
9702 /* Now it's clear that we will overwrite the old hostname,
9703 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009704 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009705 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009706 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009707 }
9708
9709 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009710 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009711 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009712 } else {
9713 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9714 if (session->hostname == NULL) {
9715 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9716 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009717
Gilles Peskine449bd832023-01-11 14:50:10 +01009718 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009719 }
9720
Gilles Peskine449bd832023-01-11 14:50:10 +01009721 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009722}
Xiaokang Qian03409292022-10-12 02:49:52 +00009723#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9724 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009725 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009726 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009727
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009728#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9729 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009730int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9731 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009732{
9733 size_t alpn_len = 0;
9734
9735 if (alpn != NULL) {
9736 alpn_len = strlen(alpn);
9737
9738 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9740 }
9741 }
9742
9743 if (session->ticket_alpn != NULL) {
9744 mbedtls_zeroize_and_free(session->ticket_alpn,
9745 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009746 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009747 }
9748
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009749 if (alpn != NULL) {
9750 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009751 if (session->ticket_alpn == NULL) {
9752 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9753 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009754 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009755 }
9756
9757 return 0;
9758}
9759#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009760
9761/*
9762 * The following functions are used by 1.2 and 1.3, client and server.
9763 */
9764#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9765int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9766 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9767 int recv_endpoint,
9768 mbedtls_ssl_protocol_version tls_version,
9769 uint32_t *flags)
9770{
9771 int ret = 0;
9772 unsigned int usage = 0;
9773 const char *ext_oid;
9774 size_t ext_len;
9775
9776 /*
9777 * keyUsage
9778 */
9779
9780 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9781 * to check what a compliant client will think while choosing which cert
9782 * to send to the client. */
9783#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9784 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9785 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9786 /* TLS 1.2 server part of the key exchange */
9787 switch (ciphersuite->key_exchange) {
9788 case MBEDTLS_KEY_EXCHANGE_RSA:
9789 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9790 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9791 break;
9792
9793 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9794 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9795 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9796 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9797 break;
9798
9799 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9800 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9801 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9802 break;
9803
9804 /* Don't use default: we want warnings when adding new values */
9805 case MBEDTLS_KEY_EXCHANGE_NONE:
9806 case MBEDTLS_KEY_EXCHANGE_PSK:
9807 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9808 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9809 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9810 usage = 0;
9811 }
9812 } else
9813#endif
9814 {
9815 /* This is either TLS 1.3 authentication, which always uses signatures,
9816 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9817 * options we implement, both using signatures. */
9818 (void) tls_version;
9819 (void) ciphersuite;
9820 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9821 }
9822
9823 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9824 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9825 ret = -1;
9826 }
9827
9828 /*
9829 * extKeyUsage
9830 */
9831
9832 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9833 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9834 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9835 } else {
9836 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9837 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9838 }
9839
9840 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9841 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9842 ret = -1;
9843 }
9844
9845 return ret;
9846}
9847
Gilles Peskinee61852e2025-02-12 23:28:48 +01009848static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
9849 const char **hostname)
9850{
9851 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
9852 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
Gilles Peskine2c33c752025-02-13 14:39:02 +01009853#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
9854 if (mbedtls_ssl_conf_get_endpoint(ssl->conf) == MBEDTLS_SSL_IS_CLIENT &&
9855 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9856 return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
9857 }
9858#endif
Gilles Peskinee61852e2025-02-12 23:28:48 +01009859 }
9860
9861 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
9862 if (*hostname == NULL) {
9863 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
9864 }
9865
9866 return 0;
9867}
9868
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009869int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9870 int authmode,
9871 mbedtls_x509_crt *chain,
9872 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9873 void *rs_ctx)
9874{
9875 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9876 return 0;
9877 }
9878
9879 /*
9880 * Primary check: use the appropriate X.509 verification function
9881 */
9882 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9883 void *p_vrfy;
9884 if (ssl->f_vrfy != NULL) {
9885 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9886 f_vrfy = ssl->f_vrfy;
9887 p_vrfy = ssl->p_vrfy;
9888 } else {
9889 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9890 f_vrfy = ssl->conf->f_vrfy;
9891 p_vrfy = ssl->conf->p_vrfy;
9892 }
9893
Gilles Peskinee61852e2025-02-12 23:28:48 +01009894 const char *hostname = "";
9895 int ret = get_hostname_for_verification(ssl, &hostname);
9896 if (ret != 0) {
9897 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
9898 return ret;
9899 }
9900
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009901 int have_ca_chain_or_callback = 0;
9902#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9903 if (ssl->conf->f_ca_cb != NULL) {
9904 ((void) rs_ctx);
9905 have_ca_chain_or_callback = 1;
9906
9907 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9908 ret = mbedtls_x509_crt_verify_with_ca_cb(
9909 chain,
9910 ssl->conf->f_ca_cb,
9911 ssl->conf->p_ca_cb,
9912 ssl->conf->cert_profile,
Gilles Peskinee61852e2025-02-12 23:28:48 +01009913 hostname,
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009914 &ssl->session_negotiate->verify_result,
9915 f_vrfy, p_vrfy);
9916 } else
9917#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9918 {
9919 mbedtls_x509_crt *ca_chain;
9920 mbedtls_x509_crl *ca_crl;
9921#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9922 if (ssl->handshake->sni_ca_chain != NULL) {
9923 ca_chain = ssl->handshake->sni_ca_chain;
9924 ca_crl = ssl->handshake->sni_ca_crl;
9925 } else
9926#endif
9927 {
9928 ca_chain = ssl->conf->ca_chain;
9929 ca_crl = ssl->conf->ca_crl;
9930 }
9931
9932 if (ca_chain != NULL) {
9933 have_ca_chain_or_callback = 1;
9934 }
9935
9936 ret = mbedtls_x509_crt_verify_restartable(
9937 chain,
9938 ca_chain, ca_crl,
9939 ssl->conf->cert_profile,
Gilles Peskinee61852e2025-02-12 23:28:48 +01009940 hostname,
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009941 &ssl->session_negotiate->verify_result,
9942 f_vrfy, p_vrfy, rs_ctx);
9943 }
9944
9945 if (ret != 0) {
9946 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9947 }
9948
9949#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9950 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9951 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9952 }
9953#endif
9954
9955 /*
9956 * Secondary checks: always done, but change 'ret' only if it was 0
9957 */
9958
9959 /* With TLS 1.2 and ECC certs, check that the curve used by the
9960 * certificate is on our list of acceptable curves.
9961 *
9962 * With TLS 1.3 this is not needed because the curve is part of the
9963 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9964 * we validate the signature made with the key associated to this cert.
9965 */
9966#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9967 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9968 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9969 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9970 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9971 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9972 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9973 if (ret == 0) {
9974 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9975 }
9976 }
9977 }
9978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9979
9980 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9981 if (mbedtls_ssl_check_cert_usage(chain,
9982 ciphersuite_info,
9983 ssl->conf->endpoint,
9984 ssl->tls_version,
9985 &ssl->session_negotiate->verify_result) != 0) {
9986 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9987 if (ret == 0) {
9988 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9989 }
9990 }
9991
9992 /* With authmode optional, we want to keep going if the certificate was
9993 * unacceptable, but still fail on other errors (out of memory etc),
9994 * including fatal errors from the f_vrfy callback.
9995 *
9996 * The only acceptable errors are:
9997 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9998 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9999 * Anything else is a fatal error. */
10000 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
10001 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
10002 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
10003 ret = 0;
10004 }
10005
10006 /* Return a specific error as this is a user error: inconsistent
10007 * configuration - can't verify without trust anchors. */
10008 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
10009 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
10010 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
10011 }
10012
10013 if (ret != 0) {
10014 uint8_t alert;
10015
10016 /* The certificate may have been rejected for several reasons.
10017 Pick one and send the corresponding alert. Which alert to send
10018 may be a subject of debate in some cases. */
10019 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
10020 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
10021 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
10022 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
10023 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
10024 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10025 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
10026 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10027 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
10028 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10029 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
10030 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10031 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
10032 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
10033 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
10034 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
10035 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
10036 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
10037 } else {
10038 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
10039 }
10040 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10041 alert);
10042 }
10043
10044#if defined(MBEDTLS_DEBUG_C)
10045 if (ssl->session_negotiate->verify_result != 0) {
10046 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
10047 (unsigned int) ssl->session_negotiate->verify_result));
10048 } else {
10049 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
10050 }
10051#endif /* MBEDTLS_DEBUG_C */
10052
10053 return ret;
10054}
10055#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
10056
Max Fillinger951b8862024-10-25 00:52:24 +020010057#if defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT)
Max Fillinger7b523282024-10-23 18:35:09 +020010058
Max Fillinger01182932024-09-21 11:06:28 +020010059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Max Fillinger44042f02024-07-22 14:43:56 +020010060static int mbedtls_ssl_tls12_export_keying_material(const mbedtls_ssl_context *ssl,
10061 const mbedtls_md_type_t hash_alg,
Max Fillinger9359f4d2024-09-21 10:48:57 +020010062 uint8_t *out,
10063 const size_t key_len,
10064 const char *label,
10065 const size_t label_len,
10066 const unsigned char *context,
10067 const size_t context_len,
Max Fillinger44042f02024-07-22 14:43:56 +020010068 const int use_context)
10069{
10070 int ret = 0;
Max Fillinger44042f02024-07-22 14:43:56 +020010071 unsigned char *prf_input = NULL;
Max Fillinger44042f02024-07-22 14:43:56 +020010072
Max Fillinger44042f02024-07-22 14:43:56 +020010073 /* The input to the PRF is client_random, then server_random.
10074 * If a context is provided, this is then followed by the context length
10075 * as a 16-bit big-endian integer, and then the context itself. */
Max Fillinger6c02ea82024-10-23 16:32:54 +020010076 const size_t randbytes_len = MBEDTLS_CLIENT_HELLO_RANDOM_LEN + MBEDTLS_SERVER_HELLO_RANDOM_LEN;
10077 size_t prf_input_len = randbytes_len;
Max Fillinger44042f02024-07-22 14:43:56 +020010078 if (use_context) {
Max Fillinger6c02ea82024-10-23 16:32:54 +020010079 if (context_len > UINT16_MAX) {
10080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10081 }
10082
10083 /* This does not overflow a 32-bit size_t because the current value of
10084 * prf_input_len is 64 (length of client_random + server_random) and
10085 * context_len fits into two bytes (checked above). */
10086 prf_input_len += sizeof(uint16_t) + context_len;
Max Fillinger44042f02024-07-22 14:43:56 +020010087 }
Max Fillinger6c02ea82024-10-23 16:32:54 +020010088
10089 prf_input = mbedtls_calloc(prf_input_len, sizeof(unsigned char));
10090 if (prf_input == NULL) {
10091 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
10092 }
10093
10094 memcpy(prf_input,
10095 ssl->transform->randbytes + MBEDTLS_SERVER_HELLO_RANDOM_LEN,
10096 MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
10097 memcpy(prf_input + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
10098 ssl->transform->randbytes,
10099 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
10100 if (use_context) {
10101 MBEDTLS_PUT_UINT16_BE(context_len, prf_input, randbytes_len);
10102 memcpy(prf_input + randbytes_len + sizeof(uint16_t), context, context_len);
10103 }
10104 ret = tls_prf_generic(hash_alg, ssl->session->master, sizeof(ssl->session->master),
Max Fillinger76077e22024-10-23 15:47:23 +020010105 label, label_len,
Max Fillinger44042f02024-07-22 14:43:56 +020010106 prf_input, prf_input_len,
10107 out, key_len);
Max Fillinger44042f02024-07-22 14:43:56 +020010108 mbedtls_free(prf_input);
Max Fillinger44042f02024-07-22 14:43:56 +020010109 return ret;
10110}
Max Fillinger7b523282024-10-23 18:35:09 +020010111#endif /* defined(MBEDTLS_SSL_PROTO_TLS1_2) */
Max Fillinger44042f02024-07-22 14:43:56 +020010112
Max Fillinger01182932024-09-21 11:06:28 +020010113#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Max Fillinger44042f02024-07-22 14:43:56 +020010114static int mbedtls_ssl_tls13_export_keying_material(mbedtls_ssl_context *ssl,
10115 const mbedtls_md_type_t hash_alg,
Max Fillinger9359f4d2024-09-21 10:48:57 +020010116 uint8_t *out,
10117 const size_t key_len,
10118 const char *label,
10119 const size_t label_len,
10120 const unsigned char *context,
10121 const size_t context_len)
Max Fillinger44042f02024-07-22 14:43:56 +020010122{
10123 const psa_algorithm_t psa_hash_alg = mbedtls_md_psa_alg_from_type(hash_alg);
10124 const size_t hash_len = PSA_HASH_LENGTH(hash_alg);
10125 const unsigned char *secret = ssl->session->app_secrets.exporter_master_secret;
10126
Max Fillinger76bb7532024-11-21 12:33:46 +010010127 /* The length of the label must be at most 249 bytes to fit into the HkdfLabel
Max Fillinger8ee21412024-11-01 16:05:34 +010010128 * struct as defined in RFC 8446, Section 7.1.
Max Fillingere95edbf2024-10-29 19:18:54 +010010129 *
Max Fillinger8ee21412024-11-01 16:05:34 +010010130 * The length of the context is unlimited even though the context field in the
Max Fillinger76bb7532024-11-21 12:33:46 +010010131 * struct can only hold up to 255 bytes. This is because we place a *hash* of
Max Fillinger8ee21412024-11-01 16:05:34 +010010132 * the context in the field. */
Max Fillinger76bb7532024-11-21 12:33:46 +010010133 if (label_len > 249) {
Max Fillinger44042f02024-07-22 14:43:56 +020010134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10135 }
10136
10137 return mbedtls_ssl_tls13_exporter(psa_hash_alg, secret, hash_len,
Max Fillinger9359f4d2024-09-21 10:48:57 +020010138 (const unsigned char *) label, label_len,
Max Fillinger44042f02024-07-22 14:43:56 +020010139 context, context_len, out, key_len);
10140}
Max Fillinger7b523282024-10-23 18:35:09 +020010141#endif /* defined(MBEDTLS_SSL_PROTO_TLS1_3) */
Max Fillinger44042f02024-07-22 14:43:56 +020010142
10143int mbedtls_ssl_export_keying_material(mbedtls_ssl_context *ssl,
Max Fillinger3be83a72024-08-14 16:44:50 +020010144 uint8_t *out, const size_t key_len,
10145 const char *label, const size_t label_len,
10146 const unsigned char *context, const size_t context_len,
10147 const int use_context)
Max Fillinger44042f02024-07-22 14:43:56 +020010148{
10149 if (!mbedtls_ssl_is_handshake_over(ssl)) {
Max Fillinger97a28792024-11-18 18:22:51 +010010150 /* TODO: Change this to a more appropriate error code when one is available. */
Max Fillinger44042f02024-07-22 14:43:56 +020010151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10152 }
10153
Max Fillinger8ee21412024-11-01 16:05:34 +010010154 if (key_len > MBEDTLS_SSL_EXPORT_MAX_KEY_LEN) {
10155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
10156 }
10157
Max Fillinger44042f02024-07-22 14:43:56 +020010158 int ciphersuite_id = mbedtls_ssl_get_ciphersuite_id_from_ssl(ssl);
10159 const mbedtls_ssl_ciphersuite_t *ciphersuite = mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
10160 const mbedtls_md_type_t hash_alg = ciphersuite->mac;
10161
10162 switch (mbedtls_ssl_get_version_number(ssl)) {
Max Fillinger01182932024-09-21 11:06:28 +020010163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Max Fillinger44042f02024-07-22 14:43:56 +020010164 case MBEDTLS_SSL_VERSION_TLS1_2:
10165 return mbedtls_ssl_tls12_export_keying_material(ssl, hash_alg, out, key_len,
10166 label, label_len,
10167 context, context_len, use_context);
Max Fillinger01182932024-09-21 11:06:28 +020010168#endif
10169#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Max Fillinger44042f02024-07-22 14:43:56 +020010170 case MBEDTLS_SSL_VERSION_TLS1_3:
Max Fillinger9359f4d2024-09-21 10:48:57 +020010171 return mbedtls_ssl_tls13_export_keying_material(ssl,
10172 hash_alg,
10173 out,
10174 key_len,
10175 label,
10176 label_len,
Max Fillinger44042f02024-07-22 14:43:56 +020010177 use_context ? context : NULL,
10178 use_context ? context_len : 0);
Max Fillinger01182932024-09-21 11:06:28 +020010179#endif
Max Fillinger44042f02024-07-22 14:43:56 +020010180 default:
10181 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
10182 }
10183}
10184
Max Fillinger951b8862024-10-25 00:52:24 +020010185#endif /* defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) */
Max Fillinger7b523282024-10-23 18:35:09 +020010186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187#endif /* MBEDTLS_SSL_TLS_C */