blob: f7554d202d8800bbbc28428bd7e8b009320c4662 [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"
Andrzej Kurek25f27152022-08-17 16:09:31 -040022
Valerio Settib4f50762024-01-17 10:24:52 +010023#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000024#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010026#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020027#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050031#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010032#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020033#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050035#include "psa/crypto.h"
36#endif
37
Janos Follath23bdca02016-10-07 14:47:14 +010038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020040#endif
41
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040043/* Define local translating functions to save code size by not using too many
44 * arguments in each translating place. */
45static int local_err_translation(psa_status_t status)
46{
47 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040048 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040049 psa_generic_status_to_mbedtls);
50}
51#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050052#endif
53
Ronald Cronad8c17b2022-06-10 17:18:09 +020054#if defined(MBEDTLS_TEST_HOOKS)
55static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
56
57void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010058 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020059{
60 chk_buf_ptr_fail_args.cur = cur;
61 chk_buf_ptr_fail_args.end = end;
62 chk_buf_ptr_fail_args.need = need;
63}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020066{
Gilles Peskine449bd832023-01-11 14:50:10 +010067 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020068}
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020071{
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return (chk_buf_ptr_fail_args.cur != args->cur) ||
73 (chk_buf_ptr_fail_args.end != args->end) ||
74 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020075}
76#endif /* MBEDTLS_TEST_HOOKS */
77
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010079
Hanno Beckera0e20d02019-05-15 14:03:01 +010080#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010081/* Top-level Connection ID API */
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
84 size_t len,
85 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
88 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
89 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
93 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010094 }
95
96 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010097 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
102 int enable,
103 unsigned char const *own_cid,
104 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
108 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (enable == MBEDTLS_SSL_CID_DISABLED) {
112 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
113 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
116 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (own_cid_len != ssl->conf->cid_len) {
119 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
120 (unsigned) own_cid_len,
121 (unsigned) ssl->conf->cid_len));
122 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100126 /* Truncation is not an issue here because
127 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
128 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100131}
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
134 int *enabled,
Sam Berry9722fd12024-06-11 14:34:17 +0100135 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000137{
138 *enabled = MBEDTLS_SSL_CID_DISABLED;
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
142 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000143
144 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
145 * zero as this is indistinguishable from not requesting to use
146 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
148 return 0;
149 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000152 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (own_cid != NULL) {
154 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
155 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
166 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
171 mbedtls_ssl_is_handshake_over(ssl) == 0) {
172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100173 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100174
Hanno Beckerc5f24222019-05-03 12:54:52 +0100175 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
176 * were used, but client and server requested the empty CID.
177 * This is indistinguishable from not using the CID extension
178 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (ssl->transform_in->in_cid_len == 0 &&
180 ssl->transform_in->out_cid_len == 0) {
181 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100185 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (peer_cid != NULL) {
187 memcpy(peer_cid, ssl->transform_in->out_cid,
188 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100189 }
190 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100191
192 *enabled = MBEDTLS_SSL_CID_ENABLED;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100195}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200201/*
202 * Convert max_fragment_length codes to length.
203 * RFC 6066 says:
204 * enum{
205 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
206 * } MaxFragmentLength;
207 * and we add 0 -> extension unused
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200210{
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 switch (mfl) {
212 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
213 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
214 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
215 return 512;
216 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
217 return 1024;
218 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
219 return 2048;
220 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
221 return 4096;
222 default:
223 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000224 }
225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
229 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_ssl_session_free(dst);
232 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800233#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
234 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000235#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
236 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000237 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800238#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000239#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800240
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000241#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
242 defined(MBEDTLS_SSL_EARLY_DATA)
243 dst->ticket_alpn = NULL;
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
253 if (dst->peer_cert == NULL) {
254 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
255 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
260 src->peer_cert->raw.len)) != 0) {
261 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200262 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264 }
265 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000266#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 mbedtls_calloc(1, src->peer_cert_digest_len);
270 if (dst->peer_cert_digest == NULL) {
271 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
272 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
275 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000276 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000277 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000278 }
279#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200282
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000283#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
284 defined(MBEDTLS_SSL_EARLY_DATA)
285 {
286 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
287 if (ret != 0) {
288 return ret;
289 }
290 }
291#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
292
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200293#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (src->ticket != NULL) {
295 dst->ticket = mbedtls_calloc(1, src->ticket_len);
296 if (dst->ticket == NULL) {
297 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
298 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000302
303#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
304 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
308 if (ret != 0) {
309 return ret;
310 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000311 }
312#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
313 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200314#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200317}
318
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200320MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
324 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800325 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500327
328 /* We want to copy len_new bytes when downsizing the buffer, and
329 * len_old bytes when upsizing, so we choose the smaller of two sizes,
330 * to fit one buffer into another. Size checks, ensuring that no data is
331 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 memcpy(resized_buffer, *buffer,
333 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100334 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500335
336 *buffer = resized_buffer;
337 *len_old = len_new;
338
339 return 0;
340}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
343 size_t in_buf_new_len,
344 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345{
346 int modified = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000347 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000349 size_t hshdr_in = 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;
355 if (ssl->in_hshdr != NULL) {
356 hshdr_in = ssl->in_hshdr - ssl->in_buf;
357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200359 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 ssl->in_buf_len < in_buf_new_len) {
361 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
362 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
363 } else {
364 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
365 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 modified = 1;
367 }
368 }
369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 written_out = ssl->out_msg - ssl->out_buf;
373 iv_offset_out = ssl->out_iv - ssl->out_buf;
374 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200376 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ssl->out_buf_len < out_buf_new_len) {
378 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
379 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
380 } else {
381 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
382 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 modified = 1;
384 }
385 }
386 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200388 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000389 ssl->in_hdr = ssl->in_buf + hdr_in;
390 mbedtls_ssl_update_in_pointers(ssl);
391 mbedtls_ssl_reset_out_pointers(ssl);
392
Andrzej Kurek4a063792020-10-21 15:08:44 +0200393 /* Fields below might not be properly updated with record
394 * splitting or with CID, so they are manually updated here. */
395 ssl->out_msg = ssl->out_buf + written_out;
396 ssl->out_len = ssl->out_buf + len_offset_out;
397 ssl->out_iv = ssl->out_buf + iv_offset_out;
398
399 ssl->in_msg = ssl->in_buf + written_in;
400 ssl->in_len = ssl->in_buf + len_offset_in;
401 ssl->in_iv = ssl->in_buf + iv_offset_in;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000402 if (ssl->in_hshdr != NULL) {
403 ssl->in_hshdr = ssl->in_buf + hshdr_in;
404 }
Andrzej Kurek4a063792020-10-21 15:08:44 +0200405 }
406}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500407#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
408
Jerry Yudb8c48a2022-01-27 14:54:54 +0800409#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800410
411#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
413 const char *label,
414 const unsigned char *random, size_t rlen,
415 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800418
419#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
420
421/* Type for the TLS PRF */
422typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
423 const unsigned char *, size_t,
424 unsigned char *, size_t);
425
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200426MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100427static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
428 int ciphersuite,
429 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200430#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200432#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 ssl_tls_prf_t tls_prf,
434 const unsigned char randbytes[64],
435 mbedtls_ssl_protocol_version tls_version,
436 unsigned endpoint,
437 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800438
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100439#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200440MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300442 const char *label,
443 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100445static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
446static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100447
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100448#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100449
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100450#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100451MBEDTLS_CHECK_RETURN_CRITICAL
452static int tls_prf_sha384(const unsigned char *secret, size_t slen,
453 const char *label,
454 const unsigned char *random, size_t rlen,
455 unsigned char *dstbuf, size_t dlen);
456
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100457static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
458static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100459#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461MBEDTLS_CHECK_RETURN_CRITICAL
462static int ssl_tls12_session_load(mbedtls_ssl_session *session,
463 const unsigned char *buf,
464 size_t len);
465#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
466
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100467static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100468
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100469#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100470static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100471#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100472
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100473#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100474static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100475#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100476
477int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
478 const unsigned char *secret, size_t slen,
479 const char *label,
480 const unsigned char *random, size_t rlen,
481 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482{
483 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100487#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 case MBEDTLS_SSL_TLS_PRF_SHA384:
489 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100491#endif /* MBEDTLS_MD_CAN_SHA384*/
492#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 case MBEDTLS_SSL_TLS_PRF_SHA256:
494 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100496#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 default:
499 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300503}
504
Jerry Yuc73c6182022-02-08 20:29:25 +0800505#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100506static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800507{
508#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (session->peer_cert != NULL) {
510 mbedtls_x509_crt_free(session->peer_cert);
511 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800512 session->peer_cert = NULL;
513 }
514#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800516 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800518 session->peer_cert_digest = NULL;
519 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
520 session->peer_cert_digest_len = 0;
521 }
522#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
523}
524#endif /* MBEDTLS_X509_CRT_PARSE_C */
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800527{
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800529 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800531
532 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800534
535 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800537
538 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800540
541 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800543
544 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800546
547 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800549
550 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800552
553 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800555
556 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800558
559 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800561
562 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800564
565 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800567
568 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800570
571 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800573
574 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800576
577 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800579
580 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800582
583 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800585
586 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800588
589 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800591
592 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800594
595 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800597
598 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800600
601 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800603
604 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800606
Jan Bruckner151f6422023-02-10 12:45:19 +0100607 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
608 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
609
Jerry Yu7a485c12022-10-31 13:08:18 +0800610 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800612
613 }
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800616}
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800619{
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800621}
622
Jerry Yud25cab02022-10-31 12:48:30 +0800623#if defined(MBEDTLS_DEBUG_C)
624static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800625 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800626 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
627 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
628 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
629 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
630 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
631 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
632 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
633 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
634 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
635 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
636 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
637 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
638 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
639 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
640 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
641 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
642 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
643 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
644 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
645 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
646 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
647 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
648 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
649 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
650 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
651 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100652 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
653 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800654};
655
Chien Wong4e9683e2023-12-28 17:07:43 +0800656static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800657 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
658 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
659 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
660 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
661 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
662 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
663 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
664 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
665 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
666 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
667 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
668 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
669 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
670 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
671 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
672 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
673 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
674 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
675 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
676 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
677 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
678 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
679 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
680 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
681 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
682 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
683 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100684 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
685 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800686};
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800689{
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return extension_name_table[
691 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800692}
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800695{
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800701 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800703 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800705 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800707 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800709 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800713}
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
716 int level, const char *file, int line,
717 int hs_msg_type, unsigned int extension_type,
718 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800719{
720 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800722 mbedtls_debug_print_msg(
723 ssl, level, file, line,
724 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 ssl_tls13_get_hs_msg_name(hs_msg_type),
726 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800727 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800729 return;
730 }
731
732 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800734 mbedtls_debug_print_msg(
735 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
737 mbedtls_ssl_get_extension_name(extension_type), extension_type,
738 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800739 return;
740 }
741
742 mbedtls_debug_print_msg(
743 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
745 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800746}
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 int hs_msg_type, uint32_t extensions_mask,
751 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800752{
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 for (unsigned i = 0;
755 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
756 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800757 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800758 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800760 }
761}
762
Pengyu Lvee455c02023-01-12 14:37:24 +0800763#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800764static const char *ticket_flag_name_table[] =
765{
766 [0] = "ALLOW_PSK_RESUMPTION",
767 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
768 [3] = "ALLOW_EARLY_DATA",
769};
770
Pengyu Lv4938a562023-01-16 11:28:49 +0800771void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
772 int level, const char *file, int line,
773 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800774{
775 size_t i;
776
777 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800778 "print ticket_flags (0x%02x)", flags);
779
780 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800781
782 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800783 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800784 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
785 ticket_flag_name_table[i]);
786 }
787 }
788}
789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
790
Jerry Yud25cab02022-10-31 12:48:30 +0800791#endif /* MBEDTLS_DEBUG_C */
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
794 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800795{
796 ((void) ciphersuite_info);
797
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100798#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800800 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800802#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100803#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800805 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800807#endif
808 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800810 return;
811 }
812}
813
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 unsigned hs_type,
816 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817{
818 unsigned char hs_hdr[4];
819
820 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
822 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
823 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
824 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100825
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100826 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100827}
828
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100829int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100830 unsigned hs_type,
831 unsigned char const *msg,
832 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100833{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100834 int ret;
835 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100836 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100837 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100838 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100839 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100840}
841
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100842int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800843{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100844#if defined(MBEDTLS_MD_CAN_SHA256) || \
845 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100846#if defined(MBEDTLS_USE_PSA_CRYPTO)
847 psa_status_t status;
848#else
849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100851#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800852 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100853#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100854#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200858 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 }
860 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200862 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100863 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800864#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100865 mbedtls_md_free(&ssl->handshake->fin_sha256);
866 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100867 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869 0);
870 if (ret != 0) {
871 return ret;
872 }
873 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 if (ret != 0) {
875 return ret;
876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#endif
878#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100879#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100881 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200883 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100884 }
885 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200887 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100890 mbedtls_md_free(&ssl->handshake->fin_sha384);
891 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100892 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894 if (ret != 0) {
895 return ret;
896 }
897 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100898 if (ret != 0) {
899 return ret;
900 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800901#endif
902#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100903 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800904}
905
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100906static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100907 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800908{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100909#if defined(MBEDTLS_MD_CAN_SHA256) || \
910 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911#if defined(MBEDTLS_USE_PSA_CRYPTO)
912 psa_status_t status;
913#else
914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100916#else /* SHA-256 or SHA-384 */
917 ((void) ssl);
918 (void) buf;
919 (void) len;
920#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100921#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100923 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200925 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100926 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100928 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 if (ret != 0) {
930 return ret;
931 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#endif
933#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100934#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800935#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100936 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200938 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100939 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800940#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100941 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100942 if (ret != 0) {
943 return ret;
944 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100947 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800948}
949
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100950#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100951static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100952 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800953{
954#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200955 return mbedtls_md_error_from_psa(psa_hash_update(
956 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800957#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100958 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800959#endif
960}
961#endif
962
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100963#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100964static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100965 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800966{
967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200968 return mbedtls_md_error_from_psa(psa_hash_update(
969 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800970#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100971 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800972#endif
973}
974#endif
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100980#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#if defined(MBEDTLS_USE_PSA_CRYPTO)
982 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500983#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100984 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500986#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100987#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500988#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500989 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500990#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100991 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500993#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200994
995 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200999#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02001000#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02001001 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001003#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001004#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006 handshake->psa_pake_ctx = psa_pake_operation_init();
1007 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1008#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001010#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001011#if defined(MBEDTLS_SSL_CLI_C)
1012 handshake->ecjpake_cache = NULL;
1013 handshake->ecjpake_cache_len = 0;
1014#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001015#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001016
Gilles Peskineeccd8882020-03-10 12:19:08 +01001017#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001019#endif
1020
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001021#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1022 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1023#endif
Hanno Becker75173122019-02-06 16:18:31 +00001024
1025#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1026 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001028#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001029}
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001032{
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001034
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001035#if defined(MBEDTLS_USE_PSA_CRYPTO)
1036 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1037 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001038#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1040 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001041#endif
1042
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001043#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001044#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1046 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001047#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_md_init(&transform->md_ctx_enc);
1049 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001050#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001051#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001052}
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001055{
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001057}
1058
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001059MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001060static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001061{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1063
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001064 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (ssl->transform_negotiate) {
1067 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1068 }
Jerry Yu2e199812022-12-01 18:57:19 +08001069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (ssl->session_negotiate) {
1071 mbedtls_ssl_session_free(ssl->session_negotiate);
1072 }
1073 if (ssl->handshake) {
1074 mbedtls_ssl_handshake_free(ssl);
1075 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001076
Jerry Yu2e199812022-12-01 18:57:19 +08001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001078 /*
1079 * Either the pointers are now NULL or cleared properly and can be freed.
1080 * Now allocate missing structures.
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (ssl->transform_negotiate == NULL) {
1083 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001084 }
Jerry Yu2e199812022-12-01 18:57:19 +08001085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl->session_negotiate == NULL) {
1088 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001089 }
Paul Bakker48916f92012-09-16 19:57:18 +00001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ssl->handshake == NULL) {
1092 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001093 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001094#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1095 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1098 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001099#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001100
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001101 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001104 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001105#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 ssl->session_negotiate == NULL) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001110 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001111
1112#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001114 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001115#endif
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001118 ssl->session_negotiate = NULL;
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001121 }
1122
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001123#if defined(MBEDTLS_SSL_EARLY_DATA)
1124#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001125 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001126#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001127#if defined(MBEDTLS_SSL_SRV_C)
1128 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1129#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001130 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001131#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001132
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001133 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_ssl_session_init(ssl->session_negotiate);
1135 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001136
Jerry Yu2e199812022-12-01 18:57:19 +08001137#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001139#endif
1140
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001141 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001142 ret = mbedtls_ssl_reset_checksum(ssl);
1143 if (ret != 0) {
1144 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1145 return ret;
1146 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001147
Jerry Yud0766ec2022-09-22 10:46:57 +08001148#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1149 defined(MBEDTLS_SSL_SRV_C) && \
1150 defined(MBEDTLS_SSL_SESSION_TICKETS)
1151 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001153#endif
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001157 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001160 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001162 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001166 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001167#endif
1168
Brett Warrene0edc842021-08-17 09:53:13 +01001169/*
1170 * curve_list is translated to IANA TLS group identifiers here because
1171 * mbedtls_ssl_conf_curves returns void and so can't return
1172 * any error codes.
1173 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001174#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001175#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1176 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001178 size_t length;
1179 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1180
Thomas Daubney850a0792023-05-22 12:05:03 +01001181 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
Brett Warrene0edc842021-08-17 09:53:13 +01001183
1184 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1186 if (group_list == NULL) {
1187 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1188 }
Brett Warrene0edc842021-08-17 09:53:13 +01001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001191 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 curve_list[i]);
1193 if (tls_id == 0) {
1194 mbedtls_free(group_list);
1195 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001196 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001197 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001198 }
1199
1200 group_list[length] = 0;
1201
1202 ssl->handshake->group_list = group_list;
1203 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001205 ssl->handshake->group_list = ssl->conf->group_list;
1206 ssl->handshake->group_list_heap_allocated = 0;
1207 }
1208#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001209#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001210
Ronald Crone68ab4f2022-10-05 12:46:29 +02001211#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001212#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001214 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1215 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1217 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001218 const int *md;
1219 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001220 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 uint16_t *p;
1222
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001223 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1224 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1225 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1228 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001229 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001231#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001233#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001234
Jerry Yub476a442022-01-21 18:14:45 +08001235#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001237#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1239 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1240 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001241 }
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1244 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1245 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1248 sizeof(uint16_t));
1249 if (ssl->handshake->sig_algs == NULL) {
1250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1251 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 p = (uint16_t *) ssl->handshake->sig_algs;
1254 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1255 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1256 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001257 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001259#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001261 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001262#endif
1263#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001265 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001266#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001267 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001268 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001269 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001272 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001273 ssl->handshake->sig_algs_heap_allocated = 0;
1274 }
Jerry Yucc539102022-06-27 16:27:35 +08001275#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001276#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001278}
1279
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001280#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001281/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001283static int ssl_cookie_write_dummy(void *ctx,
1284 unsigned char **p, unsigned char *end,
1285 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286{
1287 ((void) ctx);
1288 ((void) p);
1289 ((void) end);
1290 ((void) cli_id);
1291 ((void) cli_id_len);
1292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001294}
1295
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001297static int ssl_cookie_check_dummy(void *ctx,
1298 const unsigned char *cookie, size_t cookie_len,
1299 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001300{
1301 ((void) ctx);
1302 ((void) cookie);
1303 ((void) cookie_len);
1304 ((void) cli_id);
1305 ((void) cli_id_len);
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001308}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001309#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001310
Paul Bakker5121ce52009-01-03 21:22:43 +00001311/*
1312 * Initialize an SSL context
1313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001315{
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001317}
1318
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001321{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001322 const mbedtls_ssl_config *conf = ssl->conf;
1323
Ronald Cron6f135e12021-12-08 16:57:54 +01001324#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1326 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1327 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1328 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001329 }
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1332 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001333 }
1334#endif
1335
1336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1338 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1339 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001340 }
1341#endif
1342
Ronald Cron6f135e12021-12-08 16:57:54 +01001343#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1345 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1346 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1347 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001348 }
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1351 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001352 }
1353#endif
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1356 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001357}
1358
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001359MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001360static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1361{
1362 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 ret = ssl_conf_version_check(ssl);
1364 if (ret != 0) {
1365 return ret;
1366 }
Jerry Yu60835a82021-08-04 10:13:52 +08001367
Yanray Wangb422cab2023-12-01 16:18:10 +08001368 if (ssl->conf->f_rng == NULL) {
1369 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1370 return MBEDTLS_ERR_SSL_NO_RNG;
1371 }
1372
Jerry Yu60835a82021-08-04 10:13:52 +08001373 /* Space for further checks */
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001376}
1377
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001378/*
1379 * Setup an SSL context
1380 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1383 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Janos Follath865b3eb2019-12-16 11:46:15 +00001385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001389 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if ((ret = ssl_conf_check(ssl)) != 0) {
1392 return ret;
1393 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001394 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001395
Paul Bakker62f2dee2012-09-28 07:31:51 +00001396 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001397 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001398 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001399
1400 /* Set to NULL in case of an error condition */
1401 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001402
Darryl Greenb33cc762019-11-28 14:29:44 +00001403#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1404 ssl->in_buf_len = in_buf_len;
1405#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1407 if (ssl->in_buf == NULL) {
1408 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001409 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001410 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001411 }
1412
Darryl Greenb33cc762019-11-28 14:29:44 +00001413#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1414 ssl->out_buf_len = out_buf_len;
1415#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1417 if (ssl->out_buf == NULL) {
1418 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001419 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001420 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001423 mbedtls_ssl_reset_in_pointers(ssl);
1424 mbedtls_ssl_reset_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001425
Johan Pascalb62bb512015-12-03 21:56:45 +01001426#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001428#endif
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001431 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001435
1436error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 mbedtls_free(ssl->in_buf);
1438 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001439
1440 ssl->conf = NULL;
1441
Darryl Greenb33cc762019-11-28 14:29:44 +00001442#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1443 ssl->in_buf_len = 0;
1444 ssl->out_buf_len = 0;
1445#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001446 ssl->in_buf = NULL;
1447 ssl->out_buf = NULL;
1448
1449 ssl->in_hdr = NULL;
1450 ssl->in_ctr = NULL;
1451 ssl->in_len = NULL;
1452 ssl->in_iv = NULL;
1453 ssl->in_msg = NULL;
1454
1455 ssl->out_hdr = NULL;
1456 ssl->out_ctr = NULL;
1457 ssl->out_len = NULL;
1458 ssl->out_iv = NULL;
1459 ssl->out_msg = NULL;
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001462}
1463
1464/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001465 * Reset an initialized and used SSL context for re-use while retaining
1466 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001467 *
1468 * If partial is non-zero, keep data in the input buffer and client ID.
1469 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1472 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001473{
Darryl Greenb33cc762019-11-28 14:29:44 +00001474#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1475 size_t in_buf_len = ssl->in_buf_len;
1476 size_t out_buf_len = ssl->out_buf_len;
1477#else
1478 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1479 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1480#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001481
Hanno Beckerb0302c42021-08-03 09:39:42 +01001482#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1483 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001484#endif
1485
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001486 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001488
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001489 mbedtls_ssl_reset_in_pointers(ssl);
1490 mbedtls_ssl_reset_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001491
1492 /* Reset incoming message parsing */
1493 ssl->in_offt = NULL;
1494 ssl->nb_zero = 0;
1495 ssl->in_msgtype = 0;
1496 ssl->in_msglen = 0;
1497 ssl->in_hslen = 0;
1498 ssl->keep_current_message = 0;
1499 ssl->transform_in = NULL;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00001500 ssl->in_hshdr = NULL;
1501 ssl->in_hsfraglen = 0;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001502
1503#if defined(MBEDTLS_SSL_PROTO_DTLS)
1504 ssl->next_record_offset = 0;
1505 ssl->in_epoch = 0;
1506#endif
1507
1508 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001512 }
1513
Ronald Cronad8c17b2022-06-10 17:18:09 +02001514 ssl->send_alert = 0;
1515
Hanno Beckerb0302c42021-08-03 09:39:42 +01001516 /* Reset outgoing message writing */
1517 ssl->out_msgtype = 0;
1518 ssl->out_msglen = 0;
1519 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 memset(ssl->out_buf, 0, out_buf_len);
1521 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001522 ssl->transform_out = NULL;
1523
1524#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001526#endif
1527
XiaokangQian2b01dc32022-01-21 02:53:13 +00001528#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ssl->transform) {
1530 mbedtls_ssl_transform_free(ssl->transform);
1531 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001532 ssl->transform = NULL;
1533 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001534#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1535
XiaokangQian2b01dc32022-01-21 02:53:13 +00001536#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 mbedtls_ssl_transform_free(ssl->transform_application);
1538 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001539 ssl->transform_application = NULL;
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001542#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1544 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001545 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001546#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1549 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001550 ssl->handshake->transform_handshake = NULL;
1551 }
1552
XiaokangQian2b01dc32022-01-21 02:53:13 +00001553#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554}
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001557{
1558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1559
1560 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001561 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001564
1565 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_RENEGOTIATION)
1567 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001568 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001569
1570 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1572 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001575
Hanno Beckerb0302c42021-08-03 09:39:42 +01001576 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001577 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ssl->session) {
1579 mbedtls_ssl_session_free(ssl->session);
1580 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001581 ssl->session = NULL;
1582 }
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001585 ssl->alpn_chosen = NULL;
1586#endif
1587
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001588#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001589 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001590#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001592#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (free_cli_id) {
1594 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595 ssl->cli_id = NULL;
1596 ssl->cli_id_len = 0;
1597 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001598#endif
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if ((ret = ssl_handshake_init(ssl)) != 0) {
1601 return ret;
1602 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001605}
1606
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001607/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001608 * Reset an initialized and used SSL context for re-use while retaining
1609 * all application-set variables, function pointers and data.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001612{
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001614}
1615
1616/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 * SSL set accessors
1618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001621 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622}
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001626 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001627}
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001631{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001632 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001633}
1634#endif
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001638 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001639}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1644 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001645{
1646 ssl->disable_datagram_packing = !allow_packing;
1647}
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1650 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001651{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001652 conf->hs_timeout_min = min;
1653 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001654}
1655#endif
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001659 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1664 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1665 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001667 conf->f_vrfy = f_vrfy;
1668 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1673 int (*f_rng)(void *, unsigned char *, size_t),
1674 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001675{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001676 conf->f_rng = f_rng;
1677 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678}
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1681 void (*f_dbg)(void *, int, const char *, int, const char *),
1682 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001683{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001684 conf->f_dbg = f_dbg;
1685 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001686}
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1689 void *p_bio,
1690 mbedtls_ssl_send_t *f_send,
1691 mbedtls_ssl_recv_t *f_recv,
1692 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001693{
1694 ssl->p_bio = p_bio;
1695 ssl->f_send = f_send;
1696 ssl->f_recv = f_recv;
1697 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001698}
1699
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001701void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001702{
1703 ssl->mtu = mtu;
1704}
1705#endif
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001708{
1709 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001710}
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1713 void *p_timer,
1714 mbedtls_ssl_set_timer_t *f_set_timer,
1715 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001716{
1717 ssl->p_timer = p_timer;
1718 ssl->f_set_timer = f_set_timer;
1719 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001720
1721 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001723}
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001726void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1727 void *p_cache,
1728 mbedtls_ssl_cache_get_t *f_get_cache,
1729 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001730{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001731 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001732 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001733 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001738int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001739{
Janos Follath865b3eb2019-12-16 11:46:15 +00001740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001743 session == NULL ||
1744 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001747 }
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (ssl->handshake->resume == 1) {
1750 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1751 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001752
Jerry Yu21092062022-10-10 21:21:31 +08001753#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron8d630842024-04-04 14:05:21 +02001755#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001756 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001760 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1762 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1763 session->ciphersuite));
1764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001765 }
Ronald Cron8d630842024-04-04 14:05:21 +02001766#else
1767 /*
1768 * If session tickets are not enabled, it is not possible to resume a
1769 * TLS 1.3 session, thus do not make any change to the SSL context in
1770 * the first place.
1771 */
1772 return 0;
1773#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001774 }
Jerry Yu21092062022-10-10 21:21:31 +08001775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1778 session)) != 0) {
1779 return ret;
1780 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001781
Paul Bakker0a597072012-09-25 21:55:46 +00001782 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1789 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001790{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001791 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001792}
1793
Ronald Cron6f135e12021-12-08 16:57:54 +01001794#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1796 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001797{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001798 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001799}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001800
1801#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001802void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1803 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001804{
1805 conf->early_data_enabled = early_data_enabled;
1806}
Jerry Yucc4e0072022-11-22 17:22:22 +08001807
1808#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001809void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001811{
Jerry Yu39da9852022-12-06 16:58:36 +08001812 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001813}
1814#endif /* MBEDTLS_SSL_SRV_C */
1815
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001816#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001817#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001820void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1821 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001822{
1823 conf->cert_profile = profile;
1824}
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001827{
1828 mbedtls_ssl_key_cert *cur = key_cert, *next;
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001831 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001833 cur = next;
1834 }
1835}
1836
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001837/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001838MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001839static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1840 mbedtls_x509_crt *cert,
1841 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001842{
niisato8ee24222018-06-25 19:05:48 +09001843 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001846 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001848 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001850 }
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1853 if (new_cert == NULL) {
1854 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1855 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001856
niisato8ee24222018-06-25 19:05:48 +09001857 new_cert->cert = cert;
1858 new_cert->key = key;
1859 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001860
Glenn Strauss36872db2022-01-22 05:06:31 -05001861 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001863 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001867 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 }
niisato8ee24222018-06-25 19:05:48 +09001869 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001870 }
1871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001873}
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001876 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001878{
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001880}
1881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001883 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001885{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001886 conf->ca_chain = ca_chain;
1887 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001888
1889#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1890 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1891 * cannot be used together. */
1892 conf->f_ca_cb = NULL;
1893 conf->p_ca_cb = NULL;
1894#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001895}
Hanno Becker5adaad92019-03-27 16:54:37 +00001896
1897#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001898void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1899 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1900 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001901{
1902 conf->f_ca_cb = f_ca_cb;
1903 conf->p_ca_cb = p_ca_cb;
1904
1905 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1906 * cannot be used together. */
1907 conf->ca_chain = NULL;
1908 conf->ca_crl = NULL;
1909}
1910#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001912
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001913#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001914const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1915 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001916{
1917 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001919}
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1922 mbedtls_x509_crt *own_cert,
1923 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001924{
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1926 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001927}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1930 mbedtls_x509_crt *ca_chain,
1931 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001932{
1933 ssl->handshake->sni_ca_chain = ca_chain;
1934 ssl->handshake->sni_ca_crl = ca_crl;
1935}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001936
Glenn Strauss999ef702022-03-11 01:37:23 -05001937#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001938void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1939 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001940{
1941 ssl->handshake->dn_hints = crt;
1942}
1943#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1946 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001947{
1948 ssl->handshake->sni_authmode = authmode;
1949}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001950#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1951
Hanno Becker8927c832019-04-03 12:52:50 +01001952#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001953void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1954 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1955 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001956{
1957 ssl->f_vrfy = f_vrfy;
1958 ssl->p_vrfy = p_vrfy;
1959}
1960#endif
1961
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001962#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001963
Valerio Setti016f6822022-12-09 14:17:50 +01001964#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001965static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1966static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1967
Valerio Setti016f6822022-12-09 14:17:50 +01001968static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 mbedtls_ssl_context *ssl,
1970 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001971{
1972 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001973 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001974 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001975 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001976 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001977 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1979 psa_pake_cs_set_primitive(&cipher_suite,
1980 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1981 PSA_ECC_FAMILY_SECP_R1,
1982 256));
1983 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1986 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001987 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001991 user = jpake_server_id;
1992 user_len = sizeof(jpake_server_id);
1993 peer = jpake_client_id;
1994 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001996 user = jpake_client_id;
1997 user_len = sizeof(jpake_client_id);
1998 peer = jpake_server_id;
1999 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002001
Przemek Stekiel4cd20312023-03-01 11:11:28 +01002002 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2003 if (status != PSA_SUCCESS) {
2004 return status;
2005 }
2006
2007 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002009 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 }
Valerio Setti016f6822022-12-09 14:17:50 +01002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2013 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002014 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 }
Valerio Setti016f6822022-12-09 14:17:50 +01002016
2017 ssl->handshake->psa_pake_ctx_is_ok = 1;
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002020}
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2023 const unsigned char *pw,
2024 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002025{
2026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2027 psa_status_t status;
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ssl->handshake == NULL || ssl->conf == NULL) {
2030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002031 }
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 /* Empty password is not valid */
2034 if ((pw == NULL) || (pw_len == 0)) {
2035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2036 }
2037
2038 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2039 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2040 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2041
2042 status = psa_import_key(&attributes, pw, pw_len,
2043 &ssl->handshake->psa_pake_password);
2044 if (status != PSA_SUCCESS) {
2045 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2046 }
2047
2048 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2049 ssl->handshake->psa_pake_password);
2050 if (status != PSA_SUCCESS) {
2051 psa_destroy_key(ssl->handshake->psa_pake_password);
2052 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2053 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2054 }
2055
2056 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002057}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2060 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002061{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002062 psa_status_t status;
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ssl->handshake == NULL || ssl->conf == NULL) {
2065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002066 }
2067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (mbedtls_svc_key_id_is_null(pwd)) {
2069 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2070 }
2071
2072 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2073 if (status != PSA_SUCCESS) {
2074 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2075 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2076 }
2077
2078 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002079}
2080#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2082 const unsigned char *pw,
2083 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002084{
2085 mbedtls_ecjpake_role role;
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (ssl->handshake == NULL || ssl->conf == NULL) {
2088 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2089 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002090
Valerio Settic689ed82022-12-07 14:40:38 +01002091 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if ((pw == NULL) || (pw_len == 0)) {
2093 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2094 }
Valerio Settic689ed82022-12-07 14:40:38 +01002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002097 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002099 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002101
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2103 role,
2104 MBEDTLS_MD_SHA256,
2105 MBEDTLS_ECP_DP_SECP256R1,
2106 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002107}
Valerio Setti02c25b52022-11-15 14:08:42 +01002108#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002109#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002110
Ronald Cron73fe8df2022-10-05 14:31:43 +02002111#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002112int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002113{
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (conf->psk_identity == NULL ||
2115 conf->psk_identity_len == 0) {
2116 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002117 }
2118
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002119#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2121 return 1;
2122 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002123#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (conf->psk != NULL && conf->psk_len != 0) {
2126 return 1;
2127 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002130}
2131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002133{
2134 /* Remove reference to existing PSK, if any. */
2135#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002137 /* The maintenance of the PSK key slot is the
2138 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002140 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002143 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002144 conf->psk = NULL;
2145 conf->psk_len = 0;
2146 }
2147
2148 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (conf->psk_identity != NULL) {
2150 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002151 conf->psk_identity = NULL;
2152 conf->psk_identity_len = 0;
2153 }
2154}
2155
Hanno Becker7390c712018-11-15 13:33:04 +00002156/* This function assumes that PSK identity in the SSL config is unset.
2157 * It checks that the provided identity is well-formed and attempts
2158 * to make a copy of it in the SSL config.
2159 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002161static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2162 unsigned char const *psk_identity,
2163 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002164{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002165 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002167 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 (psk_identity_len >> 16) != 0 ||
2169 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002171 }
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2174 if (conf->psk_identity == NULL) {
2175 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2176 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002177
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002178 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002182}
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2185 const unsigned char *psk, size_t psk_len,
2186 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002187{
Janos Follath865b3eb2019-12-16 11:46:15 +00002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002189
2190 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2192 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2193 }
Hanno Becker7390c712018-11-15 13:33:04 +00002194
2195 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (psk == NULL) {
2197 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2198 }
2199 if (psk_len == 0) {
2200 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2201 }
2202 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2203 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2204 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2207 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2208 }
Hanno Becker7390c712018-11-15 13:33:04 +00002209 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002211
2212 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2214 if (ret != 0) {
2215 ssl_conf_remove_psk(conf);
2216 }
Hanno Becker7390c712018-11-15 13:33:04 +00002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002219}
2220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002222{
2223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002225 /* The maintenance of the external PSK key slot is the
2226 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 if (ssl->handshake->psk_opaque_is_internal) {
2228 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002229 ssl->handshake->psk_opaque_is_internal = 0;
2230 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002231 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002232 }
Neil Armstronge952a302022-05-03 10:22:14 +02002233#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002235 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002237 ssl->handshake->psk_len = 0;
lhuang046d4d94f2024-06-11 12:37:02 -07002238 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002239 }
Neil Armstronge952a302022-05-03 10:22:14 +02002240#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002241}
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2244 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002245{
Neil Armstrong501c9322022-05-03 09:35:09 +02002246#if defined(MBEDTLS_USE_PSA_CRYPTO)
2247 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002248 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002249 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002251#endif /* MBEDTLS_USE_PSA_CRYPTO */
2252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if (psk == NULL || ssl->handshake == NULL) {
2254 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2255 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2258 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2259 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002262
Neil Armstrong501c9322022-05-03 09:35:09 +02002263#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002264#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2266 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2267 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2268 } else {
2269 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2270 }
2271 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002272 }
2273#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002274
Jerry Yu568ec252022-07-22 21:27:34 +08002275#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2277 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2278 psa_set_key_usage_flags(&key_attributes,
2279 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002280 }
2281#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 psa_set_key_algorithm(&key_attributes, alg);
2284 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2287 if (status != PSA_SUCCESS) {
2288 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2289 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002290
2291 /* Allow calling psa_destroy_key() on psk remove */
2292 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002294#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2296 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2297 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002298
2299 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002303#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002304}
2305
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002306#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002307int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2308 mbedtls_svc_key_id_t psk,
2309 const unsigned char *psk_identity,
2310 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002311{
Janos Follath865b3eb2019-12-16 11:46:15 +00002312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002313
2314 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2316 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2317 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002318
Hanno Becker7390c712018-11-15 13:33:04 +00002319 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 if (mbedtls_svc_key_id_is_null(psk)) {
2321 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2322 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002323 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002324
2325 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2327 psk_identity_len);
2328 if (ret != 0) {
2329 ssl_conf_remove_psk(conf);
2330 }
Hanno Becker7390c712018-11-15 13:33:04 +00002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002333}
2334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2336 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002337{
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if ((mbedtls_svc_key_id_is_null(psk)) ||
2339 (ssl->handshake == NULL)) {
2340 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2341 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002344 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002346}
2347#endif /* MBEDTLS_USE_PSA_CRYPTO */
2348
Jerry Yu8897c072022-08-12 13:56:53 +08002349#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002350void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2351 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2352 size_t),
2353 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002354{
2355 conf->f_psk = f_psk;
2356 conf->p_psk = p_psk;
2357}
Jerry Yu8897c072022-08-12 13:56:53 +08002358#endif /* MBEDTLS_SSL_SRV_C */
2359
Ronald Cron73fe8df2022-10-05 14:31:43 +02002360#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002361
2362#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002363static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002366#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if (alg == PSA_ALG_CBC_NO_PADDING) {
2368 return MBEDTLS_SSL_MODE_CBC;
2369 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002370#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (PSA_ALG_IS_AEAD(alg)) {
2372 return MBEDTLS_SSL_MODE_AEAD;
2373 }
2374 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002375}
2376
2377#else /* MBEDTLS_USE_PSA_CRYPTO */
2378
2379static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002381{
2382#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (mode == MBEDTLS_MODE_CBC) {
2384 return MBEDTLS_SSL_MODE_CBC;
2385 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002386#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2387
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002388#if defined(MBEDTLS_GCM_C) || \
2389 defined(MBEDTLS_CCM_C) || \
2390 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002392 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 mode == MBEDTLS_MODE_CHACHAPOLY) {
2394 return MBEDTLS_SSL_MODE_AEAD;
2395 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002396#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002399}
Gilles Peskine301711e2022-04-26 16:57:05 +02002400#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002401
Gilles Peskinee108d982022-04-26 16:50:40 +02002402static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2403 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002405{
2406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2408 base_mode == MBEDTLS_SSL_MODE_CBC) {
2409 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002410 }
2411#else
2412 (void) encrypt_then_mac;
2413#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002415}
2416
Neil Armstrongab555e02022-04-04 11:07:59 +02002417mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002419{
Gilles Peskinee108d982022-04-26 16:50:40 +02002420 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002421#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002423#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002425#endif
2426 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002427
Gilles Peskinee108d982022-04-26 16:50:40 +02002428 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002429#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002430 encrypt_then_mac = transform->encrypt_then_mac;
2431#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002433}
2434
Neil Armstrongab555e02022-04-04 11:07:59 +02002435mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002436#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002438#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002440{
Gilles Peskinee108d982022-04-26 16:50:40 +02002441 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2442
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002443#if defined(MBEDTLS_USE_PSA_CRYPTO)
2444 psa_status_t status;
2445 psa_algorithm_t alg;
2446 psa_key_type_t type;
2447 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002448 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2449 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (status == PSA_SUCCESS) {
2451 base_mode = mbedtls_ssl_get_base_mode(alg);
2452 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002453#else
2454 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002455 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002457 base_mode =
2458 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002460 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002461#endif /* MBEDTLS_USE_PSA_CRYPTO */
2462
Gilles Peskinee108d982022-04-26 16:50:40 +02002463#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2464 int encrypt_then_mac = 0;
2465#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002467}
2468
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002469#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2472 size_t taglen,
2473 psa_algorithm_t *alg,
2474 psa_key_type_t *key_type,
2475 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002476{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002477#if !defined(MBEDTLS_SSL_HAVE_CCM)
2478 (void) taglen;
2479#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002481#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002482 case MBEDTLS_CIPHER_AES_128_CBC:
2483 *alg = PSA_ALG_CBC_NO_PADDING;
2484 *key_type = PSA_KEY_TYPE_AES;
2485 *key_size = 128;
2486 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002487#endif
2488#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002489 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002491 *key_type = PSA_KEY_TYPE_AES;
2492 *key_size = 128;
2493 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002494#endif
2495#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002496 case MBEDTLS_CIPHER_AES_128_GCM:
2497 *alg = PSA_ALG_GCM;
2498 *key_type = PSA_KEY_TYPE_AES;
2499 *key_size = 128;
2500 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002501#endif
2502#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002503 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002505 *key_type = PSA_KEY_TYPE_AES;
2506 *key_size = 192;
2507 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002508#endif
2509#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002510 case MBEDTLS_CIPHER_AES_192_GCM:
2511 *alg = PSA_ALG_GCM;
2512 *key_type = PSA_KEY_TYPE_AES;
2513 *key_size = 192;
2514 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002515#endif
2516#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002517 case MBEDTLS_CIPHER_AES_256_CBC:
2518 *alg = PSA_ALG_CBC_NO_PADDING;
2519 *key_type = PSA_KEY_TYPE_AES;
2520 *key_size = 256;
2521 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002522#endif
2523#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002524 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002526 *key_type = PSA_KEY_TYPE_AES;
2527 *key_size = 256;
2528 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002529#endif
2530#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002531 case MBEDTLS_CIPHER_AES_256_GCM:
2532 *alg = PSA_ALG_GCM;
2533 *key_type = PSA_KEY_TYPE_AES;
2534 *key_size = 256;
2535 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002536#endif
2537#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002538 case MBEDTLS_CIPHER_ARIA_128_CBC:
2539 *alg = PSA_ALG_CBC_NO_PADDING;
2540 *key_type = PSA_KEY_TYPE_ARIA;
2541 *key_size = 128;
2542 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002543#endif
2544#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002545 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002547 *key_type = PSA_KEY_TYPE_ARIA;
2548 *key_size = 128;
2549 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002550#endif
2551#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002552 case MBEDTLS_CIPHER_ARIA_128_GCM:
2553 *alg = PSA_ALG_GCM;
2554 *key_type = PSA_KEY_TYPE_ARIA;
2555 *key_size = 128;
2556 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002557#endif
2558#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002559 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002561 *key_type = PSA_KEY_TYPE_ARIA;
2562 *key_size = 192;
2563 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002564#endif
2565#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002566 case MBEDTLS_CIPHER_ARIA_192_GCM:
2567 *alg = PSA_ALG_GCM;
2568 *key_type = PSA_KEY_TYPE_ARIA;
2569 *key_size = 192;
2570 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002571#endif
2572#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002573 case MBEDTLS_CIPHER_ARIA_256_CBC:
2574 *alg = PSA_ALG_CBC_NO_PADDING;
2575 *key_type = PSA_KEY_TYPE_ARIA;
2576 *key_size = 256;
2577 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002578#endif
2579#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002580 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002582 *key_type = PSA_KEY_TYPE_ARIA;
2583 *key_size = 256;
2584 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002585#endif
2586#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002587 case MBEDTLS_CIPHER_ARIA_256_GCM:
2588 *alg = PSA_ALG_GCM;
2589 *key_type = PSA_KEY_TYPE_ARIA;
2590 *key_size = 256;
2591 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002592#endif
2593#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002594 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2595 *alg = PSA_ALG_CBC_NO_PADDING;
2596 *key_type = PSA_KEY_TYPE_CAMELLIA;
2597 *key_size = 128;
2598 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002599#endif
2600#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002601 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002603 *key_type = PSA_KEY_TYPE_CAMELLIA;
2604 *key_size = 128;
2605 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002606#endif
2607#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002608 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2609 *alg = PSA_ALG_GCM;
2610 *key_type = PSA_KEY_TYPE_CAMELLIA;
2611 *key_size = 128;
2612 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002613#endif
2614#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002615 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002617 *key_type = PSA_KEY_TYPE_CAMELLIA;
2618 *key_size = 192;
2619 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002620#endif
2621#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002622 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2623 *alg = PSA_ALG_GCM;
2624 *key_type = PSA_KEY_TYPE_CAMELLIA;
2625 *key_size = 192;
2626 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002627#endif
2628#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002629 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2630 *alg = PSA_ALG_CBC_NO_PADDING;
2631 *key_type = PSA_KEY_TYPE_CAMELLIA;
2632 *key_size = 256;
2633 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002634#endif
2635#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002636 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002638 *key_type = PSA_KEY_TYPE_CAMELLIA;
2639 *key_size = 256;
2640 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002641#endif
2642#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002643 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_CAMELLIA;
2646 *key_size = 256;
2647 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002648#endif
2649#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002650 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2651 *alg = PSA_ALG_CHACHA20_POLY1305;
2652 *key_type = PSA_KEY_TYPE_CHACHA20;
2653 *key_size = 256;
2654 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002655#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002656 case MBEDTLS_CIPHER_NULL:
2657 *alg = MBEDTLS_SSL_NULL_CIPHER;
2658 *key_type = 0;
2659 *key_size = 0;
2660 break;
2661 default:
2662 return PSA_ERROR_NOT_SUPPORTED;
2663 }
2664
2665 return PSA_SUCCESS;
2666}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002667#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002668
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002669#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002670int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2671 const unsigned char *dhm_P, size_t P_len,
2672 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002673{
Janos Follath865b3eb2019-12-16 11:46:15 +00002674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 mbedtls_mpi_free(&conf->dhm_P);
2677 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2680 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2681 mbedtls_mpi_free(&conf->dhm_P);
2682 mbedtls_mpi_free(&conf->dhm_G);
2683 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002684 }
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002687}
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002690{
Janos Follath865b3eb2019-12-16 11:46:15 +00002691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 mbedtls_mpi_free(&conf->dhm_P);
2694 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2697 &conf->dhm_P)) != 0 ||
2698 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2699 &conf->dhm_G)) != 0) {
2700 mbedtls_mpi_free(&conf->dhm_P);
2701 mbedtls_mpi_free(&conf->dhm_G);
2702 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002703 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002706}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002707#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002708
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002709#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2710/*
2711 * Set the minimum length for Diffie-Hellman parameters
2712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2714 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002715{
2716 conf->dhm_min_bitlen = bitlen;
2717}
2718#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2719
Ronald Crone68ab4f2022-10-05 12:46:29 +02002720#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002721#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002722/*
2723 * Set allowed/preferred hashes for handshake signatures
2724 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002725void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2726 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002727{
2728 conf->sig_hashes = hashes;
2729}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002730#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002731
Jerry Yuf017ee42022-01-12 15:49:48 +08002732/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2734 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002735{
Jerry Yuf017ee42022-01-12 15:49:48 +08002736#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2737 conf->sig_hashes = NULL;
2738#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2739 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002740}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002741#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002742
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002743#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002744#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002745/*
2746 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002747 *
2748 * mbedtls_ssl_setup() takes the provided list
2749 * and translates it to a list of IANA TLS group identifiers,
2750 * stored in ssl->handshake->group_list.
2751 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002752 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002753void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2754 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002755{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002756 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002757 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002758}
Brett Warrene0edc842021-08-17 09:53:13 +01002759#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002760#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002761
Brett Warrene0edc842021-08-17 09:53:13 +01002762/*
2763 * Set the allowed groups
2764 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002765void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2766 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002767{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002768#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002769 conf->curve_list = NULL;
2770#endif
2771 conf->group_list = group_list;
2772}
2773
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002774#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002775int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002776{
Hanno Becker947194e2017-04-07 13:25:49 +01002777 /* Initialize to suppress unnecessary compiler warning */
2778 size_t hostname_len = 0;
2779
2780 /* Check if new hostname is valid before
2781 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (hostname != NULL) {
2783 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2786 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2787 }
Hanno Becker947194e2017-04-07 13:25:49 +01002788 }
2789
2790 /* Now it's clear that we will overwrite the old hostname,
2791 * so we can free it safely */
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002794 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002795 }
2796
2797 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002800 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 } else {
2802 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2803 if (ssl->hostname == NULL) {
2804 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2805 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002808
Hanno Becker947194e2017-04-07 13:25:49 +01002809 ssl->hostname[hostname_len] = '\0';
2810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002813}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002814#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002815
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002816#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002817void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2818 int (*f_sni)(void *, mbedtls_ssl_context *,
2819 const unsigned char *, size_t),
2820 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002821{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002822 conf->f_sni = f_sni;
2823 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002828int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002829{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002830 size_t cur_len, tot_len;
2831 const char **p;
2832
2833 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002834 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2835 * MUST NOT be truncated."
2836 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002837 */
2838 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 for (p = protos; *p != NULL; p++) {
2840 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002841 tot_len += cur_len;
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 if ((cur_len == 0) ||
2844 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2845 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2846 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2847 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002848 }
2849
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002850 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002853}
2854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002856{
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002859#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002860
Johan Pascalb62bb512015-12-03 21:56:45 +01002861#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002862void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2863 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002864{
2865 conf->dtls_srtp_mki_support = support_mki_value;
2866}
2867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2869 unsigned char *mki_value,
2870 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002871{
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2873 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002874 }
Ron Eldor591f1622018-01-22 12:30:04 +02002875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2877 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002878 }
Ron Eldor591f1622018-01-22 12:30:04 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002881 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002883}
2884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2886 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002887{
Johan Pascal253d0262020-09-22 13:04:45 +02002888 const mbedtls_ssl_srtp_profile *p;
2889 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002890
Johan Pascal253d0262020-09-22 13:04:45 +02002891 /* check the profiles list: all entry must be valid,
2892 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2894 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2895 p++) {
2896 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002897 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002899 /* unsupported value, stop parsing and set the size to an error value */
2900 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002901 }
2902 }
2903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2905 conf->dtls_srtp_profile_list = NULL;
2906 conf->dtls_srtp_profile_list_len = 0;
2907 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002908 }
2909
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002910 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002911 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002914}
2915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2917 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002918{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002919 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2920 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002922 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002924 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2926 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002927 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002928}
Johan Pascalb62bb512015-12-03 21:56:45 +01002929#endif /* MBEDTLS_SSL_DTLS_SRTP */
2930
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302931#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002932void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002933{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002934 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002935}
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002938{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002939 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002940}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302941#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002942
Janos Follath088ce432017-04-10 12:42:31 +01002943#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002944void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2945 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002946{
2947 conf->cert_req_ca_list = cert_req_ca_list;
2948}
2949#endif
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002952void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002953{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002954 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002955}
2956#endif
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002959void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002960{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002961 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002962}
2963#endif
2964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002966int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002967{
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2969 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2970 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002971 }
2972
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002973 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002980{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002981 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002982}
2983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002985void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002986{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002987 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002988}
2989
Gilles Peskine449bd832023-01-11 14:50:10 +01002990void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002992 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002993}
2994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2996 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002997{
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003003#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02003004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003006{
Ronald Crond67f8012024-08-28 07:45:57 +02003007 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3008 conf->session_tickets |= (use_tickets != 0) <<
3009 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003010}
Ronald Crond67f8012024-08-28 07:45:57 +02003011
Ronald Cronbedddd72024-08-27 14:18:50 +02003012#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003013void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3014 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003015{
Ronald Crond67f8012024-08-28 07:45:57 +02003016 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003017 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003018 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3019}
Ronald Cronbedddd72024-08-27 14:18:50 +02003020#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3021#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003022
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003023#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003024
Jerry Yud0766ec2022-09-22 10:46:57 +08003025#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003026void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3027 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003028{
Jerry Yud0766ec2022-09-22 10:46:57 +08003029 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003030}
3031#endif
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3034 mbedtls_ssl_ticket_write_t *f_ticket_write,
3035 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3036 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003037{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003038 conf->f_ticket_write = f_ticket_write;
3039 conf->f_ticket_parse = f_ticket_parse;
3040 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003041}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3046 mbedtls_ssl_export_keys_t *f_export_keys,
3047 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003048{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003049 ssl->f_export_keys = f_export_keys;
3050 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003051}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003052
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003053#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003054void mbedtls_ssl_conf_async_private_cb(
3055 mbedtls_ssl_config *conf,
3056 mbedtls_ssl_async_sign_t *f_async_sign,
3057 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3058 mbedtls_ssl_async_resume_t *f_async_resume,
3059 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003061{
3062 conf->f_async_sign_start = f_async_sign;
3063 conf->f_async_decrypt_start = f_async_decrypt;
3064 conf->f_async_resume = f_async_resume;
3065 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003066 conf->p_async_config_data = async_config_data;
3067}
3068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003070{
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003072}
3073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003075{
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 if (ssl->handshake == NULL) {
3077 return NULL;
3078 } else {
3079 return ssl->handshake->user_async_ctx;
3080 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003081}
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3084 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003085{
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003087 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003089}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003090#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003091
Paul Bakker5121ce52009-01-03 21:22:43 +00003092/*
3093 * SSL get accessors
3094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003095uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003096{
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 if (ssl->session != NULL) {
3098 return ssl->session->verify_result;
3099 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (ssl->session_negotiate != NULL) {
3102 return ssl->session_negotiate->verify_result;
3103 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003106}
3107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003109{
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (ssl == NULL || ssl->session == NULL) {
3111 return 0;
3112 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003115}
3116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003118{
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (ssl == NULL || ssl->session == NULL) {
3120 return NULL;
3121 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003124}
3125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3130 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003131 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003133 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003135 }
3136 }
3137#endif
3138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003140 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003142 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003144 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003146 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003147}
3148
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003149#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3150
3151size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3152{
3153 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3154 size_t record_size_limit = max_len;
3155
3156 if (ssl->session != NULL &&
3157 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3158 ssl->session->record_size_limit < max_len) {
3159 record_size_limit = ssl->session->record_size_limit;
3160 }
3161
3162 // TODO: this is currently untested
3163 /* During a handshake, use the value being negotiated */
3164 if (ssl->session_negotiate != NULL &&
3165 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3166 ssl->session_negotiate->record_size_limit < max_len) {
3167 record_size_limit = ssl->session_negotiate->record_size_limit;
3168 }
3169
3170 return record_size_limit;
3171}
3172#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3173
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003174#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003175size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003176{
David Horstmann95d516f2021-05-04 18:36:56 +01003177 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003178 size_t read_mfl;
3179
Jerry Yuddda0502022-12-01 19:43:12 +08003180#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003181 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3183 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3184 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003185 }
Jerry Yuddda0502022-12-01 19:43:12 +08003186#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003187
3188 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 if (ssl->session_out != NULL) {
3190 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3191 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003192 max_len = read_mfl;
3193 }
3194 }
3195
Jerry Yuddda0502022-12-01 19:43:12 +08003196 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 if (ssl->session_negotiate != NULL) {
3198 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3199 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003200 max_len = read_mfl;
3201 }
3202 }
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003205}
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003208{
3209 size_t max_len;
3210
3211 /*
3212 * Assume mfl_code is correct since it was checked when set
3213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003215
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003216 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 if (ssl->session_out != NULL &&
3218 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3219 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003220 }
3221
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003222 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 if (ssl->session_negotiate != NULL &&
3224 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3225 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003226 }
3227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003229}
3230#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3231
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003233size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003234{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003235 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3237 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3238 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3239 return 0;
3240 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3243 return ssl->mtu;
3244 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->mtu == 0) {
3247 return ssl->handshake->mtu;
3248 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 return ssl->mtu < ssl->handshake->mtu ?
3251 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003252}
3253#endif /* MBEDTLS_SSL_PROTO_DTLS */
3254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003256{
3257 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3258
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003259#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003260 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003261 !defined(MBEDTLS_SSL_PROTO_DTLS)
3262 (void) ssl;
3263#endif
3264
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003265#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003269 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003271#endif
3272
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003273#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3274 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3275
3276 if (max_len > record_size_limit) {
3277 max_len = record_size_limit;
3278 }
3279#endif
3280
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003281 if (ssl->transform_out != NULL &&
3282 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003283 /*
3284 * In TLS 1.3 case, when records are protected, `max_len` as computed
3285 * above is the maximum length of the TLSInnerPlaintext structure that
3286 * along the plaintext payload contains the inner content type (one byte)
3287 * and some zero padding. Given the algorithm used for padding
3288 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3289 * the plaintext payload. Round down to a multiple of
3290 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3291 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003292 */
3293 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3294 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3295 }
3296
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3299 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3300 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003301 const size_t overhead = (size_t) ret;
3302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (ret < 0) {
3304 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003305 }
3306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 if (mtu <= overhead) {
3308 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3309 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3310 }
3311
3312 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003315 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003316#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003317
Hanno Becker0defedb2018-08-10 12:35:02 +01003318#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003319 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3320 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003321 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322#endif
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003325}
3326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003328{
3329 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3330
3331#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3332 (void) ssl;
3333#endif
3334
3335#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003339 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003341#endif
3342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003344}
3345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003347const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003348{
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ssl == NULL || ssl->session == NULL) {
3350 return NULL;
3351 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003352
Hanno Beckere6824572019-02-07 13:18:46 +00003353#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003355#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003357#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003362int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3363 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003364{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003365 int ret;
3366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003368 dst == NULL ||
3369 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3371 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003372 }
3373
Hanno Beckere810bbc2021-05-14 16:01:05 +01003374 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3375 * idempotent: Each session can only be exported once.
3376 *
3377 * (This is in preparation for TLS 1.3 support where we will
3378 * need the ability to export multiple sessions (aka tickets),
3379 * which will be achieved by calling mbedtls_ssl_get_session()
3380 * multiple times until it fails.)
3381 *
3382 * Check whether we have already exported the current session,
3383 * and fail if so.
3384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 if (ssl->session->exported == 1) {
3386 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3387 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3390 if (ret != 0) {
3391 return ret;
3392 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003393
3394 /* Remember that we've exported the session. */
3395 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003399
David Horstmanncb01b362024-02-29 17:31:46 +00003400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3401
3402/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003403 *
David Horstmanncb01b362024-02-29 17:31:46 +00003404 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003405 */
3406static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3407 unsigned char *buf,
3408 size_t buf_len)
3409{
3410 unsigned char *p = buf;
3411 size_t used = 0;
3412
3413#if defined(MBEDTLS_HAVE_TIME)
3414 uint64_t start;
3415#endif
3416#if defined(MBEDTLS_X509_CRT_PARSE_C)
3417#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3418 size_t cert_len;
3419#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3420#endif /* MBEDTLS_X509_CRT_PARSE_C */
3421
3422 /*
3423 * Time
3424 */
3425#if defined(MBEDTLS_HAVE_TIME)
3426 used += 8;
3427
3428 if (used <= buf_len) {
3429 start = (uint64_t) session->start;
3430
3431 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3432 p += 8;
3433 }
3434#endif /* MBEDTLS_HAVE_TIME */
3435
3436 /*
3437 * Basic mandatory fields
3438 */
3439 used += 1 /* id_len */
3440 + sizeof(session->id)
3441 + sizeof(session->master)
3442 + 4; /* verify_result */
3443
3444 if (used <= buf_len) {
3445 *p++ = MBEDTLS_BYTE_0(session->id_len);
3446 memcpy(p, session->id, 32);
3447 p += 32;
3448
3449 memcpy(p, session->master, 48);
3450 p += 48;
3451
3452 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3453 p += 4;
3454 }
3455
3456 /*
3457 * Peer's end-entity certificate
3458 */
3459#if defined(MBEDTLS_X509_CRT_PARSE_C)
3460#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3461 if (session->peer_cert == NULL) {
3462 cert_len = 0;
3463 } else {
3464 cert_len = session->peer_cert->raw.len;
3465 }
3466
3467 used += 3 + cert_len;
3468
3469 if (used <= buf_len) {
3470 *p++ = MBEDTLS_BYTE_2(cert_len);
3471 *p++ = MBEDTLS_BYTE_1(cert_len);
3472 *p++ = MBEDTLS_BYTE_0(cert_len);
3473
3474 if (session->peer_cert != NULL) {
3475 memcpy(p, session->peer_cert->raw.p, cert_len);
3476 p += cert_len;
3477 }
3478 }
3479#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3480 if (session->peer_cert_digest != NULL) {
3481 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3482 if (used <= buf_len) {
3483 *p++ = (unsigned char) session->peer_cert_digest_type;
3484 *p++ = (unsigned char) session->peer_cert_digest_len;
3485 memcpy(p, session->peer_cert_digest,
3486 session->peer_cert_digest_len);
3487 p += session->peer_cert_digest_len;
3488 }
3489 } else {
3490 used += 2;
3491 if (used <= buf_len) {
3492 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3493 *p++ = 0;
3494 }
3495 }
3496#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3497#endif /* MBEDTLS_X509_CRT_PARSE_C */
3498
3499 /*
3500 * Session ticket if any, plus associated data
3501 */
3502#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3503#if defined(MBEDTLS_SSL_CLI_C)
3504 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3505 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3506
3507 if (used <= buf_len) {
3508 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3509 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3510 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3511
3512 if (session->ticket != NULL) {
3513 memcpy(p, session->ticket, session->ticket_len);
3514 p += session->ticket_len;
3515 }
3516
3517 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3518 p += 4;
3519 }
3520 }
3521#endif /* MBEDTLS_SSL_CLI_C */
3522#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3523 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3524 used += 8;
3525
3526 if (used <= buf_len) {
3527 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3528 p += 8;
3529 }
3530 }
3531#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3532#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3533
3534 /*
3535 * Misc extension-related info
3536 */
3537#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3538 used += 1;
3539
3540 if (used <= buf_len) {
3541 *p++ = session->mfl_code;
3542 }
3543#endif
3544
3545#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3546 used += 1;
3547
3548 if (used <= buf_len) {
3549 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3550 }
3551#endif
3552
3553 return used;
3554}
3555
3556MBEDTLS_CHECK_RETURN_CRITICAL
3557static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3558 const unsigned char *buf,
3559 size_t len)
3560{
3561#if defined(MBEDTLS_HAVE_TIME)
3562 uint64_t start;
3563#endif
3564#if defined(MBEDTLS_X509_CRT_PARSE_C)
3565#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3566 size_t cert_len;
3567#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3568#endif /* MBEDTLS_X509_CRT_PARSE_C */
3569
3570 const unsigned char *p = buf;
3571 const unsigned char * const end = buf + len;
3572
3573 /*
3574 * Time
3575 */
3576#if defined(MBEDTLS_HAVE_TIME)
3577 if (8 > (size_t) (end - p)) {
3578 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3579 }
3580
3581 start = MBEDTLS_GET_UINT64_BE(p, 0);
3582 p += 8;
3583
3584 session->start = (time_t) start;
3585#endif /* MBEDTLS_HAVE_TIME */
3586
3587 /*
3588 * Basic mandatory fields
3589 */
3590 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3592 }
3593
3594 session->id_len = *p++;
3595 memcpy(session->id, p, 32);
3596 p += 32;
3597
3598 memcpy(session->master, p, 48);
3599 p += 48;
3600
3601 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3602 p += 4;
3603
3604 /* Immediately clear invalid pointer values that have been read, in case
3605 * we exit early before we replaced them with valid ones. */
3606#if defined(MBEDTLS_X509_CRT_PARSE_C)
3607#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3608 session->peer_cert = NULL;
3609#else
3610 session->peer_cert_digest = NULL;
3611#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3612#endif /* MBEDTLS_X509_CRT_PARSE_C */
3613#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3614 session->ticket = NULL;
3615#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3616
3617 /*
3618 * Peer certificate
3619 */
3620#if defined(MBEDTLS_X509_CRT_PARSE_C)
3621#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3622 /* Deserialize CRT from the end of the ticket. */
3623 if (3 > (size_t) (end - p)) {
3624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3625 }
3626
3627 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3628 p += 3;
3629
3630 if (cert_len != 0) {
3631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3632
3633 if (cert_len > (size_t) (end - p)) {
3634 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3635 }
3636
3637 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3638
3639 if (session->peer_cert == NULL) {
3640 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3641 }
3642
3643 mbedtls_x509_crt_init(session->peer_cert);
3644
3645 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3646 p, cert_len)) != 0) {
3647 mbedtls_x509_crt_free(session->peer_cert);
3648 mbedtls_free(session->peer_cert);
3649 session->peer_cert = NULL;
3650 return ret;
3651 }
3652
3653 p += cert_len;
3654 }
3655#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3656 /* Deserialize CRT digest from the end of the ticket. */
3657 if (2 > (size_t) (end - p)) {
3658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3659 }
3660
3661 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3662 session->peer_cert_digest_len = (size_t) *p++;
3663
3664 if (session->peer_cert_digest_len != 0) {
3665 const mbedtls_md_info_t *md_info =
3666 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3667 if (md_info == NULL) {
3668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3669 }
3670 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3672 }
3673
3674 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3676 }
3677
3678 session->peer_cert_digest =
3679 mbedtls_calloc(1, session->peer_cert_digest_len);
3680 if (session->peer_cert_digest == NULL) {
3681 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3682 }
3683
3684 memcpy(session->peer_cert_digest, p,
3685 session->peer_cert_digest_len);
3686 p += session->peer_cert_digest_len;
3687 }
3688#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3689#endif /* MBEDTLS_X509_CRT_PARSE_C */
3690
3691 /*
3692 * Session ticket and associated data
3693 */
3694#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3695#if defined(MBEDTLS_SSL_CLI_C)
3696 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3697 if (3 > (size_t) (end - p)) {
3698 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3699 }
3700
3701 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3702 p += 3;
3703
3704 if (session->ticket_len != 0) {
3705 if (session->ticket_len > (size_t) (end - p)) {
3706 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3707 }
3708
3709 session->ticket = mbedtls_calloc(1, session->ticket_len);
3710 if (session->ticket == NULL) {
3711 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3712 }
3713
3714 memcpy(session->ticket, p, session->ticket_len);
3715 p += session->ticket_len;
3716 }
3717
3718 if (4 > (size_t) (end - p)) {
3719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3720 }
3721
3722 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3723 p += 4;
3724 }
3725#endif /* MBEDTLS_SSL_CLI_C */
3726#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3727 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3728 if (8 > (size_t) (end - p)) {
3729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3730 }
3731 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3732 p += 8;
3733 }
3734#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3735#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3736
3737 /*
3738 * Misc extension-related info
3739 */
3740#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3741 if (1 > (size_t) (end - p)) {
3742 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3743 }
3744
3745 session->mfl_code = *p++;
3746#endif
3747
3748#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3749 if (1 > (size_t) (end - p)) {
3750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3751 }
3752
3753 session->encrypt_then_mac = *p++;
3754#endif
3755
3756 /* Done, should have consumed entire buffer */
3757 if (p != end) {
3758 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3759 }
3760
3761 return 0;
3762}
3763
3764#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3765
3766#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3767/* Serialization of TLS 1.3 sessions:
3768 *
David Horstmanncb01b362024-02-29 17:31:46 +00003769 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003770 */
3771#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3772MBEDTLS_CHECK_RETURN_CRITICAL
3773static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3774 unsigned char *buf,
3775 size_t buf_len,
3776 size_t *olen)
3777{
3778 unsigned char *p = buf;
3779#if defined(MBEDTLS_SSL_CLI_C) && \
3780 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3781 size_t hostname_len = (session->hostname == NULL) ?
3782 0 : strlen(session->hostname) + 1;
3783#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003784
3785#if defined(MBEDTLS_SSL_SRV_C) && \
3786 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003787 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003788 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003789#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003790 size_t needed = 4 /* ticket_age_add */
3791 + 1 /* ticket_flags */
3792 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003793
David Horstmanne59f9702024-02-29 16:08:57 +00003794 *olen = 0;
3795
3796 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3797 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3798 }
3799 needed += session->resumption_key_len; /* resumption_key */
3800
3801#if defined(MBEDTLS_SSL_EARLY_DATA)
3802 needed += 4; /* max_early_data_size */
3803#endif
3804#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3805 needed += 2; /* record_size_limit */
3806#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3807
3808#if defined(MBEDTLS_HAVE_TIME)
3809 needed += 8; /* ticket_creation_time or ticket_reception_time */
3810#endif
3811
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003812#if defined(MBEDTLS_SSL_SRV_C)
3813 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3814#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003815 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003816 + alpn_len; /* alpn */
3817#endif
3818 }
3819#endif /* MBEDTLS_SSL_SRV_C */
3820
David Horstmanne59f9702024-02-29 16:08:57 +00003821#if defined(MBEDTLS_SSL_CLI_C)
3822 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3823#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3824 needed += 2 /* hostname_len */
3825 + hostname_len; /* hostname */
3826#endif
3827
3828 needed += 4 /* ticket_lifetime */
3829 + 2; /* ticket_len */
3830
3831 /* Check size_t overflow */
3832 if (session->ticket_len > SIZE_MAX - needed) {
3833 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3834 }
3835
3836 needed += session->ticket_len; /* ticket */
3837 }
3838#endif /* MBEDTLS_SSL_CLI_C */
3839
3840 *olen = needed;
3841 if (needed > buf_len) {
3842 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3843 }
3844
3845 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3846 p[4] = session->ticket_flags;
3847
3848 /* save resumption_key */
3849 p[5] = session->resumption_key_len;
3850 p += 6;
3851 memcpy(p, session->resumption_key, session->resumption_key_len);
3852 p += session->resumption_key_len;
3853
3854#if defined(MBEDTLS_SSL_EARLY_DATA)
3855 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3856 p += 4;
3857#endif
3858#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3859 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3860 p += 2;
3861#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3862
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003863#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003864 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003865#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003866 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3867 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003868#endif /* MBEDTLS_HAVE_TIME */
3869
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003870#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003871 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3872 p += 2;
3873
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003874 if (alpn_len > 0) {
3875 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003876 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003877 p += alpn_len;
3878 }
3879#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3880 }
3881#endif /* MBEDTLS_SSL_SRV_C */
3882
David Horstmanne59f9702024-02-29 16:08:57 +00003883#if defined(MBEDTLS_SSL_CLI_C)
3884 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3886 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3887 p += 2;
3888 if (hostname_len > 0) {
3889 /* save host name */
3890 memcpy(p, session->hostname, hostname_len);
3891 p += hostname_len;
3892 }
3893#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3894
3895#if defined(MBEDTLS_HAVE_TIME)
3896 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3897 p += 8;
3898#endif
3899 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3900 p += 4;
3901
3902 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3903 p += 2;
3904
3905 if (session->ticket != NULL && session->ticket_len > 0) {
3906 memcpy(p, session->ticket, session->ticket_len);
3907 p += session->ticket_len;
3908 }
3909 }
3910#endif /* MBEDTLS_SSL_CLI_C */
3911 return 0;
3912}
3913
3914MBEDTLS_CHECK_RETURN_CRITICAL
3915static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3916 const unsigned char *buf,
3917 size_t len)
3918{
3919 const unsigned char *p = buf;
3920 const unsigned char *end = buf + len;
3921
3922 if (end - p < 6) {
3923 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3924 }
3925 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3926 session->ticket_flags = p[4];
3927
3928 /* load resumption_key */
3929 session->resumption_key_len = p[5];
3930 p += 6;
3931
3932 if (end - p < session->resumption_key_len) {
3933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3934 }
3935
3936 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3937 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3938 }
3939 memcpy(session->resumption_key, p, session->resumption_key_len);
3940 p += session->resumption_key_len;
3941
3942#if defined(MBEDTLS_SSL_EARLY_DATA)
3943 if (end - p < 4) {
3944 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3945 }
3946 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3947 p += 4;
3948#endif
3949#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3950 if (end - p < 2) {
3951 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3952 }
3953 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3954 p += 2;
3955#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3956
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003957#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003958 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003959#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003960 if (end - p < 8) {
3961 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3962 }
3963 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3964 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003965#endif /* MBEDTLS_HAVE_TIME */
3966
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003967#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003968 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003969
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003970 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003971 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3972 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003973
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003974 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3975 p += 2;
3976
3977 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003978 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3979 }
3980
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003981 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003982 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3983 if (ret != 0) {
3984 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003985 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003986 p += alpn_len;
3987 }
3988#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3989 }
3990#endif /* MBEDTLS_SSL_SRV_C */
3991
David Horstmanne59f9702024-02-29 16:08:57 +00003992#if defined(MBEDTLS_SSL_CLI_C)
3993 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3994#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3995 size_t hostname_len;
3996 /* load host name */
3997 if (end - p < 2) {
3998 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3999 }
4000 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
4001 p += 2;
4002
4003 if (end - p < (long int) hostname_len) {
4004 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4005 }
4006 if (hostname_len > 0) {
4007 session->hostname = mbedtls_calloc(1, hostname_len);
4008 if (session->hostname == NULL) {
4009 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4010 }
4011 memcpy(session->hostname, p, hostname_len);
4012 p += hostname_len;
4013 }
4014#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4015
4016#if defined(MBEDTLS_HAVE_TIME)
4017 if (end - p < 8) {
4018 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4019 }
4020 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4021 p += 8;
4022#endif
4023 if (end - p < 4) {
4024 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4025 }
4026 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4027 p += 4;
4028
4029 if (end - p < 2) {
4030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4031 }
4032 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4033 p += 2;
4034
4035 if (end - p < (long int) session->ticket_len) {
4036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4037 }
4038 if (session->ticket_len > 0) {
4039 session->ticket = mbedtls_calloc(1, session->ticket_len);
4040 if (session->ticket == NULL) {
4041 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4042 }
4043 memcpy(session->ticket, p, session->ticket_len);
4044 p += session->ticket_len;
4045 }
4046 }
4047#endif /* MBEDTLS_SSL_CLI_C */
4048
4049 return 0;
4050
4051}
4052#else /* MBEDTLS_SSL_SESSION_TICKETS */
4053MBEDTLS_CHECK_RETURN_CRITICAL
4054static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4055 unsigned char *buf,
4056 size_t buf_len,
4057 size_t *olen)
4058{
4059 ((void) session);
4060 ((void) buf);
4061 ((void) buf_len);
4062 *olen = 0;
4063 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4064}
4065
4066static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004067 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004068 size_t buf_len)
4069{
4070 ((void) session);
4071 ((void) buf);
4072 ((void) buf_len);
4073 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4074}
4075#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4076#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4077
Paul Bakker5121ce52009-01-03 21:22:43 +00004078/*
Hanno Beckera835da52019-05-16 12:39:07 +01004079 * Define ticket header determining Mbed TLS version
4080 * and structure of the ticket.
4081 */
4082
Hanno Becker94ef3b32019-05-16 12:50:45 +01004083/*
Hanno Becker50b59662019-05-28 14:30:45 +01004084 * Define bitflag determining compile-time settings influencing
4085 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004086 */
4087
Hanno Becker50b59662019-05-28 14:30:45 +01004088#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004089#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004090#else
Hanno Becker3e088662019-05-29 11:10:18 +01004091#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004092#endif /* MBEDTLS_HAVE_TIME */
4093
4094#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004095#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004096#else
Hanno Becker3e088662019-05-29 11:10:18 +01004097#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004098#endif /* MBEDTLS_X509_CRT_PARSE_C */
4099
David Horstmann5c5a32f2024-02-13 17:53:35 +00004100#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004101#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004102#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004103#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004104#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4105
Hanno Becker94ef3b32019-05-16 12:50:45 +01004106#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004107#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004108#else
Hanno Becker3e088662019-05-29 11:10:18 +01004109#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004110#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4111
4112#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004113#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004114#else
Hanno Becker3e088662019-05-29 11:10:18 +01004115#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004116#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4117
Hanno Becker94ef3b32019-05-16 12:50:45 +01004118#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004119#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004120#else
Hanno Becker3e088662019-05-29 11:10:18 +01004121#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004122#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4123
Hanno Becker94ef3b32019-05-16 12:50:45 +01004124#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4125#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4126#else
4127#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4128#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4129
David Horstmann92b258b2024-02-13 17:23:34 +00004130#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4131#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4132#else
4133#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4134#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4135
4136#if defined(MBEDTLS_SSL_EARLY_DATA)
4137#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4138#else
4139#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4140#endif /* MBEDTLS_SSL_EARLY_DATA */
4141
4142#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4143#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4144#else
4145#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4146#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4147
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004148#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4149 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004150#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4151#else
4152#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4153#endif /* MBEDTLS_SSL_ALPN */
4154
Hanno Becker3e088662019-05-29 11:10:18 +01004155#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4156#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4157#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4158#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004159#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4160#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004161#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004162#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4163#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4164#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004165#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004166
Hanno Becker50b59662019-05-28 14:30:45 +01004167#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 ((uint16_t) ( \
4169 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4170 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4171 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4172 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4173 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4174 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004175 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004176 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4177 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004178 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4179 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4180 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4181 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004182 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004183 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004184 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004185
Chien Wong4e9683e2023-12-28 17:07:43 +08004186static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004187 MBEDTLS_VERSION_MAJOR,
4188 MBEDTLS_VERSION_MINOR,
4189 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004190 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4191 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004192};
Hanno Beckera835da52019-05-16 12:39:07 +01004193
4194/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004195 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004196 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004197 *
David Horstmanncb01b362024-02-29 17:31:46 +00004198 * TLS 1.2 session:
4199 *
4200 * struct {
4201 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4202 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4203 * uint32 ticket_lifetime;
4204 * #endif
4205 * } ClientOnlyData;
4206 *
4207 * struct {
4208 * #if defined(MBEDTLS_HAVE_TIME)
4209 * uint64 start_time;
4210 * #endif
4211 * uint8 session_id_len; // at most 32
4212 * opaque session_id[32];
4213 * opaque master[48]; // fixed length in the standard
4214 * uint32 verify_result;
4215 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4216 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4217 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004218 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004219 * opaque peer_cert_digest<0..2^8-1>
4220 * #endif
4221 * select (endpoint) {
4222 * case client: ClientOnlyData;
4223 * case server: uint64 ticket_creation_time;
4224 * };
4225 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4226 * uint8 mfl_code; // up to 255 according to standard
4227 * #endif
4228 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4229 * uint8 encrypt_then_mac; // 0 or 1
4230 * #endif
4231 * } serialized_session_tls12;
4232 *
4233 *
4234 * TLS 1.3 Session:
4235 *
4236 * struct {
4237 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4238 * opaque hostname<0..2^16-1>;
4239 * #endif
4240 * #if defined(MBEDTLS_HAVE_TIME)
4241 * uint64 ticket_reception_time;
4242 * #endif
4243 * uint32 ticket_lifetime;
4244 * opaque ticket<1..2^16-1>;
4245 * } ClientOnlyData;
4246 *
4247 * struct {
4248 * uint32 ticket_age_add;
4249 * uint8 ticket_flags;
4250 * opaque resumption_key<0..255>;
4251 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4252 * uint32 max_early_data_size;
4253 * #endif
4254 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4255 * uint16 record_size_limit;
4256 * #endif
4257 * select ( endpoint ) {
4258 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004259 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004260 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004261 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004262 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004263 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004264 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004265 * #endif
4266 * };
4267 * } serialized_session_tls13;
4268 *
4269 *
4270 * SSL session:
4271 *
4272 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004273 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004274 * opaque mbedtls_version[3]; // library version: major, minor, patch
4275 * opaque session_format[2]; // library-version specific 16-bit field
4276 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004277 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004278 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004279 * Note: When updating the format, remember to keep
4280 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004281 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004282 * // In this version, `session_format` determines
4283 * // the setting of those compile-time
4284 * // configuration options which influence
4285 * // the structure of mbedtls_ssl_session.
4286 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004287 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004288 * // - TLS 1.2 (0x0303)
4289 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004290 * uint8_t endpoint;
4291 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004292 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004293 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004294 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004295 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004296 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004297 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004298 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004299 *
4300 * };
4301 *
4302 * } serialized_session;
4303 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004304 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004305
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004307static int ssl_session_save(const mbedtls_ssl_session *session,
4308 unsigned char omit_header,
4309 unsigned char *buf,
4310 size_t buf_len,
4311 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004312{
4313 unsigned char *p = buf;
4314 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004315 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004316#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4317 size_t out_len;
4318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4319#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (session == NULL) {
4321 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4322 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004323
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004325 /*
4326 * Add Mbed TLS version identifier
4327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004329
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 if (used <= buf_len) {
4331 memcpy(p, ssl_serialized_session_header,
4332 sizeof(ssl_serialized_session_header));
4333 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004334 }
4335 }
4336
4337 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004338 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004339 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004340 used += 1 /* TLS version */
4341 + 1 /* endpoint */
4342 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 if (used <= buf_len) {
4344 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004345 *p++ = session->endpoint;
4346 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4347 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004348 }
4349
4350 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004351 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004353#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 case MBEDTLS_SSL_VERSION_TLS1_2:
4355 used += ssl_tls12_session_save(session, p, remaining_len);
4356 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004357#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4358
Jerry Yu251a12e2022-07-13 15:15:48 +08004359#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 case MBEDTLS_SSL_VERSION_TLS1_3:
4361 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4362 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4363 return ret;
4364 }
4365 used += out_len;
4366 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004367#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 default:
4370 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004371 }
4372
4373 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (used > buf_len) {
4375 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4376 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004377
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004379}
4380
4381/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004382 * Public wrapper for ssl_session_save()
4383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004384int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4385 unsigned char *buf,
4386 size_t buf_len,
4387 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004388{
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004390}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004391
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004392/*
4393 * Deserialize session, see mbedtls_ssl_session_save() for format.
4394 *
4395 * This internal version is wrapped by a public function that cleans up in
4396 * case of error, and has an extra option omit_header.
4397 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004398MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004399static int ssl_session_load(mbedtls_ssl_session *session,
4400 unsigned char omit_header,
4401 const unsigned char *buf,
4402 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004403{
4404 const unsigned char *p = buf;
4405 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004406 size_t remaining_len;
4407
4408
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (session == NULL) {
4410 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4411 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004414 /*
4415 * Check Mbed TLS version identifier
4416 */
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4419 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004420 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004421
4422 if (memcmp(p, ssl_serialized_session_header,
4423 sizeof(ssl_serialized_session_header)) != 0) {
4424 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4425 }
4426 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004427 }
4428
4429 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004430 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004431 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004432 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4434 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004435 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004436 session->endpoint = *p++;
4437 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4438 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004439
4440 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004441 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004443#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004444 case MBEDTLS_SSL_VERSION_TLS1_2:
4445 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004446#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4447
Jerry Yu438ddd82022-07-07 06:55:50 +00004448#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004449 case MBEDTLS_SSL_VERSION_TLS1_3:
4450 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004451#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 default:
4454 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004455 }
4456}
4457
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004458/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004459 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004461int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4462 const unsigned char *buf,
4463 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004464{
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004466
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (ret != 0) {
4468 mbedtls_ssl_session_free(session);
4469 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004472}
4473
4474/*
Paul Bakker1961b702013-01-25 14:49:24 +01004475 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004478static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004479{
4480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4481
Ronald Cron66dbf912022-02-02 15:33:46 +01004482 /*
4483 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004484 * that were written into the output buffer by the previous handshake step,
4485 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004486 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4487 * We proceed to the next handshake step only when all data from the
4488 * previous one have been sent to the peer, thus we make sure that this is
4489 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4490 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4491 * we have to wait before to go ahead.
4492 * In the case of TLS 1.3, handshake step handlers do not send data to the
4493 * peer. Data are only sent here and through
4494 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004495 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004496 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4498 return ret;
4499 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004500
4501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4503 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4504 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4505 return ret;
4506 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004507 }
4508#endif /* MBEDTLS_SSL_PROTO_DTLS */
4509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004511}
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004514{
Hanno Becker41934dd2021-08-07 19:13:43 +01004515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004518 ssl->conf == NULL ||
4519 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4521 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004522 }
4523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 ret = ssl_prepare_handshake_step(ssl);
4525 if (ret != 0) {
4526 return ret;
4527 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 ret = mbedtls_ssl_handle_pending_alert(ssl);
4530 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004531 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 }
Jerry Yue7047812021-09-13 19:26:39 +08004533
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004534 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4535 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4536 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004538#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4540 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004541 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004544 case MBEDTLS_SSL_HELLO_REQUEST:
4545 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004546 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004547 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004548
Ronald Cron9f0fba32022-02-10 16:45:15 +01004549 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004551 break;
4552
4553 default:
4554#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4556 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4557 } else {
4558 ret = mbedtls_ssl_handshake_client_step(ssl);
4559 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004560#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004562#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004564#endif
4565 }
Jerry Yub9930e72021-08-06 17:11:51 +08004566 }
Ronald Cron6291b232023-03-08 15:51:25 +01004567#endif /* MBEDTLS_SSL_CLI_C */
4568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004571#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4572 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004574 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 ret = mbedtls_ssl_handshake_server_step(ssl);
4576 }
Ronald Cron6291b232023-03-08 15:51:25 +01004577#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4578 ret = mbedtls_ssl_handshake_server_step(ssl);
4579#else
4580 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004581#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004582 }
4583#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004586 /* handshake_step return error. And it is same
4587 * with alert_reason.
4588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (ssl->send_alert) {
4590 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004591 goto cleanup;
4592 }
4593 }
4594
4595cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004597}
4598
4599/*
4600 * Perform the SSL handshake
4601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004602int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004603{
4604 int ret = 0;
4605
Hanno Beckera817ea42020-10-20 15:20:23 +01004606 /* Sanity checks */
4607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if (ssl == NULL || ssl->conf == NULL) {
4609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4610 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004611
Hanno Beckera817ea42020-10-20 15:20:23 +01004612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4614 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4615 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4616 "mbedtls_ssl_set_timer_cb() for DTLS"));
4617 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004618 }
4619#endif /* MBEDTLS_SSL_PROTO_DTLS */
4620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004622
Hanno Beckera817ea42020-10-20 15:20:23 +01004623 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4625 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004626
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004628 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 }
Paul Bakker1961b702013-01-25 14:49:24 +01004630 }
4631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004635}
4636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637#if defined(MBEDTLS_SSL_RENEGOTIATION)
4638#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004639/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004640 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004641 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004642MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004643static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004644{
Janos Follath865b3eb2019-12-16 11:46:15 +00004645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004648
4649 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4651 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4654 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4655 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004656 }
4657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004663
4664/*
4665 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004666 * - any side: calling mbedtls_ssl_renegotiate(),
4667 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4668 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004669 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004670 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004673int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004674{
Janos Follath865b3eb2019-12-16 11:46:15 +00004675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if ((ret = ssl_handshake_init(ssl)) != 0) {
4680 return ret;
4681 }
Paul Bakker48916f92012-09-16 19:57:18 +00004682
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004683 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4684 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4687 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4688 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004689 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004691 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004693 }
4694#endif
4695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4697 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4700 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4701 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004702 }
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004707}
4708
4709/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004710 * Renegotiate current connection on client,
4711 * or request renegotiation on server
4712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004713int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004715 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if (ssl == NULL || ssl->conf == NULL) {
4718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4719 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004721#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004722 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4724 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4725 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4726 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004729
4730 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if (ssl->out_left != 0) {
4732 return mbedtls_ssl_flush_output(ssl);
4733 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004736 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004740 /*
4741 * On client, either start the renegotiation process or,
4742 * if already in progress, continue the handshake
4743 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4745 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004747 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004748
4749 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4750 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4751 return ret;
4752 }
4753 } else {
4754 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4755 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4756 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004757 }
4758 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004759#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004766{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004767 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004772
Valerio Setti6f0441d2023-07-03 12:11:36 +02004773#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004774#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (ssl->handshake->group_list_heap_allocated) {
4776 mbedtls_free((void *) handshake->group_list);
4777 }
Brett Warrene0edc842021-08-17 09:53:13 +01004778 handshake->group_list = NULL;
4779#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004780#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004781
Ronald Crone68ab4f2022-10-05 12:46:29 +02004782#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004783#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (ssl->handshake->sig_algs_heap_allocated) {
4785 mbedtls_free((void *) handshake->sig_algs);
4786 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004787 handshake->sig_algs = NULL;
4788#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004789#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 if (ssl->handshake->certificate_request_context) {
4791 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004792 }
4793#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004794#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004795
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004796#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4798 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004799 handshake->async_in_progress = 0;
4800 }
4801#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4802
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004803#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004804#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004806#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004807 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004808#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004809#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004810#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004811#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004813#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004814 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004815#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004816#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004820#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004821#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004822 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004824#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004825
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004826#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004827#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004829 /*
4830 * Opaque keys are not stored in the handshake's data and it's the user
4831 * responsibility to destroy them. Clear ones, instead, are created by
4832 * the TLS library and should be destroyed at the same level
4833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4835 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004836 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004837 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4838#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004840#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004841#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004843 handshake->ecjpake_cache = NULL;
4844 handshake->ecjpake_cache_len = 0;
4845#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004846#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004847
Valerio Setti7aeec542023-07-05 18:57:21 +02004848#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004849 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004850 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004851 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004853#endif
4854
Ronald Cron73fe8df2022-10-05 14:31:43 +02004855#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004856#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004858 /* The maintenance of the external PSK key slot is the
4859 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (ssl->handshake->psk_opaque_is_internal) {
4861 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004862 ssl->handshake->psk_opaque_is_internal = 0;
4863 }
4864 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4865 }
Neil Armstronge952a302022-05-03 10:22:14 +02004866#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004868 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004869 }
Neil Armstronge952a302022-05-03 10:22:14 +02004870#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004871#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4874 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004875 /*
4876 * Free only the linked list wrapper, not the keys themselves
4877 * since the belong to the SNI callback
4878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004881
Gilles Peskineeccd8882020-03-10 12:19:08 +01004882#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4884 if (handshake->ecrs_peer_cert != NULL) {
4885 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4886 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004887 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004888#endif
4889
Hanno Becker75173122019-02-06 16:18:31 +00004890#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4891 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004892 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004893#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4894
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004895#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4897 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004898#endif /* MBEDTLS_SSL_CLI_C &&
4899 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004900
4901#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 mbedtls_ssl_flight_free(handshake->flight);
4903 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004904#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004905
Valerio Settiea59c432023-07-25 11:14:03 +02004906#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004907 if (handshake->xxdh_psa_privkey_is_external == 0) {
4908 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 }
Valerio Settiea59c432023-07-25 11:14:03 +02004910#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004911
Ronald Cron6f135e12021-12-08 16:57:54 +01004912#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_ssl_transform_free(handshake->transform_handshake);
4914 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004915#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4917 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004918#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004919#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004920
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004921
4922#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4923 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4924 * processes datagrams and the fact that a datagram is allowed to have
4925 * several records in it, it is possible that the I/O buffers are not
4926 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4928 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004929#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004930
Jerry Yuba9c7272021-10-30 11:54:10 +08004931 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 mbedtls_platform_zeroize(handshake,
4933 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004934}
4935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004937{
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004939 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004944#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004945
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004946#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004947#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4948 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004950#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004952#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004953
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004954#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4955 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004956 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004957#endif
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004960}
4961
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004962#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004963
4964#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4965#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4966#else
4967#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4968#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4969
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004970#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004971
4972#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4973#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4974#else
4975#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4976#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4977
4978#if defined(MBEDTLS_SSL_ALPN)
4979#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4980#else
4981#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4982#endif /* MBEDTLS_SSL_ALPN */
4983
4984#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4985#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4986#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4987#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4988
4989#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 ((uint32_t) ( \
4991 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4992 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4993 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4994 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4995 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4996 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4997 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4998 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004999
Chien Wong4e9683e2023-12-28 17:07:43 +08005000static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005001 MBEDTLS_VERSION_MAJOR,
5002 MBEDTLS_VERSION_MINOR,
5003 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5005 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5006 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5007 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5008 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005009};
5010
Paul Bakker5121ce52009-01-03 21:22:43 +00005011/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005012 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005013 *
5014 * The format of the serialized data is:
5015 * (in the presentation language of TLS, RFC 8446 section 3)
5016 *
5017 * // header
5018 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005019 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005020 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005021 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005022 * Note: When updating the format, remember to keep these
5023 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005024 *
5025 * // session sub-structure
5026 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5027 * // transform sub-structure
5028 * uint8 random[64]; // ServerHello.random+ClientHello.random
5029 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5030 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5031 * // fields from ssl_context
5032 * uint32 badmac_seen; // DTLS: number of records with failing MAC
5033 * uint64 in_window_top; // DTLS: last validated record seq_num
5034 * uint64 in_window; // DTLS: bitmask for replay protection
5035 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5036 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5037 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5038 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5039 *
5040 * Note that many fields of the ssl_context or sub-structures are not
5041 * serialized, as they fall in one of the following categories:
5042 *
5043 * 1. forced value (eg in_left must be 0)
5044 * 2. pointer to dynamically-allocated memory (eg session, transform)
5045 * 3. value can be re-derived from other data (eg session keys from MS)
5046 * 4. value was temporary (eg content of input buffer)
5047 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005049int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5050 unsigned char *buf,
5051 size_t buf_len,
5052 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005053{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005054 unsigned char *p = buf;
5055 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005056 size_t session_len;
5057 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005058
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005059 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005060 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5061 * this function's documentation.
5062 *
5063 * These are due to assumptions/limitations in the implementation. Some of
5064 * them are likely to stay (no handshake in progress) some might go away
5065 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005066 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005067 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5069 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5070 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 if (ssl->handshake != NULL) {
5073 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5074 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005075 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005076 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 if (ssl->transform == NULL || ssl->session == NULL) {
5078 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5079 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005080 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005081 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 if (mbedtls_ssl_check_pending(ssl) != 0) {
5083 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5084 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 if (ssl->out_left != 0) {
5087 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5088 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005089 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005090 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5092 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5093 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005094 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005095 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5097 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5098 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005099 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005100 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5102 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5103 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005104 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005105 /* Renegotiation must not be enabled */
5106#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5108 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5109 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005110 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005111#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005112
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005113 /*
5114 * Version and format identifier
5115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005117
Gilles Peskine449bd832023-01-11 14:50:10 +01005118 if (used <= buf_len) {
5119 memcpy(p, ssl_serialized_context_header,
5120 sizeof(ssl_serialized_context_header));
5121 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005122 }
5123
5124 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005125 * Session (length + data)
5126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5128 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5129 return ret;
5130 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005131
5132 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (used <= buf_len) {
5134 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005135 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005136
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 ret = ssl_session_save(ssl->session, 1,
5138 p, session_len, &session_len);
5139 if (ret != 0) {
5140 return ret;
5141 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005142
5143 p += session_len;
5144 }
5145
5146 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005147 * Transform
5148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 used += sizeof(ssl->transform->randbytes);
5150 if (used <= buf_len) {
5151 memcpy(p, ssl->transform->randbytes,
5152 sizeof(ssl->transform->randbytes));
5153 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005154 }
5155
5156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005157 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005159 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005161 p += ssl->transform->in_cid_len;
5162
5163 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005165 p += ssl->transform->out_cid_len;
5166 }
5167#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5168
5169 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005170 * Saved fields from top-level ssl_context structure
5171 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005172 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 if (used <= buf_len) {
5174 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005175 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005176 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005177
5178#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5179 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (used <= buf_len) {
5181 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005182 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005185 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005186 }
5187#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5188
5189#if defined(MBEDTLS_SSL_PROTO_DTLS)
5190 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005192 *p++ = ssl->disable_datagram_packing;
5193 }
5194#endif /* MBEDTLS_SSL_PROTO_DTLS */
5195
Jerry Yuae0b2e22021-10-08 15:21:19 +08005196 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (used <= buf_len) {
5198 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005199 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005200 }
5201
5202#if defined(MBEDTLS_SSL_PROTO_DTLS)
5203 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 if (used <= buf_len) {
5205 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005206 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005207 }
5208#endif /* MBEDTLS_SSL_PROTO_DTLS */
5209
5210#if defined(MBEDTLS_SSL_ALPN)
5211 {
5212 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005214 : 0;
5215
5216 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005218 *p++ = alpn_len;
5219
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 if (ssl->alpn_chosen != NULL) {
5221 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005222 p += alpn_len;
5223 }
5224 }
5225 }
5226#endif /* MBEDTLS_SSL_ALPN */
5227
5228 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005229 * Done
5230 */
5231 *olen = used;
5232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if (used > buf_len) {
5234 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5235 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005240}
5241
5242/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005243 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005244 *
5245 * This internal version is wrapped by a public function that cleans up in
5246 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005247 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005249static int ssl_context_load(mbedtls_ssl_context *ssl,
5250 const unsigned char *buf,
5251 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005252{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005253 const unsigned char *p = buf;
5254 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005255 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005257#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005258 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005259#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005260
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005261 /*
5262 * The context should have been freshly setup or reset.
5263 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005264 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005265 * renegotiating, or if the user mistakenly loaded a session first.)
5266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5268 ssl->session != NULL) {
5269 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005270 }
5271
5272 /*
5273 * We can't check that the config matches the initial one, but we can at
5274 * least check it matches the requirements for serializing.
5275 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005276 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005277#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005278 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005279#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005280 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5281 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5282 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5283 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005285 }
5286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005288
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005289 /*
5290 * Check version identifier
5291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5293 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005295
5296 if (memcmp(p, ssl_serialized_context_header,
5297 sizeof(ssl_serialized_context_header)) != 0) {
5298 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5299 }
5300 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005301
5302 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005303 * Session
5304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if ((size_t) (end - p) < 4) {
5306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5307 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005308
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005309 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005310 p += 4;
5311
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005312 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005313 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005314 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005315 ssl->session_in = ssl->session;
5316 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005317 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 if ((size_t) (end - p) < session_len) {
5320 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5321 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 ret = ssl_session_load(ssl->session, 1, p, session_len);
5324 if (ret != 0) {
5325 mbedtls_ssl_session_free(ssl->session);
5326 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005327 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005328
5329 p += session_len;
5330
5331 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005332 * Transform
5333 */
5334
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005335 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005336 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005338 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005339 ssl->transform_in = ssl->transform;
5340 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005341 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005342#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005343
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005344#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5346 if (prf_func == NULL) {
5347 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5348 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005349
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005350 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5353 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005354
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 ret = ssl_tls12_populate_transform(ssl->transform,
5356 ssl->session->ciphersuite,
5357 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005358#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005360#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 prf_func,
5362 p, /* currently pointing to randbytes */
5363 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5364 ssl->conf->endpoint,
5365 ssl);
5366 if (ret != 0) {
5367 return ret;
5368 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005371
5372#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5373 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 if ((size_t) (end - p) < 1) {
5375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5376 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005377
5378 ssl->transform->in_cid_len = *p++;
5379
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5381 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5382 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005385 p += ssl->transform->in_cid_len;
5386
5387 ssl->transform->out_cid_len = *p++;
5388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5390 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5391 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005394 p += ssl->transform->out_cid_len;
5395#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5396
5397 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005398 * Saved fields from top-level ssl_context structure
5399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if ((size_t) (end - p) < 4) {
5401 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5402 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005403
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005404 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005405 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005406
5407#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 if ((size_t) (end - p) < 16) {
5409 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5410 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005411
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005412 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005413 p += 8;
5414
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005415 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005416 p += 8;
5417#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5418
5419#if defined(MBEDTLS_SSL_PROTO_DTLS)
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é-Gonnardc86c5df2019-07-15 11:23:03 +02005423
5424 ssl->disable_datagram_packing = *p++;
5425#endif /* MBEDTLS_SSL_PROTO_DTLS */
5426
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5428 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5429 }
5430 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5431 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005432
5433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if ((size_t) (end - p) < 2) {
5435 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5436 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005437
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005438 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005439 p += 2;
5440#endif /* MBEDTLS_SSL_PROTO_DTLS */
5441
5442#if defined(MBEDTLS_SSL_ALPN)
5443 {
5444 uint8_t alpn_len;
5445 const char **cur;
5446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 if ((size_t) (end - p) < 1) {
5448 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5449 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005450
5451 alpn_len = *p++;
5452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005454 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5456 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005457 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005458 ssl->alpn_chosen = *cur;
5459 break;
5460 }
5461 }
5462 }
5463
5464 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5466 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5467 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005468
5469 p += alpn_len;
5470 }
5471#endif /* MBEDTLS_SSL_ALPN */
5472
5473 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005474 * Forced fields from top-level ssl_context structure
5475 *
5476 * Most of them already set to the correct value by mbedtls_ssl_init() and
5477 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5478 */
5479 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005480 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005481
Hanno Becker361b10d2019-08-30 10:42:49 +01005482 /* Adjust pointers for header fields of outgoing records to
5483 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005485
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005486#if defined(MBEDTLS_SSL_PROTO_DTLS)
5487 ssl->in_epoch = 1;
5488#endif
5489
5490 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5491 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005492 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5493 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 if (ssl->handshake != NULL) {
5495 mbedtls_ssl_handshake_free(ssl);
5496 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005497 ssl->handshake = NULL;
5498 }
5499
5500 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005501 * Done - should have consumed entire buffer
5502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 if (p != end) {
5504 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5505 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005508}
5509
5510/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005511 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005513int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5514 const unsigned char *buf,
5515 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005516{
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005518
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 if (ret != 0) {
5520 mbedtls_ssl_free(context);
5521 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005522
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005524}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005525#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005526
5527/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 * Free an SSL context
5529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005530void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005531{
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005533 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005539#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5540 size_t out_buf_len = ssl->out_buf_len;
5541#else
5542 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5543#endif
5544
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005545 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005546 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 }
5548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005550#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5551 size_t in_buf_len = ssl->in_buf_len;
5552#else
5553 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5554#endif
5555
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005556 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005557 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005558 }
5559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 if (ssl->transform) {
5561 mbedtls_ssl_transform_free(ssl->transform);
5562 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005563 }
5564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 if (ssl->handshake) {
5566 mbedtls_ssl_handshake_free(ssl);
5567 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005568
5569#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5571 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005572#endif
5573
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 mbedtls_ssl_session_free(ssl->session_negotiate);
5575 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005576 }
5577
Ronald Cron6f135e12021-12-08 16:57:54 +01005578#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 mbedtls_ssl_transform_free(ssl->transform_application);
5580 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005581#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (ssl->session) {
5584 mbedtls_ssl_session_free(ssl->session);
5585 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005586 }
5587
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005588#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005590 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005591 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005593
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005594#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005596#endif
5597
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005599
Paul Bakker86f04f42013-02-14 11:20:09 +01005600 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005602}
5603
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005604/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005605 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005608{
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005610}
5611
Gilles Peskineae270bf2021-06-02 00:05:29 +02005612/* The selection should be the same as mbedtls_x509_crt_profile_default in
5613 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005614 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005615 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005616 * about this list.
5617 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005618static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005619#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005620 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005621#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005622#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005623 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005624#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005625#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005626 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005627#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005628#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005629 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005630#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005631#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005632 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005633#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005634#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005635 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005636#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005637#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005638 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005639#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005640#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005641 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005642#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005643#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005644 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5645 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5646 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5647 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5648 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5649#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005650 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005651};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005652
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005653static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005654 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5655 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5656 0
5657};
5658
Ronald Crone68ab4f2022-10-05 12:46:29 +02005659#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005660
Jerry Yu909df7b2022-01-22 11:56:27 +08005661/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005662 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005663 * rules SHOULD be upheld.
5664 * - No duplicate entries.
5665 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005666 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005667 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005668 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005669static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005670
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005671#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005672 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005673 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005674 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005675 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5676#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005677
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005678#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005679 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005680 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005681 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005682 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5683#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005684
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005685#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005686 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005687 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005688 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005689 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5690#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005691
Yanray Wang1136fad2023-11-22 16:54:31 +08005692#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005693 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005694#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005695
Yanray Wang1136fad2023-11-22 16:54:31 +08005696#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005697 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005698#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005699
Yanray Wang1136fad2023-11-22 16:54:31 +08005700#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005701 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005702#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005703
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005705 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005706#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005707
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005708#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005709 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005710#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005711
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005712#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005713 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005714#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005715
Gabor Mezei15b95a62022-05-09 16:37:58 +02005716 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005717};
5718
5719/* NOTICE: see above */
5720#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5721static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005722
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005723#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005724#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005726#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005727#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5728 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005729#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005730#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005732#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005733#endif /* MBEDTLS_MD_CAN_SHA512 */
5734
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005735#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005736#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005738#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005739#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5740 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005741#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005742#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005743 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005744#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005745#endif /* MBEDTLS_MD_CAN_SHA384 */
5746
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005747#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005748#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005750#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005751#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5752 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005753#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005754#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005756#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005757#endif /* MBEDTLS_MD_CAN_SHA256 */
5758
Gabor Mezei15b95a62022-05-09 16:37:58 +02005759 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005760};
5761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005762
Jerry Yu909df7b2022-01-22 11:56:27 +08005763/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005764static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005765
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005766#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005767 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005768 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005769 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005770 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5771#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005772
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005773#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005774 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005775 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005776 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005777 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5778#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005779
Gabor Mezei15b95a62022-05-09 16:37:58 +02005780 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005781};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005782
Jerry Yu909df7b2022-01-22 11:56:27 +08005783/* NOTICE: see above */
5784#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5785static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005786
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005787#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005788#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005790#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005791#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005792
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005793#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005794#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005796#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005797#endif /* MBEDTLS_MD_CAN_SHA384 */
5798
Gabor Mezei15b95a62022-05-09 16:37:58 +02005799 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005800};
Jerry Yu909df7b2022-01-22 11:56:27 +08005801#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5802
Ronald Crone68ab4f2022-10-05 12:46:29 +02005803#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005804
Chien Wong4e9683e2023-12-28 17:07:43 +08005805static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005806#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005807 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005808#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005809#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005810 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005811#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005812 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005813};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005814
Ronald Crone68ab4f2022-10-05 12:46:29 +02005815#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005816/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005817 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005818MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005819static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005820{
5821 size_t i, j;
5822 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5825 for (j = 0; j < i; j++) {
5826 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005827 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 }
5829 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5830 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5831 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005832 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005833 }
5834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005835 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005836}
5837
Ronald Crone68ab4f2022-10-05 12:46:29 +02005838#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005839
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005840/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005841 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005843int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5844 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005845{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005846#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005848#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005849
Ronald Crone68ab4f2022-10-05 12:46:29 +02005850#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5852 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5853 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005854 }
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5857 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5858 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005859 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005860
5861#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5863 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5864 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005865 }
5866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5868 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5869 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005870 }
5871#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005872#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005873
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005874 /* Use the functions here so that they are covered in tests,
5875 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 mbedtls_ssl_conf_endpoint(conf, endpoint);
5877 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005878
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005879 /*
5880 * Things that are common to all presets
5881 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005882#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005884 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5885#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005886 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005887#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005888 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5889 * handling is disabled by default in Mbed TLS 3.6.x for backward
5890 * compatibility with client applications developed using Mbed TLS 3.5
5891 * or earlier with the default configuration.
5892 *
5893 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5894 * disabled, and a Mbed TLS client with the default configuration would
5895 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5896 * server.
5897 *
5898 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5899 * an Mbed TLS client with the default configuration establishes a
5900 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5901 * following the handshake the TLS 1.3 server sends NewSessionTicket
5902 * messages and the Mbed TLS client processes them, this results in
5903 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5904 * mbedtls_ssl_handshake(), ...) to eventually return an
5905 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5906 * (see the documentation of mbedtls_ssl_read() for more information on
5907 * that error code). Applications unaware of that TLS 1.3 specific non
5908 * fatal error code are then failing.
5909 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005910 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5911 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005912#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005913#endif
5914 }
5915#endif
5916
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005917#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5918 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5919#endif
5920
5921#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5922 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5923#endif
5924
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005925#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005926 conf->f_cookie_write = ssl_cookie_write_dummy;
5927 conf->f_cookie_check = ssl_cookie_check_dummy;
5928#endif
5929
5930#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5931 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5932#endif
5933
Janos Follath088ce432017-04-10 12:42:31 +01005934#if defined(MBEDTLS_SSL_SRV_C)
5935 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005936 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005937#endif
5938
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005939#if defined(MBEDTLS_SSL_PROTO_DTLS)
5940 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5941 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5942#endif
5943
5944#if defined(MBEDTLS_SSL_RENEGOTIATION)
5945 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005946 memset(conf->renego_period, 0x00, 2);
5947 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005948#endif
5949
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005950#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005952 const unsigned char dhm_p[] =
5953 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5954 const unsigned char dhm_g[] =
5955 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5958 dhm_p, sizeof(dhm_p),
5959 dhm_g, sizeof(dhm_g))) != 0) {
5960 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005961 }
5962 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005963#endif
5964
Ronald Cron6f135e12021-12-08 16:57:54 +01005965#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005966
5967#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005968 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005969#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005970 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005971#endif
5972#endif /* MBEDTLS_SSL_EARLY_DATA */
5973
Jerry Yud0766ec2022-09-22 10:46:57 +08005974#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005975 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005977#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005978 /*
5979 * Allow all TLS 1.3 key exchange modes by default.
5980 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005981 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005982#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005983
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005985#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005986 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005987 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005988#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005990#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005992#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005993 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5994 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005995#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5996 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5997 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5998#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5999 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6000 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6001#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006003#endif
6004 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006005
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006006 /*
6007 * Preset-specific defaults
6008 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006010 /*
6011 * NSA Suite B
6012 */
6013 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006014
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006015 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006016
6017#if defined(MBEDTLS_X509_CRT_PARSE_C)
6018 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006019#endif
6020
Ronald Crone68ab4f2022-10-05 12:46:29 +02006021#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006022#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006024 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006026#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006028#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006029
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006030#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006031 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006032#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006033 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006034 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006035
6036 /*
6037 * Default
6038 */
6039 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006040
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006041 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006042
6043#if defined(MBEDTLS_X509_CRT_PARSE_C)
6044 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6045#endif
6046
Ronald Crone68ab4f2022-10-05 12:46:29 +02006047#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006048#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006050 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006054#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006055
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006056#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006057 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006058#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006059 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006060
6061#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6062 conf->dhm_min_bitlen = 1024;
6063#endif
6064 }
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006067}
6068
6069/*
6070 * Free mbedtls_ssl_config
6071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006072void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006073{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006074 if (conf == NULL) {
6075 return;
6076 }
6077
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006078#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 mbedtls_mpi_free(&conf->dhm_P);
6080 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006081#endif
6082
Ronald Cron73fe8df2022-10-05 14:31:43 +02006083#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006084#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006086 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6087 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006088#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006090 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006091 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006092 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006093 }
6094
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006096 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006097 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006098 conf->psk_identity_len = 0;
6099 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006100#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006101
6102#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006104#endif
6105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006107}
6108
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006109#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006110 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006111/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006114unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6118 return MBEDTLS_SSL_SIG_RSA;
6119 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006120#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006121#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6123 return MBEDTLS_SSL_SIG_ECDSA;
6124 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006125#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006127}
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006130{
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006132 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006134 case MBEDTLS_PK_ECDSA:
6135 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006137 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006139 }
6140}
6141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006143{
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145#if defined(MBEDTLS_RSA_C)
6146 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006148#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006149#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006152#endif
6153 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006155 }
6156}
Valerio Settie9646ec2023-08-02 20:02:28 +02006157#endif /* MBEDTLS_PK_C &&
6158 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006159
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006160/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006161 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006163mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006164{
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006166#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006169#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006170#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006173#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006174#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006177#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006178#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006181#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006182#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006185#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006186#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006189#endif
6190 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006192 }
6193}
6194
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006195/*
6196 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006198unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006199{
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006201#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006202 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006204#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006205#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006206 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006208#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006209#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006210 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006212#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006213#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006214 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006216#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006217#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006218 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006220#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006221#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006222 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006224#endif
6225 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006227 }
6228}
6229
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006230/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006231 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006232 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006234int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006235{
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 if (group_list == NULL) {
6239 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006240 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 for (; *group_list != 0; group_list++) {
6243 if (*group_list == tls_id) {
6244 return 0;
6245 }
6246 }
6247
6248 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006249}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006250
Valerio Setti49e69072023-06-27 17:27:51 +02006251#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006252/*
6253 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006255int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006256{
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006260 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006264}
Valerio Setti49e69072023-06-27 17:27:51 +02006265#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006266
Valerio Setti18c9fed2022-12-30 17:44:24 +01006267static const struct {
6268 uint16_t tls_id;
6269 mbedtls_ecp_group_id ecp_group_id;
6270 psa_ecc_family_t psa_family;
6271 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006272} tls_id_match_table[] =
6273{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006274#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006275 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006276#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006277#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006278 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006279#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006280#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006281 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006282#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006283#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006284 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006285#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006286#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006287 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006288#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006289#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006290 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006291#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006292#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006293 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006294#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006295#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006296 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006297#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006298#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006299 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006300#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006301#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006302 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006303#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006304#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006305 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006306#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006307#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006308 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006309#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006310#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006311 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006312#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006313 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006314};
6315
Gilles Peskine449bd832023-01-11 14:50:10 +01006316int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006317 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006319{
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6321 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006322 if (type != NULL) {
6323 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 }
6325 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006326 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006328 return PSA_SUCCESS;
6329 }
6330 }
6331
6332 return PSA_ERROR_NOT_SUPPORTED;
6333}
6334
Gilles Peskine449bd832023-01-11 14:50:10 +01006335mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006336{
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6338 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006339 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006341 }
6342
6343 return MBEDTLS_ECP_DP_NONE;
6344}
6345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006347{
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6349 i++) {
6350 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006351 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006353 }
6354
6355 return 0;
6356}
6357
Valerio Setti67419f02023-01-04 16:12:42 +01006358#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006359static const struct {
6360 uint16_t tls_id;
6361 const char *name;
6362} tls_id_curve_name_table[] =
6363{
Valerio Setti54e23792023-07-07 10:49:27 +02006364 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6365 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6366 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6367 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6368 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6369 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6370 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6371 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6372 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6373 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6374 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6375 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6376 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006377 { 0, NULL },
6378};
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006381{
Valerio Settiacd32c02023-06-29 18:06:29 +02006382 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6383 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6384 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006386 }
6387
6388 return NULL;
6389}
Valerio Setti67419f02023-01-04 16:12:42 +01006390#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006391
Jerry Yu148165c2021-09-24 23:20:59 +08006392#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006393int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6394 const mbedtls_md_type_t md,
6395 unsigned char *dst,
6396 size_t dst_len,
6397 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006398{
Ronald Cronf6893e12022-01-07 22:09:01 +01006399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6400 psa_hash_operation_t *hash_operation_to_clone;
6401 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6402
Jerry Yu148165c2021-09-24 23:20:59 +08006403 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006406#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 case MBEDTLS_MD_SHA384:
6408 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6409 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006410#endif
6411
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006412#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 case MBEDTLS_MD_SHA256:
6414 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6415 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006416#endif
6417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 default:
6419 goto exit;
6420 }
6421
6422 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6423 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006424 goto exit;
6425 }
6426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6428 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006429 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006431
6432exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006433#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6434 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006435 (void) ssl;
6436#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006437 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006438}
6439#else /* MBEDTLS_USE_PSA_CRYPTO */
6440
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006441#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006442MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006443static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6444 unsigned char *dst,
6445 size_t dst_len,
6446 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006447{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006448 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006449 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 if (dst_len < 48) {
6452 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6453 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006454
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006455 mbedtls_md_init(&sha384);
6456 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006457 if (ret != 0) {
6458 goto exit;
6459 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006460 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006461 if (ret != 0) {
6462 goto exit;
6463 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006464
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006465 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006466 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006467 goto exit;
6468 }
6469
6470 *olen = 48;
6471
6472exit:
6473
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006474 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006476}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006477#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006478
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006479#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006480MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006481static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6482 unsigned char *dst,
6483 size_t dst_len,
6484 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006485{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006486 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006487 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 if (dst_len < 32) {
6490 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6491 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006492
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006493 mbedtls_md_init(&sha256);
6494 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6495 if (ret != 0) {
6496 goto exit;
6497 }
6498 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6499 if (ret != 0) {
6500 goto exit;
6501 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006502
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006503 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6504 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006505 goto exit;
6506 }
6507
6508 *olen = 32;
6509
6510exit:
6511
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006512 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006514}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006515#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6518 const mbedtls_md_type_t md,
6519 unsigned char *dst,
6520 size_t dst_len,
6521 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006522{
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006524
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006525#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 case MBEDTLS_MD_SHA384:
6527 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006528#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006529
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006530#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 case MBEDTLS_MD_SHA256:
6532 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006533#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006536#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6537 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 (void) ssl;
6539 (void) dst;
6540 (void) dst_len;
6541 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006542#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006544 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006546}
XiaokangQian647719a2021-12-07 09:16:29 +00006547
Jerry Yu148165c2021-09-24 23:20:59 +08006548#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006549
Ronald Crone68ab4f2022-10-05 12:46:29 +02006550#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006551/* mbedtls_ssl_parse_sig_alg_ext()
6552 *
6553 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6554 * value (TLS 1.3 RFC8446):
6555 * enum {
6556 * ....
6557 * ecdsa_secp256r1_sha256( 0x0403 ),
6558 * ecdsa_secp384r1_sha384( 0x0503 ),
6559 * ecdsa_secp521r1_sha512( 0x0603 ),
6560 * ....
6561 * } SignatureScheme;
6562 *
6563 * struct {
6564 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6565 * } SignatureSchemeList;
6566 *
6567 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6568 * value (TLS 1.2 RFC5246):
6569 * enum {
6570 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6571 * sha512(6), (255)
6572 * } HashAlgorithm;
6573 *
6574 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6575 * SignatureAlgorithm;
6576 *
6577 * struct {
6578 * HashAlgorithm hash;
6579 * SignatureAlgorithm signature;
6580 * } SignatureAndHashAlgorithm;
6581 *
6582 * SignatureAndHashAlgorithm
6583 * supported_signature_algorithms<2..2^16-2>;
6584 *
6585 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6586 * generalization of the TLS 1.2 signature algorithm extension.
6587 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6588 * `SignatureScheme` field of TLS 1.3
6589 *
6590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006591int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6592 const unsigned char *buf,
6593 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006594{
6595 const unsigned char *p = buf;
6596 size_t supported_sig_algs_len = 0;
6597 const unsigned char *supported_sig_algs_end;
6598 uint16_t sig_alg;
6599 uint32_t common_idx = 0;
6600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6602 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006603 p += 2;
6604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 memset(ssl->handshake->received_sig_algs, 0,
6606 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006609 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 while (p < supported_sig_algs_end) {
6611 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6612 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006613 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6615 sig_alg,
6616 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6619 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6620 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006621 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006622 }
6623#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6626 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006629 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6630 common_idx += 1;
6631 }
6632 }
6633 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (p != end) {
6635 MBEDTLS_SSL_DEBUG_MSG(1,
6636 ("Signature algorithms extension length misaligned"));
6637 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6638 MBEDTLS_ERR_SSL_DECODE_ERROR);
6639 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006640 }
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 if (common_idx == 0) {
6643 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6644 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6645 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6646 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006647 }
6648
Gabor Mezei15b95a62022-05-09 16:37:58 +02006649 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006651}
6652
Ronald Crone68ab4f2022-10-05 12:46:29 +02006653#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006654
Jerry Yudc7bd172022-02-17 13:44:15 +08006655#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6656
6657#if defined(MBEDTLS_USE_PSA_CRYPTO)
6658
Gilles Peskine449bd832023-01-11 14:50:10 +01006659static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6660 mbedtls_svc_key_id_t key,
6661 psa_algorithm_t alg,
6662 const unsigned char *raw_psk, size_t raw_psk_length,
6663 const unsigned char *seed, size_t seed_length,
6664 const unsigned char *label, size_t label_length,
6665 const unsigned char *other_secret,
6666 size_t other_secret_length,
6667 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006668{
6669 psa_status_t status;
6670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 status = psa_key_derivation_setup(derivation, alg);
6672 if (status != PSA_SUCCESS) {
6673 return status;
6674 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6677 status = psa_key_derivation_input_bytes(derivation,
6678 PSA_KEY_DERIVATION_INPUT_SEED,
6679 seed, seed_length);
6680 if (status != PSA_SUCCESS) {
6681 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006682 }
6683
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 if (other_secret != NULL) {
6685 status = psa_key_derivation_input_bytes(derivation,
6686 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6687 other_secret, other_secret_length);
6688 if (status != PSA_SUCCESS) {
6689 return status;
6690 }
6691 }
6692
6693 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006694 status = psa_key_derivation_input_bytes(
6695 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 raw_psk, raw_psk_length);
6697 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006698 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006700 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 if (status != PSA_SUCCESS) {
6702 return status;
6703 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 status = psa_key_derivation_input_bytes(derivation,
6706 PSA_KEY_DERIVATION_INPUT_LABEL,
6707 label, label_length);
6708 if (status != PSA_SUCCESS) {
6709 return status;
6710 }
6711 } else {
6712 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006713 }
6714
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 status = psa_key_derivation_set_capacity(derivation, capacity);
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 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006721}
6722
Andrzej Kurek57d10632022-10-24 10:32:01 -04006723#if defined(PSA_WANT_ALG_SHA_384) || \
6724 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006725MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006726static int tls_prf_generic(mbedtls_md_type_t md_type,
6727 const unsigned char *secret, size_t slen,
6728 const char *label,
6729 const unsigned char *random, size_t rlen,
6730 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006731{
6732 psa_status_t status;
6733 psa_algorithm_t alg;
6734 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6735 psa_key_derivation_operation_t derivation =
6736 PSA_KEY_DERIVATION_OPERATION_INIT;
6737
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006739 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006741 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006743
6744 /* Normally a "secret" should be long enough to be impossible to
6745 * find by brute force, and in particular should not be empty. But
6746 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6747 * and for this use case it makes sense to have a 0-length "secret".
6748 * Since the key API doesn't allow importing a key of length 0,
6749 * keep master_key=0, which setup_psa_key_derivation() understands
6750 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006752 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6754 psa_set_key_algorithm(&key_attributes, alg);
6755 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006756
Gilles Peskine449bd832023-01-11 14:50:10 +01006757 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6758 if (status != PSA_SUCCESS) {
6759 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6760 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006761 }
6762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 status = setup_psa_key_derivation(&derivation,
6764 master_key, alg,
6765 NULL, 0,
6766 random, rlen,
6767 (unsigned char const *) label,
6768 (size_t) strlen(label),
6769 NULL, 0,
6770 dlen);
6771 if (status != PSA_SUCCESS) {
6772 psa_key_derivation_abort(&derivation);
6773 psa_destroy_key(master_key);
6774 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006775 }
6776
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6778 if (status != PSA_SUCCESS) {
6779 psa_key_derivation_abort(&derivation);
6780 psa_destroy_key(master_key);
6781 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006782 }
6783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 status = psa_key_derivation_abort(&derivation);
6785 if (status != PSA_SUCCESS) {
6786 psa_destroy_key(master_key);
6787 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006788 }
6789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (!mbedtls_svc_key_id_is_null(master_key)) {
6791 status = psa_destroy_key(master_key);
6792 }
6793 if (status != PSA_SUCCESS) {
6794 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6795 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006798}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006799#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006800#else /* MBEDTLS_USE_PSA_CRYPTO */
6801
Andrzej Kurek57d10632022-10-24 10:32:01 -04006802#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006803 (defined(MBEDTLS_MD_CAN_SHA256) || \
6804 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006805MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006806static int tls_prf_generic(mbedtls_md_type_t md_type,
6807 const unsigned char *secret, size_t slen,
6808 const char *label,
6809 const unsigned char *random, size_t rlen,
6810 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006811{
6812 size_t nb;
6813 size_t i, j, k, md_len;
6814 unsigned char *tmp;
6815 size_t tmp_len = 0;
6816 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6817 const mbedtls_md_info_t *md_info;
6818 mbedtls_md_context_t md_ctx;
6819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006822
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6824 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6825 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006828
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 tmp_len = md_len + strlen(label) + rlen;
6830 tmp = mbedtls_calloc(1, tmp_len);
6831 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006832 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6833 goto exit;
6834 }
6835
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 nb = strlen(label);
6837 memcpy(tmp + md_len, label, nb);
6838 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006839 nb += rlen;
6840
6841 /*
6842 * Compute P_<hash>(secret, label + random)[0..dlen]
6843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006845 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6849 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006850 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 }
6852 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6853 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006854 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 }
6856 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6857 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006858 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 for (i = 0; i < dlen; i += md_len) {
6862 ret = mbedtls_md_hmac_reset(&md_ctx);
6863 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006864 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 }
6866 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6867 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006868 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 }
6870 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6871 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006872 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 ret = mbedtls_md_hmac_reset(&md_ctx);
6876 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006877 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 }
6879 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6880 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006881 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 }
6883 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6884 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006885 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006887
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006891 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006893 }
6894
6895exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 if (tmp != NULL) {
6899 mbedtls_platform_zeroize(tmp, tmp_len);
6900 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006907}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006908#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006909#endif /* MBEDTLS_USE_PSA_CRYPTO */
6910
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006911#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006913static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6914 const char *label,
6915 const unsigned char *random, size_t rlen,
6916 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006917{
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6919 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006920}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006921#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006922
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006923#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006924MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006925static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6926 const char *label,
6927 const unsigned char *random, size_t rlen,
6928 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006929{
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6931 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006932}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006933#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006934
Jerry Yuf009d862022-02-17 14:01:37 +08006935/*
6936 * Set appropriate PRF function and other SSL / TLS1.2 functions
6937 *
6938 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006939 * - hash associated with the ciphersuite (only used by TLS 1.2)
6940 *
6941 * Outputs:
6942 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6943 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006944MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006945static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6946 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006947{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006948#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006950 handshake->tls_prf = tls_prf_sha384;
6951 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6952 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006954#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006955#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006956 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006957 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006958 handshake->tls_prf = tls_prf_sha256;
6959 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6960 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6961 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006962#else
Jerry Yuf009d862022-02-17 14:01:37 +08006963 {
Jerry Yuf009d862022-02-17 14:01:37 +08006964 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006965 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006967 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006968#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006971}
Jerry Yud6ab2352022-02-17 14:03:43 +08006972
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006973/*
6974 * Compute master secret if needed
6975 *
6976 * Parameters:
6977 * [in/out] handshake
6978 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6979 * (PSA-PSK) ciphersuite_info, psk_opaque
6980 * [out] premaster (cleared)
6981 * [out] master
6982 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6983 * debug: conf->f_dbg, conf->p_dbg
6984 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006985 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006986 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006988static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6989 unsigned char *master,
6990 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006991{
6992 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6993
6994 /* cf. RFC 5246, Section 8.1:
6995 * "The master secret is always exactly 48 bytes in length." */
6996 size_t const master_secret_len = 48;
6997
6998#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6999 unsigned char session_hash[48];
7000#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7001
7002 /* The label for the KDF used for key expansion.
7003 * This is either "master secret" or "extended master secret"
7004 * depending on whether the Extended Master Secret extension
7005 * is used. */
7006 char const *lbl = "master secret";
7007
Przemek Stekielae4ed302022-04-05 17:15:55 +02007008 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007009 * - If the Extended Master Secret extension is not used,
7010 * this is ClientHello.Random + ServerHello.Random
7011 * (see Sect. 8.1 in RFC 5246).
7012 * - If the Extended Master Secret extension is used,
7013 * this is the transcript of the handshake so far.
7014 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007015 unsigned char const *seed = handshake->randbytes;
7016 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007017
7018#if !defined(MBEDTLS_DEBUG_C) && \
7019 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7020 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007022 ssl = NULL; /* make sure we don't use it except for those cases */
7023 (void) ssl;
7024#endif
7025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 if (handshake->resume != 0) {
7027 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7028 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007029 }
7030
7031#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007033 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007034 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007035 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7036 if (ret != 0) {
7037 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7038 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7041 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007042 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007043#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007044
Przemek Stekiel99114f32022-04-22 11:20:09 +02007045#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007046 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007048 /* Perform PSK-to-MS expansion in a single step. */
7049 psa_status_t status;
7050 psa_algorithm_t alg;
7051 mbedtls_svc_key_id_t psk;
7052 psa_key_derivation_operation_t derivation =
7053 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007054 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007061 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007063 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007065
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007066 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007070 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007071 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007072 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007073 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007074 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007075 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007076 other_secret_len = 48;
7077 other_secret = handshake->premaster + 2;
7078 break;
7079 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007080 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007081 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7082 other_secret = handshake->premaster + 2;
7083 break;
7084 default:
7085 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007086 }
7087
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 status = setup_psa_key_derivation(&derivation, psk, alg,
7089 ssl->conf->psk, ssl->conf->psk_len,
7090 seed, seed_len,
7091 (unsigned char const *) lbl,
7092 (size_t) strlen(lbl),
7093 other_secret, other_secret_len,
7094 master_secret_len);
7095 if (status != PSA_SUCCESS) {
7096 psa_key_derivation_abort(&derivation);
7097 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007098 }
7099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 status = psa_key_derivation_output_bytes(&derivation,
7101 master,
7102 master_secret_len);
7103 if (status != PSA_SUCCESS) {
7104 psa_key_derivation_abort(&derivation);
7105 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007106 }
7107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 status = psa_key_derivation_abort(&derivation);
7109 if (status != PSA_SUCCESS) {
7110 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7111 }
7112 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007113#endif
7114 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007115#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7117 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007118 psa_status_t status;
7119 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7120 psa_key_derivation_operation_t derivation =
7121 PSA_KEY_DERIVATION_OPERATION_INIT;
7122
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007124
7125 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7126
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 status = psa_key_derivation_setup(&derivation, alg);
7128 if (status != PSA_SUCCESS) {
7129 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007130 }
7131
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 status = psa_key_derivation_set_capacity(&derivation,
7133 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7134 if (status != PSA_SUCCESS) {
7135 psa_key_derivation_abort(&derivation);
7136 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007137 }
7138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7140 &derivation);
7141 if (status != PSA_SUCCESS) {
7142 psa_key_derivation_abort(&derivation);
7143 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007144 }
7145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 status = psa_key_derivation_output_bytes(&derivation,
7147 handshake->premaster,
7148 handshake->pmslen);
7149 if (status != PSA_SUCCESS) {
7150 psa_key_derivation_abort(&derivation);
7151 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7152 }
7153
7154 status = psa_key_derivation_abort(&derivation);
7155 if (status != PSA_SUCCESS) {
7156 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007157 }
7158 }
7159#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7161 lbl, seed, seed_len,
7162 master,
7163 master_secret_len);
7164 if (ret != 0) {
7165 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7166 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007167 }
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7170 handshake->premaster,
7171 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007172
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 mbedtls_platform_zeroize(handshake->premaster,
7174 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007175 }
7176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007178}
7179
Gilles Peskine449bd832023-01-11 14:50:10 +01007180int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007181{
7182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7183 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7184 ssl->handshake->ciphersuite_info;
7185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007187
7188 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007190 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 if (ret != 0) {
7192 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7193 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007194 }
7195
7196 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 ret = ssl_compute_master(ssl->handshake,
7198 ssl->session_negotiate->master,
7199 ssl);
7200 if (ret != 0) {
7201 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7202 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007203 }
7204
7205 /* Swap the client and server random values:
7206 * - MS derivation wanted client+server (RFC 5246 8.1)
7207 * - key derivation wants server+client (RFC 5246 6.3) */
7208 {
7209 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 memcpy(tmp, ssl->handshake->randbytes, 64);
7211 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7212 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7213 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007214 }
7215
7216 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7218 ssl->session_negotiate->ciphersuite,
7219 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007220#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007222#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 ssl->handshake->tls_prf,
7224 ssl->handshake->randbytes,
7225 ssl->tls_version,
7226 ssl->conf->endpoint,
7227 ssl);
7228 if (ret != 0) {
7229 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7230 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007231 }
7232
7233 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7235 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007240}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007243{
Gilles Peskine449bd832023-01-11 14:50:10 +01007244 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007245#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007246 case MBEDTLS_SSL_HASH_SHA384:
7247 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7248 break;
7249#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007250#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007251 case MBEDTLS_SSL_HASH_SHA256:
7252 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7253 break;
7254#endif
7255 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007257 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007258#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7259 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007260 (void) ssl;
7261#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007263}
7264
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007265#if defined(MBEDTLS_USE_PSA_CRYPTO)
7266static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7267 const psa_hash_operation_t *hs_op,
7268 size_t buffer_size,
7269 unsigned char *hash,
7270 size_t *hlen)
7271{
7272 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007273 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007274
7275#if !defined(MBEDTLS_DEBUG_C)
7276 (void) ssl;
7277#endif
7278 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007279 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007280 if (status != PSA_SUCCESS) {
7281 goto exit;
7282 }
7283
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007284 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007285 if (status != PSA_SUCCESS) {
7286 goto exit;
7287 }
7288
7289 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7290 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7291
7292exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007293 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007294 return mbedtls_md_error_from_psa(status);
7295}
7296#else
7297static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7298 const mbedtls_md_context_t *hs_ctx,
7299 unsigned char *hash,
7300 size_t *hlen)
7301{
7302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007303 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007304
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007305 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007306
7307#if !defined(MBEDTLS_DEBUG_C)
7308 (void) ssl;
7309#endif
7310 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7311
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007312 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007313 if (ret != 0) {
7314 goto exit;
7315 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007316 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007317 if (ret != 0) {
7318 goto exit;
7319 }
7320
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007321 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007322 if (ret != 0) {
7323 goto exit;
7324 }
7325
7326 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7327
7328 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7329 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7330
7331exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007332 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007333 return ret;
7334}
7335#endif /* MBEDTLS_USE_PSA_CRYPTO */
7336
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007337#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007338int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7339 unsigned char *hash,
7340 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007341{
7342#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007343 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7344 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007345#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007346 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7347 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007348#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007349}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007350#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007351
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007352#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007353int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7354 unsigned char *hash,
7355 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007356{
7357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007358 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7359 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007360#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007361 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7362 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007363#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007364}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007365#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007366
Neil Armstrong80f6f322022-05-03 17:56:38 +02007367#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7368 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007369int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007370{
7371 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007373 const unsigned char *psk = NULL;
7374 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007378 /*
7379 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007380 * checked before calling this function.
7381 *
7382 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007383 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7386 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7387 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007388 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007389 }
7390
7391 /*
7392 * PMS = struct {
7393 * opaque other_secret<0..2^16-1>;
7394 * opaque psk<0..2^16-1>;
7395 * };
7396 * with "other_secret" depending on the particular key exchange
7397 */
7398#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7400 if (end - p < 2) {
7401 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7402 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007405 p += 2;
7406
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 if (end < p || (size_t) (end - p) < psk_len) {
7408 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7409 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007410
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007412 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007414#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7415#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007417 /*
7418 * other_secret already set by the ClientKeyExchange message,
7419 * and is 48 bytes long
7420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 if (end - p < 2) {
7422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7423 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007424
7425 *p++ = 0;
7426 *p++ = 48;
7427 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007429#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7430#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7433 size_t len;
7434
7435 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007437 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7439 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7440 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007443 p += 2 + len;
7444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7446 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007447#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007448#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7451 size_t zlen;
7452
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007454 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7456 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7457 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007458 }
7459
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007461 p += 2 + zlen;
7462
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7464 MBEDTLS_DEBUG_ECDH_Z);
7465 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007466#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007467 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7469 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007470 }
7471
7472 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (end - p < 2) {
7474 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7475 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007478 p += 2;
7479
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 if (end < p || (size_t) (end - p) < psk_len) {
7481 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7482 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007483
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007485 p += psk_len;
7486
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007487 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007490}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007491#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007492
7493#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007494MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007495static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007496
7497#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007498int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007499{
7500 /* If renegotiation is not enforced, retransmit until we would reach max
7501 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007503 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7504 unsigned char doublings = 1;
7505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007507 ++doublings;
7508 ratio >>= 1;
7509 }
7510
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 if (++ssl->renego_records_seen > doublings) {
7512 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7513 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007514 }
7515 }
7516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007518}
7519#endif
7520#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007521
Jerry Yud9526692022-02-17 14:23:47 +08007522/*
7523 * Handshake functions
7524 */
7525#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7526/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007527int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007528{
7529 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7530 ssl->handshake->ciphersuite_info;
7531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007533
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7535 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007536 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007538 }
7539
Gilles Peskine449bd832023-01-11 14:50:10 +01007540 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7541 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007542}
7543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007545{
7546 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7547 ssl->handshake->ciphersuite_info;
7548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007550
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7552 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007553 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007555 }
7556
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7558 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007559}
7560
7561#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7562/* Some certificate support -> implement write and parse */
7563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007565{
7566 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7567 size_t i, n;
7568 const mbedtls_x509_crt *crt;
7569 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7570 ssl->handshake->ciphersuite_info;
7571
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7575 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007576 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007578 }
7579
7580#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7582 if (ssl->handshake->client_auth == 0) {
7583 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007584 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007586 }
7587 }
7588#endif /* MBEDTLS_SSL_CLI_C */
7589#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7591 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007592 /* Should never happen because we shouldn't have picked the
7593 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007595 }
7596 }
7597#endif
7598
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007600
7601 /*
7602 * 0 . 0 handshake type
7603 * 1 . 3 handshake length
7604 * 4 . 6 length of all certs
7605 * 7 . 9 length of cert. 1
7606 * 10 . n-1 peer certificate
7607 * n . n+2 length of cert. 2
7608 * n+3 . ... upper level cert, etc.
7609 */
7610 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007614 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7616 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7617 " > %" MBEDTLS_PRINTF_SIZET,
7618 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7619 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007620 }
7621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7623 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7624 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007627 i += n; crt = crt->next;
7628 }
7629
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7631 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7632 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007633
7634 ssl->out_msglen = i;
7635 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7636 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7637
7638 ssl->state++;
7639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7641 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7642 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007643 }
7644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007646
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007648}
7649
7650#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7651
7652#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007653MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007654static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7655 unsigned char *crt_buf,
7656 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007657{
7658 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7659
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 if (peer_crt == NULL) {
7661 return -1;
7662 }
Jerry Yud9526692022-02-17 14:23:47 +08007663
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 if (peer_crt->raw.len != crt_buf_len) {
7665 return -1;
7666 }
Jerry Yud9526692022-02-17 14:23:47 +08007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007669}
7670#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007671MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007672static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7673 unsigned char *crt_buf,
7674 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007675{
7676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7677 unsigned char const * const peer_cert_digest =
7678 ssl->session->peer_cert_digest;
7679 mbedtls_md_type_t const peer_cert_digest_type =
7680 ssl->session->peer_cert_digest_type;
7681 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007683 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7684 size_t digest_len;
7685
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 if (peer_cert_digest == NULL || digest_info == NULL) {
7687 return -1;
7688 }
Jerry Yud9526692022-02-17 14:23:47 +08007689
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 digest_len = mbedtls_md_get_size(digest_info);
7691 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7692 return -1;
7693 }
Jerry Yud9526692022-02-17 14:23:47 +08007694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7696 if (ret != 0) {
7697 return -1;
7698 }
Jerry Yud9526692022-02-17 14:23:47 +08007699
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007701}
7702#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7703#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7704
7705/*
7706 * Once the certificate message is read, parse it into a cert chain and
7707 * perform basic checks, but leave actual verification to the caller
7708 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007710static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7711 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007712{
7713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7714#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007716#endif
7717 size_t i, n;
7718 uint8_t alert;
7719
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7721 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7722 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7723 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7724 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007725 }
7726
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7728 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7729 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7730 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007731 }
7732
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7734 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7735 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7736 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7737 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007738 }
7739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007741
7742 /*
7743 * Same message structure as in mbedtls_ssl_write_certificate()
7744 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007745 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007746
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 if (ssl->in_msg[i] != 0 ||
7748 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7749 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7750 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7751 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7752 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007753 }
7754
7755 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7756 i += 3;
7757
7758 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007760 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 if (i + 3 > ssl->in_hslen) {
7762 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7763 mbedtls_ssl_send_alert_message(ssl,
7764 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7765 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7766 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007767 }
7768 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7769 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (ssl->in_msg[i] != 0) {
7771 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7772 mbedtls_ssl_send_alert_message(ssl,
7773 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7774 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7775 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007776 }
7777
7778 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007779 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007780 i += 3;
7781
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 if (n < 128 || i + n > ssl->in_hslen) {
7783 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7784 mbedtls_ssl_send_alert_message(ssl,
7785 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7786 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7787 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007788 }
7789
7790 /* Check if we're handling the first CRT in the chain. */
7791#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007793 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007795 /* During client-side renegotiation, check that the server's
7796 * end-CRTs hasn't changed compared to the initial handshake,
7797 * mitigating the triple handshake attack. On success, reuse
7798 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007799 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7800 if (ssl_check_peer_crt_unchanged(ssl,
7801 &ssl->in_msg[i],
7802 n) != 0) {
7803 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7804 mbedtls_ssl_send_alert_message(ssl,
7805 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7806 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7807 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007808 }
7809
7810 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007811 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007812 }
7813#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7814
7815 /* Parse the next certificate in the chain. */
7816#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007818#else
7819 /* If we don't need to store the CRT chain permanently, parse
7820 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007822#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007823 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007824 case 0: /*ok*/
7825 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7826 /* Ignore certificate with an unknown algorithm: maybe a
7827 prior certificate was already trusted. */
7828 break;
7829
7830 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7831 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7832 goto crt_parse_der_failed;
7833
7834 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7835 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7836 goto crt_parse_der_failed;
7837
7838 default:
7839 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007840crt_parse_der_failed:
7841 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7842 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7843 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007844 }
7845
7846 i += n;
7847 }
7848
Gilles Peskine449bd832023-01-11 14:50:10 +01007849 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7850 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007851}
7852
7853#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007855static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007856{
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7858 return -1;
7859 }
Jerry Yud9526692022-02-17 14:23:47 +08007860
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007862 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7863 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7865 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7866 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007867 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007869}
7870#endif /* MBEDTLS_SSL_SRV_C */
7871
7872/* Check if a certificate message is expected.
7873 * Return either
7874 * - SSL_CERTIFICATE_EXPECTED, or
7875 * - SSL_CERTIFICATE_SKIP
7876 * indicating whether a Certificate message is expected or not.
7877 */
7878#define SSL_CERTIFICATE_EXPECTED 0
7879#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007880MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007881static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7882 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007883{
7884 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7885 ssl->handshake->ciphersuite_info;
7886
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7888 return SSL_CERTIFICATE_SKIP;
7889 }
Jerry Yud9526692022-02-17 14:23:47 +08007890
7891#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7893 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7894 return SSL_CERTIFICATE_SKIP;
7895 }
Jerry Yud9526692022-02-17 14:23:47 +08007896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007898 ssl->session_negotiate->verify_result =
7899 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007901 }
7902 }
7903#else
7904 ((void) authmode);
7905#endif /* MBEDTLS_SSL_SRV_C */
7906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007908}
7909
Jerry Yud9526692022-02-17 14:23:47 +08007910#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007911MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007912static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7913 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007914{
7915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7916 /* Remember digest of the peer's end-CRT. */
7917 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7919 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7920 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7921 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7922 mbedtls_ssl_send_alert_message(ssl,
7923 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7924 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007927 }
7928
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 ret = mbedtls_md(mbedtls_md_info_from_type(
7930 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7931 start, len,
7932 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007933
7934 ssl->session_negotiate->peer_cert_digest_type =
7935 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7936 ssl->session_negotiate->peer_cert_digest_len =
7937 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007940}
7941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007942MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007943static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7944 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007945{
7946 unsigned char *end = start + len;
7947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7948
7949 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7951 ret = mbedtls_pk_parse_subpubkey(&start, end,
7952 &ssl->handshake->peer_pubkey);
7953 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007954 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007956 }
7957
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007959}
7960#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7961
Gilles Peskine449bd832023-01-11 14:50:10 +01007962int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007963{
7964 int ret = 0;
7965 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02007966 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007967#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7968 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7969 ? ssl->handshake->sni_authmode
7970 : ssl->conf->authmode;
7971#else
7972 const int authmode = ssl->conf->authmode;
7973#endif
7974 void *rs_ctx = NULL;
7975 mbedtls_x509_crt *chain = NULL;
7976
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007978
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7980 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7981 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007982 goto exit;
7983 }
7984
7985#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if (ssl->handshake->ecrs_enabled &&
7987 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007988 chain = ssl->handshake->ecrs_peer_cert;
7989 ssl->handshake->ecrs_peer_cert = NULL;
7990 goto crt_verify;
7991 }
7992#endif
7993
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007995 /* mbedtls_ssl_read_record may have sent an alert already. We
7996 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007998 goto exit;
7999 }
8000
8001#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008003 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8004
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08008006 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 }
Jerry Yud9526692022-02-17 14:23:47 +08008008
8009 goto exit;
8010 }
8011#endif /* MBEDTLS_SSL_SRV_C */
8012
8013 /* Clear existing peer CRT structure in case we tried to
8014 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008016
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8018 if (chain == NULL) {
8019 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8020 sizeof(mbedtls_x509_crt)));
8021 mbedtls_ssl_send_alert_message(ssl,
8022 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8023 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008024
8025 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8026 goto exit;
8027 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008029
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 ret = ssl_parse_certificate_chain(ssl, chain);
8031 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008032 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008033 }
Jerry Yud9526692022-02-17 14:23:47 +08008034
8035#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008037 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 }
Jerry Yud9526692022-02-17 14:23:47 +08008039
8040crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008042 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 }
Jerry Yud9526692022-02-17 14:23:47 +08008044#endif
8045
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008046 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8047 ssl->handshake->ciphersuite_info,
8048 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008050 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008051 }
Jerry Yud9526692022-02-17 14:23:47 +08008052
8053#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8054 {
8055 unsigned char *crt_start, *pk_start;
8056 size_t crt_len, pk_len;
8057
8058 /* We parse the CRT chain without copying, so
8059 * these pointers point into the input buffer,
8060 * and are hence still valid after freeing the
8061 * CRT chain. */
8062
8063 crt_start = chain->raw.p;
8064 crt_len = chain->raw.len;
8065
8066 pk_start = chain->pk_raw.p;
8067 pk_len = chain->pk_raw.len;
8068
8069 /* Free the CRT structures before computing
8070 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008071 mbedtls_x509_crt_free(chain);
8072 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008073 chain = NULL;
8074
Gilles Peskine449bd832023-01-11 14:50:10 +01008075 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8076 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008078 }
Jerry Yud9526692022-02-17 14:23:47 +08008079
Gilles Peskine449bd832023-01-11 14:50:10 +01008080 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8081 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008082 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008083 }
Jerry Yud9526692022-02-17 14:23:47 +08008084 }
8085#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8086 /* Pass ownership to session structure. */
8087 ssl->session_negotiate->peer_cert = chain;
8088 chain = NULL;
8089#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8090
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008092
8093exit:
8094
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008096 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008097 }
Jerry Yud9526692022-02-17 14:23:47 +08008098
8099#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008100 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008101 ssl->handshake->ecrs_peer_cert = chain;
8102 chain = NULL;
8103 }
8104#endif
8105
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 if (chain != NULL) {
8107 mbedtls_x509_crt_free(chain);
8108 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008109 }
8110
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008112}
8113#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8114
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008115static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8116 unsigned char *padbuf, size_t hlen,
8117 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008118{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008119 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008120 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008121#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008122 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008123 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008124 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008125 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008126#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008128 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008129 mbedtls_md_context_t cloned_ctx;
8130 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008131#endif
8132
8133 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008135 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008137
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008139 ? "client finished"
8140 : "server finished";
8141
8142#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008143 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008144
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008145 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008147 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008148 }
8149
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008150 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008152 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008153 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008154 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008155#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008156 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008157
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008158 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008159 if (ret != 0) {
8160 goto exit;
8161 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008162 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008163 if (ret != 0) {
8164 goto exit;
8165 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008166
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008167 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008168 if (ret != 0) {
8169 goto exit;
8170 }
8171#endif /* MBEDTLS_USE_PSA_CRYPTO */
8172
8173 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8174
Jerry Yu615bd6f2022-02-17 14:25:15 +08008175 /*
8176 * TLSv1.2:
8177 * hash = PRF( master, finished_label,
8178 * Hash( handshake ) )[0.11]
8179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008181 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008182
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008184
Paul Elliottecb95be2023-08-11 16:41:04 +01008185 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008186
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008187 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008188
8189exit:
8190#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008191 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008192 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008193#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008194 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008195 return ret;
8196#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008197}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008198
8199#if defined(MBEDTLS_MD_CAN_SHA256)
8200static int ssl_calc_finished_tls_sha256(
8201 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8202{
8203 unsigned char padbuf[32];
8204 return ssl_calc_finished_tls_generic(ssl,
8205#if defined(MBEDTLS_USE_PSA_CRYPTO)
8206 &ssl->handshake->fin_sha256_psa,
8207#else
8208 &ssl->handshake->fin_sha256,
8209#endif
8210 padbuf, sizeof(padbuf),
8211 buf, from);
8212}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008213#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008214
Jerry Yub7ba49e2022-02-17 14:25:53 +08008215
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008216#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008217static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008219{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008220 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008221 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008223 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008224#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008225 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008226#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008227 padbuf, sizeof(padbuf),
8228 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008229}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008230#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008231
Gilles Peskine449bd832023-01-11 14:50:10 +01008232void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008233{
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008235
8236 /*
8237 * Free our handshake params
8238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 mbedtls_ssl_handshake_free(ssl);
8240 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008241 ssl->handshake = NULL;
8242
8243 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008244 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 if (ssl->transform) {
8247 mbedtls_ssl_transform_free(ssl->transform);
8248 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008249 }
8250 ssl->transform = ssl->transform_negotiate;
8251 ssl->transform_negotiate = NULL;
8252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008254}
8255
Gilles Peskine449bd832023-01-11 14:50:10 +01008256void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008257{
8258 int resume = ssl->handshake->resume;
8259
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008261
8262#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008264 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8265 ssl->renego_records_seen = 0;
8266 }
8267#endif
8268
8269 /*
8270 * Free the previous session and switch in the current one
8271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008273#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8274 /* RFC 7366 3.1: keep the EtM state */
8275 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008277#endif
8278
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 mbedtls_ssl_session_free(ssl->session);
8280 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008281 }
8282 ssl->session = ssl->session_negotiate;
8283 ssl->session_negotiate = NULL;
8284
8285 /*
8286 * Add cache entry
8287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008289 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 resume == 0) {
8291 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8292 ssl->session->id,
8293 ssl->session->id_len,
8294 ssl->session) != 0) {
8295 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8296 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008297 }
8298
8299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8301 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008302 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008304
8305 /* Keep last flight around in case we need to resend it:
8306 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8308 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008309#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008311
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008312 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008315}
8316
Gilles Peskine449bd832023-01-11 14:50:10 +01008317int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008318{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008319 int ret;
8320 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008323
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008325
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008326 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8327 if (ret != 0) {
8328 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8329 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008330
8331 /*
8332 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8333 * may define some other value. Currently (early 2016), no defined
8334 * ciphersuite does this (and this is unlikely to change as activity has
8335 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8336 */
8337 hash_len = 12;
8338
8339#if defined(MBEDTLS_SSL_RENEGOTIATION)
8340 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008342#endif
8343
8344 ssl->out_msglen = 4 + hash_len;
8345 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8346 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8347
8348 /*
8349 * In case of session resuming, invert the client and server
8350 * ChangeCipherSpec messages order.
8351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008353#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008355 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008357#endif
8358#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008360 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008362#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008364 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008366
8367 /*
8368 * Switch to our negotiated transform and session parameters for outbound
8369 * data.
8370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008372
8373#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008375 unsigned char i;
8376
8377 /* Remember current epoch settings for resending */
8378 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8380 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008381
8382 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008384
8385
8386 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 for (i = 2; i > 0; i--) {
8388 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008389 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 }
8391 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008392
8393 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (i == 0) {
8395 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8396 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008397 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008399#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008401
8402 ssl->transform_out = ssl->transform_negotiate;
8403 ssl->session_out = ssl->session_negotiate;
8404
8405#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8407 mbedtls_ssl_send_flight_completed(ssl);
8408 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008409#endif
8410
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8412 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8413 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008414 }
8415
8416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8418 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8419 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8420 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008421 }
8422#endif
8423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008425
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008427}
8428
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008429#define SSL_MAX_HASH_LEN 12
8430
Gilles Peskine449bd832023-01-11 14:50:10 +01008431int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008432{
8433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8434 unsigned int hash_len = 12;
8435 unsigned char buf[SSL_MAX_HASH_LEN];
8436
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008438
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008439 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8440 if (ret != 0) {
8441 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8442 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008443
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8445 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008446 goto exit;
8447 }
8448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8450 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8451 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8452 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008453 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8454 goto exit;
8455 }
8456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8458 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8459 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008460 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8461 goto exit;
8462 }
8463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8465 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8466 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8467 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008468 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8469 goto exit;
8470 }
8471
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8473 buf, hash_len) != 0) {
8474 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8475 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8476 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008477 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8478 goto exit;
8479 }
8480
8481#if defined(MBEDTLS_SSL_RENEGOTIATION)
8482 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008484#endif
8485
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008487#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008489 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008491#endif
8492#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008494 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008496#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008498 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008500
8501#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8503 mbedtls_ssl_recv_flight_completed(ssl);
8504 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008505#endif
8506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008508
8509exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 mbedtls_platform_zeroize(buf, hash_len);
8511 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008512}
8513
Jerry Yu392112c2022-02-17 14:34:10 +08008514#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8515/*
8516 * Helper to get TLS 1.2 PRF from ciphersuite
8517 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008519static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008520{
Jerry Yu392112c2022-02-17 14:34:10 +08008521 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008523#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8525 return tls_prf_sha384;
8526 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008527#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008528#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008529 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8531 return tls_prf_sha256;
8532 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008533 }
8534#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008535#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8536 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008537 (void) ciphersuite_info;
8538#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008541}
8542#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008543
Gilles Peskine449bd832023-01-11 14:50:10 +01008544static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008545{
8546 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008547#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 if (tls_prf == tls_prf_sha384) {
8549 return MBEDTLS_SSL_TLS_PRF_SHA384;
8550 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008551#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008552#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 if (tls_prf == tls_prf_sha256) {
8554 return MBEDTLS_SSL_TLS_PRF_SHA256;
8555 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008556#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008558}
8559
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008560/*
8561 * Populate a transform structure with session keys and all the other
8562 * necessary information.
8563 *
8564 * Parameters:
8565 * - [in/out]: transform: structure to populate
8566 * [in] must be just initialised with mbedtls_ssl_transform_init()
8567 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8568 * - [in] ciphersuite
8569 * - [in] master
8570 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008571 * - [in] tls_prf: pointer to PRF to use for key derivation
8572 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008573 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008574 * - [in] endpoint: client or server
8575 * - [in] ssl: used for:
8576 * - ssl->conf->{f,p}_export_keys
8577 * [in] optionally used for:
8578 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8579 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008580MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008581static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8582 int ciphersuite,
8583 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008584#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008586#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 ssl_tls_prf_t tls_prf,
8588 const unsigned char randbytes[64],
8589 mbedtls_ssl_protocol_version tls_version,
8590 unsigned endpoint,
8591 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008592{
8593 int ret = 0;
8594 unsigned char keyblk[256];
8595 unsigned char *key1;
8596 unsigned char *key2;
8597 unsigned char *mac_enc;
8598 unsigned char *mac_dec;
8599 size_t mac_key_len = 0;
8600 size_t iv_copy_len;
8601 size_t keylen;
8602 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008603 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008604#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008605 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008606 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008607#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008608
8609#if defined(MBEDTLS_USE_PSA_CRYPTO)
8610 psa_key_type_t key_type;
8611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8612 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008613 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008614 size_t key_bits;
8615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8616#endif
8617
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008618 /*
8619 * Some data just needs copying into the structure
8620 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008621#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008622 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008623#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008624 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008625
8626#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008628#endif
8629
8630#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008632 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8633 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008635 }
8636#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8637
8638 /*
8639 * Get various info structures
8640 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8642 if (ciphersuite_info == NULL) {
8643 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8644 ciphersuite));
8645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008646 }
8647
Neil Armstrongab555e02022-04-04 11:07:59 +02008648 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008649#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008651#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008653
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008655 transform->taglen =
8656 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008657 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008658
8659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008660 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 transform->taglen,
8662 &alg,
8663 &key_type,
8664 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008665 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008667 goto end;
8668 }
8669#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008670 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 if (cipher_info == NULL) {
8672 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8673 ciphersuite_info->cipher));
8674 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008675 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008677
Neil Armstronge4512952022-03-08 09:08:22 +01008678#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008679 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008681 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 (unsigned) ciphersuite_info->mac));
8683 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008684 }
8685#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008686 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 if (md_info == NULL) {
8688 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8689 (unsigned) ciphersuite_info->mac));
8690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008691 }
Neil Armstronge4512952022-03-08 09:08:22 +01008692#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008693
8694#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8695 /* Copy own and peer's CID if the use of the CID
8696 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8698 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008699
8700 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8702 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8703 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008704
8705 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8707 ssl->handshake->peer_cid_len);
8708 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8709 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008710 }
8711#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8712
8713 /*
8714 * Compute key block using the PRF
8715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8717 if (ret != 0) {
8718 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8719 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008720 }
8721
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8723 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8724 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8725 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8726 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008727
8728 /*
8729 * Determine the appropriate key, IV and MAC length.
8730 */
8731
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008732#if defined(MBEDTLS_USE_PSA_CRYPTO)
8733 keylen = PSA_BITS_TO_BYTES(key_bits);
8734#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008736#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008737
Valerio Settie5707042023-10-11 11:54:42 +02008738#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008740 size_t explicit_ivlen;
8741
8742 transform->maclen = 0;
8743 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008744
8745 /* All modes haves 96-bit IVs, but the length of the static parts vary
8746 * with mode and version:
8747 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8748 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8749 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8750 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8751 * sequence number).
8752 */
8753 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008754
8755 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008756#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008758#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8760 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008761#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008762
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008764 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008768
8769 /* Minimum length of encrypted record */
8770 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8771 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 } else
Valerio Settie5707042023-10-11 11:54:42 +02008773#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008774#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008776 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008778#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008780#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008781 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008782#endif /* MBEDTLS_USE_PSA_CRYPTO */
8783
8784#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008785 /* Get MAC length */
8786 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8787#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008788 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8790 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8791 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008792 goto end;
8793 }
8794
8795 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008797#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008798 transform->maclen = mac_key_len;
8799
8800 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008801#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008803#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008804 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008805#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008806
8807 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008809 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008811 /*
8812 * GenericBlockCipher:
8813 * 1. if EtM is in use: one block plus MAC
8814 * otherwise: * first multiple of blocklen greater than maclen
8815 * 2. IV
8816 */
8817#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008819 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 + block_size;
8821 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008822#endif
8823 {
8824 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 + block_size
8826 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008827 }
8828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008830 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 } else {
8832 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008833 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8834 goto end;
8835 }
8836 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008838#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8839 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8841 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008842 }
8843
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8845 (unsigned) keylen,
8846 (unsigned) transform->minlen,
8847 (unsigned) transform->ivlen,
8848 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008849
8850 /*
8851 * Finally setup the cipher contexts, IVs and MAC secrets.
8852 */
8853#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008855 key1 = keyblk + mac_key_len * 2;
8856 key2 = keyblk + mac_key_len * 2 + keylen;
8857
8858 mac_enc = keyblk;
8859 mac_dec = keyblk + mac_key_len;
8860
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 iv_copy_len = (transform->fixed_ivlen) ?
8862 transform->fixed_ivlen : transform->ivlen;
8863 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8864 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8865 iv_copy_len);
8866 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008867#endif /* MBEDTLS_SSL_CLI_C */
8868#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008870 key1 = keyblk + mac_key_len * 2 + keylen;
8871 key2 = keyblk + mac_key_len * 2;
8872
8873 mac_enc = keyblk + mac_key_len;
8874 mac_dec = keyblk;
8875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 iv_copy_len = (transform->fixed_ivlen) ?
8877 transform->fixed_ivlen : transform->ivlen;
8878 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8879 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8880 iv_copy_len);
8881 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008882#endif /* MBEDTLS_SSL_SRV_C */
8883 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008885 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8886 goto end;
8887 }
8888
Paul Elliott4fb19552023-10-18 12:15:30 +01008889 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 ssl->f_export_keys(ssl->p_export_keys,
8891 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8892 master, 48,
8893 randbytes + 32,
8894 randbytes,
8895 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008896 }
8897
8898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008899 transform->psa_alg = alg;
8900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8902 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8903 psa_set_key_algorithm(&attributes, alg);
8904 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008905
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 if ((status = psa_import_key(&attributes,
8907 key1,
8908 PSA_BITS_TO_BYTES(key_bits),
8909 &transform->psa_key_enc)) != PSA_SUCCESS) {
8910 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008911 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008913 goto end;
8914 }
8915
Gilles Peskine449bd832023-01-11 14:50:10 +01008916 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008917
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 if ((status = psa_import_key(&attributes,
8919 key2,
8920 PSA_BITS_TO_BYTES(key_bits),
8921 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008922 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008924 goto end;
8925 }
8926 }
8927#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8929 cipher_info)) != 0) {
8930 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008931 goto end;
8932 }
8933
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8935 cipher_info)) != 0) {
8936 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008937 goto end;
8938 }
8939
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8941 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8942 MBEDTLS_ENCRYPT)) != 0) {
8943 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008944 goto end;
8945 }
8946
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8948 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8949 MBEDTLS_DECRYPT)) != 0) {
8950 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008951 goto end;
8952 }
8953
8954#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008955 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8956 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8957 MBEDTLS_PADDING_NONE)) != 0) {
8958 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008959 goto end;
8960 }
8961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8963 MBEDTLS_PADDING_NONE)) != 0) {
8964 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008965 goto end;
8966 }
8967 }
8968#endif /* MBEDTLS_CIPHER_MODE_CBC */
8969#endif /* MBEDTLS_USE_PSA_CRYPTO */
8970
Neil Armstrong29c0c042022-03-17 17:47:28 +01008971#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8972 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8973 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008975#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8979 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8980 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008981
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 if ((status = psa_import_key(&attributes,
8983 mac_enc, mac_key_len,
8984 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008985 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008987 goto end;
8988 }
8989
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8991 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008992#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008994#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008996 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8998 PSA_KEY_USAGE_VERIFY_HASH);
8999 } else {
9000 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
9001 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 if ((status = psa_import_key(&attributes,
9004 mac_dec, mac_key_len,
9005 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009006 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009008 goto end;
9009 }
9010#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9012 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009013 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 }
9015 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9016 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009017 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009019#endif /* MBEDTLS_USE_PSA_CRYPTO */
9020 }
9021#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9022
9023 ((void) mac_dec);
9024 ((void) mac_enc);
9025
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009026end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9028 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009029}
9030
Valerio Settia08b1a42022-11-17 15:10:02 +01009031#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9032 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009033int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 psa_pake_operation_t *pake_ctx,
9035 const unsigned char *buf,
9036 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009037{
9038 psa_status_t status;
9039 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009040 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009041 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9042 * At round two perform a single cycle
9043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 for (; remaining_steps > 0; remaining_steps--) {
9047 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009048 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009050 /* Length is stored at the first byte */
9051 size_t length = buf[input_offset];
9052 input_offset += 1;
9053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009055 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9056 }
9057
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 status = psa_pake_input(pake_ctx, step,
9059 buf + input_offset, length);
9060 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009061 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009062 }
9063
9064 input_offset += length;
9065 }
9066 }
9067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009069 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009073}
9074
Valerio Setti6b3dab02022-11-17 17:14:54 +01009075int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009076 psa_pake_operation_t *pake_ctx,
9077 unsigned char *buf,
9078 size_t len, size_t *olen,
9079 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009080{
9081 psa_status_t status;
9082 size_t output_offset = 0;
9083 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009084 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009085 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9086 * At round two perform a single cycle
9087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 for (; remaining_steps > 0; remaining_steps--) {
9091 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9092 step <= PSA_PAKE_STEP_ZK_PROOF;
9093 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009094 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009095 * For each step, prepend 1 byte with the length of the data as
9096 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 status = psa_pake_output(pake_ctx, step,
9099 buf + output_offset + 1,
9100 len - output_offset - 1,
9101 &output_len);
9102 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009103 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009104 }
9105
Valerio Setti99d88c12022-11-22 16:03:43 +01009106 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009107
9108 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009109 }
9110 }
9111
9112 *olen = output_offset;
9113
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009115}
Valerio Settia08b1a42022-11-17 15:10:02 +01009116#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9117
Jerry Yuee40f9d2022-02-17 14:55:16 +08009118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009119int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9120 unsigned char *hash, size_t *hashlen,
9121 unsigned char *data, size_t data_len,
9122 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009123{
9124 psa_status_t status;
9125 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009126 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009129
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 if ((status = psa_hash_setup(&hash_operation,
9131 hash_alg)) != PSA_SUCCESS) {
9132 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009133 goto exit;
9134 }
9135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9137 64)) != PSA_SUCCESS) {
9138 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009139 goto exit;
9140 }
9141
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if ((status = psa_hash_update(&hash_operation,
9143 data, data_len)) != PSA_SUCCESS) {
9144 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009145 goto exit;
9146 }
9147
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9149 hashlen)) != PSA_SUCCESS) {
9150 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9151 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009152 }
9153
9154exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 if (status != PSA_SUCCESS) {
9156 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9157 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9158 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009159 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009161 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9162 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009164 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009166 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009168 }
9169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009171}
9172
9173#else
9174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9176 unsigned char *hash, size_t *hashlen,
9177 unsigned char *data, size_t data_len,
9178 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009179{
9180 int ret = 0;
9181 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9183 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009188
9189 /*
9190 * digitally-signed struct {
9191 * opaque client_random[32];
9192 * opaque server_random[32];
9193 * ServerDHParams params;
9194 * };
9195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009198 goto exit;
9199 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9201 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009202 goto exit;
9203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9205 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009206 goto exit;
9207 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009208 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9209 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009210 goto exit;
9211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9213 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009214 goto exit;
9215 }
9216
9217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 if (ret != 0) {
9221 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9222 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9223 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009226}
9227#endif /* MBEDTLS_USE_PSA_CRYPTO */
9228
Jerry Yud9d91da2022-02-17 14:57:06 +08009229#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9230
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009231/* Find the preferred hash for a given signature algorithm. */
9232unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 mbedtls_ssl_context *ssl,
9234 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009235{
Gabor Mezei078e8032022-04-27 21:17:56 +02009236 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009237 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9240 return MBEDTLS_SSL_HASH_NONE;
9241 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009244 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9246 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009247 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9249 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009250
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009251 mbedtls_md_type_t md_alg =
9252 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9253 if (md_alg == MBEDTLS_MD_NONE) {
9254 continue;
9255 }
9256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009259 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009260 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009261 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009262
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9264 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9265 PSA_ALG_ECDSA(psa_hash_alg),
9266 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009267 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9271 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9272 PSA_ALG_RSA_PKCS1V15_SIGN(
9273 psa_hash_alg),
9274 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009275 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009277 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009278#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009279
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009281 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009282 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009283
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009285}
9286
9287#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9288
Jerry Yudc7bd172022-02-17 13:44:15 +08009289#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9290
XiaokangQian75d40ef2022-04-20 11:05:24 +00009291int mbedtls_ssl_validate_ciphersuite(
9292 const mbedtls_ssl_context *ssl,
9293 const mbedtls_ssl_ciphersuite_t *suite_info,
9294 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009296{
9297 (void) ssl;
9298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 if (suite_info == NULL) {
9300 return -1;
9301 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009302
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 if ((suite_info->min_tls_version > max_tls_version) ||
9304 (suite_info->max_tls_version < min_tls_version)) {
9305 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009306 }
9307
XiaokangQian060d8672022-04-21 09:24:56 +00009308#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009309#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009310#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9312 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009313#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9315 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009316#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009317 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009319 }
9320#endif
9321
9322 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9323#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9325 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9326 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009327 }
9328#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9329#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9330
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009332}
9333
Ronald Crone68ab4f2022-10-05 12:46:29 +02009334#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009335/*
9336 * Function for writing a signature algorithm extension.
9337 *
9338 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9339 * value (TLS 1.3 RFC8446):
9340 * enum {
9341 * ....
9342 * ecdsa_secp256r1_sha256( 0x0403 ),
9343 * ecdsa_secp384r1_sha384( 0x0503 ),
9344 * ecdsa_secp521r1_sha512( 0x0603 ),
9345 * ....
9346 * } SignatureScheme;
9347 *
9348 * struct {
9349 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9350 * } SignatureSchemeList;
9351 *
9352 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9353 * value (TLS 1.2 RFC5246):
9354 * enum {
9355 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9356 * sha512(6), (255)
9357 * } HashAlgorithm;
9358 *
9359 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9360 * SignatureAlgorithm;
9361 *
9362 * struct {
9363 * HashAlgorithm hash;
9364 * SignatureAlgorithm signature;
9365 * } SignatureAndHashAlgorithm;
9366 *
9367 * SignatureAndHashAlgorithm
9368 * supported_signature_algorithms<2..2^16-2>;
9369 *
9370 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9371 * generalization of the TLS 1.2 signature algorithm extension.
9372 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9373 * `SignatureScheme` field of TLS 1.3
9374 *
9375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009376int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9377 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009378{
9379 unsigned char *p = buf;
9380 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9381 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9382
9383 *out_len = 0;
9384
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009386
9387 /* Check if we have space for header and length field:
9388 * - extension_type (2 bytes)
9389 * - extension_data_length (2 bytes)
9390 * - supported_signature_algorithms_length (2 bytes)
9391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009392 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009393 p += 6;
9394
9395 /*
9396 * Write supported_signature_algorithms
9397 */
9398 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9400 if (sig_alg == NULL) {
9401 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9402 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009403
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9405 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9406 *sig_alg,
9407 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9408 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009409 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009410 }
9411 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9412 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009413 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9415 *sig_alg,
9416 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009417 }
9418
9419 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009420 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 if (supported_sig_alg_len == 0) {
9422 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9423 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009424 }
9425
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9427 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9428 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009429
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009430 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009431
9432#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009434#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009437}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009438#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009439
XiaokangQian40a35232022-05-07 09:02:40 +00009440#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009441/*
9442 * mbedtls_ssl_parse_server_name_ext
9443 *
9444 * Structure of server_name extension:
9445 *
9446 * enum {
9447 * host_name(0), (255)
9448 * } NameType;
9449 * opaque HostName<1..2^16-1>;
9450 *
9451 * struct {
9452 * NameType name_type;
9453 * select (name_type) {
9454 * case host_name: HostName;
9455 * } name;
9456 * } ServerName;
9457 * struct {
9458 * ServerName server_name_list<1..2^16-1>
9459 * } ServerNameList;
9460 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009462int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9463 const unsigned char *buf,
9464 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009465{
9466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9467 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009468 size_t server_name_list_len, hostname_len;
9469 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009472
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9474 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009475 p += 2;
9476
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009478 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 while (p < server_name_list_end) {
9480 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9481 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9482 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9483 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009484
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009486 /* sni_name is intended to be used only during the parsing of the
9487 * ClientHello message (it is reset to NULL before the end of
9488 * the message parsing). Thus it is ok to just point to the
9489 * reception buffer and not make a copy of it.
9490 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009491 ssl->handshake->sni_name = p + 3;
9492 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 if (ssl->conf->f_sni == NULL) {
9494 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009495 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9497 ssl, p + 3, hostname_len);
9498 if (ret != 0) {
9499 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9500 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9501 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9502 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9503 }
9504 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009505 }
9506
9507 p += hostname_len + 3;
9508 }
9509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009511}
9512#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9513
XiaokangQianacb39922022-06-17 10:18:48 +00009514#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009515MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009516int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9517 const unsigned char *buf,
9518 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009519{
9520 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009521 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009522 const unsigned char *protocol_name_list;
9523 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009524 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009525
9526 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 if (ssl->conf->alpn_list == NULL) {
9528 return 0;
9529 }
XiaokangQianacb39922022-06-17 10:18:48 +00009530
9531 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009532 * RFC7301, section 3.1
9533 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009534 *
XiaokangQianc7403452022-06-23 03:24:12 +00009535 * struct {
9536 * ProtocolName protocol_name_list<2..2^16-1>
9537 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009538 */
9539
XiaokangQianc7403452022-06-23 03:24:12 +00009540 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009541 * protocol_name_list_len 2 bytes
9542 * protocol_name_len 1 bytes
9543 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009548 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009550 protocol_name_list = p;
9551 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009552
9553 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009555 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9557 protocol_name_len);
9558 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009559 MBEDTLS_SSL_PEND_FATAL_ALERT(
9560 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9562 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 }
9564
9565 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009566 }
9567
9568 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9570 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009571 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009573 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 if (protocol_name_len == alpn_len &&
9575 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009576 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009578 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009579
9580 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009581 }
9582 }
9583
XiaokangQian95d5f542022-06-24 02:29:26 +00009584 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009585 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9587 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9588 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009589}
9590
Gilles Peskine449bd832023-01-11 14:50:10 +01009591int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9592 unsigned char *buf,
9593 unsigned char *end,
9594 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009595{
9596 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009597 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009598 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009599
Gilles Peskine449bd832023-01-11 14:50:10 +01009600 if (ssl->alpn_chosen == NULL) {
9601 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009602 }
9603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 protocol_name_len = strlen(ssl->alpn_chosen);
9605 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009606
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009608 /*
9609 * 0 . 1 ext identifier
9610 * 2 . 3 ext length
9611 * 4 . 5 protocol list length
9612 * 6 . 6 protocol name length
9613 * 7 . 7+n protocol name
9614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009616
XiaokangQian95d5f542022-06-24 02:29:26 +00009617 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009618
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9620 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009621 /* Note: the length of the chosen protocol has been checked to be less
9622 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9623 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009625
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009627
9628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009630#endif
9631
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009633}
9634#endif /* MBEDTLS_SSL_ALPN */
9635
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009636#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009637 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009638 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009639 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009640int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9641 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009642{
9643 /* Initialize to suppress unnecessary compiler warning */
9644 size_t hostname_len = 0;
9645
9646 /* Check if new hostname is valid before
9647 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (hostname != NULL) {
9649 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009650
Gilles Peskine449bd832023-01-11 14:50:10 +01009651 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9652 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9653 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009654 }
9655
9656 /* Now it's clear that we will overwrite the old hostname,
9657 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009659 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009661 }
9662
9663 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009665 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009666 } else {
9667 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9668 if (session->hostname == NULL) {
9669 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9670 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009673 }
9674
Gilles Peskine449bd832023-01-11 14:50:10 +01009675 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009676}
Xiaokang Qian03409292022-10-12 02:49:52 +00009677#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9678 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009679 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009680 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009681
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009682#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9683 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009684int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9685 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009686{
9687 size_t alpn_len = 0;
9688
9689 if (alpn != NULL) {
9690 alpn_len = strlen(alpn);
9691
9692 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9694 }
9695 }
9696
9697 if (session->ticket_alpn != NULL) {
9698 mbedtls_zeroize_and_free(session->ticket_alpn,
9699 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009700 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009701 }
9702
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009703 if (alpn != NULL) {
9704 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009705 if (session->ticket_alpn == NULL) {
9706 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9707 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009708 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009709 }
9710
9711 return 0;
9712}
9713#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009714
9715/*
9716 * The following functions are used by 1.2 and 1.3, client and server.
9717 */
9718#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9719int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9720 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9721 int recv_endpoint,
9722 mbedtls_ssl_protocol_version tls_version,
9723 uint32_t *flags)
9724{
9725 int ret = 0;
9726 unsigned int usage = 0;
9727 const char *ext_oid;
9728 size_t ext_len;
9729
9730 /*
9731 * keyUsage
9732 */
9733
9734 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9735 * to check what a compliant client will think while choosing which cert
9736 * to send to the client. */
9737#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9738 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9739 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9740 /* TLS 1.2 server part of the key exchange */
9741 switch (ciphersuite->key_exchange) {
9742 case MBEDTLS_KEY_EXCHANGE_RSA:
9743 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9744 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9745 break;
9746
9747 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9748 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9749 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9750 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9751 break;
9752
9753 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9754 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9755 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9756 break;
9757
9758 /* Don't use default: we want warnings when adding new values */
9759 case MBEDTLS_KEY_EXCHANGE_NONE:
9760 case MBEDTLS_KEY_EXCHANGE_PSK:
9761 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9762 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9763 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9764 usage = 0;
9765 }
9766 } else
9767#endif
9768 {
9769 /* This is either TLS 1.3 authentication, which always uses signatures,
9770 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9771 * options we implement, both using signatures. */
9772 (void) tls_version;
9773 (void) ciphersuite;
9774 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9775 }
9776
9777 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9778 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9779 ret = -1;
9780 }
9781
9782 /*
9783 * extKeyUsage
9784 */
9785
9786 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9787 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9788 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9789 } else {
9790 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9791 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9792 }
9793
9794 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9795 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9796 ret = -1;
9797 }
9798
9799 return ret;
9800}
9801
9802int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9803 int authmode,
9804 mbedtls_x509_crt *chain,
9805 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9806 void *rs_ctx)
9807{
9808 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9809 return 0;
9810 }
9811
9812 /*
9813 * Primary check: use the appropriate X.509 verification function
9814 */
9815 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9816 void *p_vrfy;
9817 if (ssl->f_vrfy != NULL) {
9818 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9819 f_vrfy = ssl->f_vrfy;
9820 p_vrfy = ssl->p_vrfy;
9821 } else {
9822 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9823 f_vrfy = ssl->conf->f_vrfy;
9824 p_vrfy = ssl->conf->p_vrfy;
9825 }
9826
9827 int ret = 0;
9828 int have_ca_chain_or_callback = 0;
9829#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9830 if (ssl->conf->f_ca_cb != NULL) {
9831 ((void) rs_ctx);
9832 have_ca_chain_or_callback = 1;
9833
9834 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9835 ret = mbedtls_x509_crt_verify_with_ca_cb(
9836 chain,
9837 ssl->conf->f_ca_cb,
9838 ssl->conf->p_ca_cb,
9839 ssl->conf->cert_profile,
9840 ssl->hostname,
9841 &ssl->session_negotiate->verify_result,
9842 f_vrfy, p_vrfy);
9843 } else
9844#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9845 {
9846 mbedtls_x509_crt *ca_chain;
9847 mbedtls_x509_crl *ca_crl;
9848#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9849 if (ssl->handshake->sni_ca_chain != NULL) {
9850 ca_chain = ssl->handshake->sni_ca_chain;
9851 ca_crl = ssl->handshake->sni_ca_crl;
9852 } else
9853#endif
9854 {
9855 ca_chain = ssl->conf->ca_chain;
9856 ca_crl = ssl->conf->ca_crl;
9857 }
9858
9859 if (ca_chain != NULL) {
9860 have_ca_chain_or_callback = 1;
9861 }
9862
9863 ret = mbedtls_x509_crt_verify_restartable(
9864 chain,
9865 ca_chain, ca_crl,
9866 ssl->conf->cert_profile,
9867 ssl->hostname,
9868 &ssl->session_negotiate->verify_result,
9869 f_vrfy, p_vrfy, rs_ctx);
9870 }
9871
9872 if (ret != 0) {
9873 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9874 }
9875
9876#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9877 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9878 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9879 }
9880#endif
9881
9882 /*
9883 * Secondary checks: always done, but change 'ret' only if it was 0
9884 */
9885
9886 /* With TLS 1.2 and ECC certs, check that the curve used by the
9887 * certificate is on our list of acceptable curves.
9888 *
9889 * With TLS 1.3 this is not needed because the curve is part of the
9890 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9891 * we validate the signature made with the key associated to this cert.
9892 */
9893#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9894 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9895 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9896 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9897 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9898 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9899 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9900 if (ret == 0) {
9901 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9902 }
9903 }
9904 }
9905#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9906
9907 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9908 if (mbedtls_ssl_check_cert_usage(chain,
9909 ciphersuite_info,
9910 ssl->conf->endpoint,
9911 ssl->tls_version,
9912 &ssl->session_negotiate->verify_result) != 0) {
9913 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9914 if (ret == 0) {
9915 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9916 }
9917 }
9918
9919 /* With authmode optional, we want to keep going if the certificate was
9920 * unacceptable, but still fail on other errors (out of memory etc),
9921 * including fatal errors from the f_vrfy callback.
9922 *
9923 * The only acceptable errors are:
9924 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9925 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9926 * Anything else is a fatal error. */
9927 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9928 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9929 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9930 ret = 0;
9931 }
9932
9933 /* Return a specific error as this is a user error: inconsistent
9934 * configuration - can't verify without trust anchors. */
9935 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9936 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9937 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9938 }
9939
9940 if (ret != 0) {
9941 uint8_t alert;
9942
9943 /* The certificate may have been rejected for several reasons.
9944 Pick one and send the corresponding alert. Which alert to send
9945 may be a subject of debate in some cases. */
9946 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9947 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9948 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9949 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9950 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9951 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9952 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9953 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9954 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9955 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9956 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9957 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9958 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9959 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9960 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9961 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9962 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9963 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9964 } else {
9965 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9966 }
9967 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9968 alert);
9969 }
9970
9971#if defined(MBEDTLS_DEBUG_C)
9972 if (ssl->session_negotiate->verify_result != 0) {
9973 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9974 (unsigned int) ssl->session_negotiate->verify_result));
9975 } else {
9976 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9977 }
9978#endif /* MBEDTLS_DEBUG_C */
9979
9980 return ret;
9981}
9982#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984#endif /* MBEDTLS_SSL_TLS_C */