blob: 0c394946a5f49210905ffb5bf4eb27c04bcceae2 [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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 written_in = ssl->in_msg - ssl->in_buf;
351 iv_offset_in = ssl->in_iv - ssl->in_buf;
352 len_offset_in = ssl->in_len - ssl->in_buf;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000353 hdr_in = ssl->in_hdr - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200355 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 ssl->in_buf_len < in_buf_new_len) {
357 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
358 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
359 } else {
360 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
361 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200362 modified = 1;
363 }
364 }
365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200368 written_out = ssl->out_msg - ssl->out_buf;
369 iv_offset_out = ssl->out_iv - ssl->out_buf;
370 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 ssl->out_buf_len < out_buf_new_len) {
374 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
375 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
376 } else {
377 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
378 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 modified = 1;
380 }
381 }
382 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200384 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000385 ssl->in_hdr = ssl->in_buf + hdr_in;
386 mbedtls_ssl_update_in_pointers(ssl);
387 mbedtls_ssl_reset_out_pointers(ssl);
388
Andrzej Kurek4a063792020-10-21 15:08:44 +0200389 /* Fields below might not be properly updated with record
390 * splitting or with CID, so they are manually updated here. */
391 ssl->out_msg = ssl->out_buf + written_out;
392 ssl->out_len = ssl->out_buf + len_offset_out;
393 ssl->out_iv = ssl->out_buf + iv_offset_out;
394
395 ssl->in_msg = ssl->in_buf + written_in;
396 ssl->in_len = ssl->in_buf + len_offset_in;
397 ssl->in_iv = ssl->in_buf + iv_offset_in;
398 }
399}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500400#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
401
Jerry Yudb8c48a2022-01-27 14:54:54 +0800402#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800403
404#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100405typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
406 const char *label,
407 const unsigned char *random, size_t rlen,
408 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800411
412#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
413
414/* Type for the TLS PRF */
415typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
416 const unsigned char *, size_t,
417 unsigned char *, size_t);
418
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200419MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100420static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
421 int ciphersuite,
422 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200423#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200425#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ssl_tls_prf_t tls_prf,
427 const unsigned char randbytes[64],
428 mbedtls_ssl_protocol_version tls_version,
429 unsigned endpoint,
430 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800431
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100432#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200433MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100434static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300435 const char *label,
436 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100438static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
439static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100440
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100441#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100442
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100443#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100444MBEDTLS_CHECK_RETURN_CRITICAL
445static int tls_prf_sha384(const unsigned char *secret, size_t slen,
446 const char *label,
447 const unsigned char *random, size_t rlen,
448 unsigned char *dstbuf, size_t dlen);
449
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
451static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100452#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454MBEDTLS_CHECK_RETURN_CRITICAL
455static int ssl_tls12_session_load(mbedtls_ssl_session *session,
456 const unsigned char *buf,
457 size_t len);
458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
459
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100460static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100461
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100462#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100463static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100464#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100465
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100466#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100467static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100468#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100469
470int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
471 const unsigned char *secret, size_t slen,
472 const char *label,
473 const unsigned char *random, size_t rlen,
474 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300475{
476 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100480#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300481 case MBEDTLS_SSL_TLS_PRF_SHA384:
482 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100484#endif /* MBEDTLS_MD_CAN_SHA384*/
485#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300486 case MBEDTLS_SSL_TLS_PRF_SHA256:
487 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100489#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 default:
492 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 }
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496}
497
Jerry Yuc73c6182022-02-08 20:29:25 +0800498#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800500{
501#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (session->peer_cert != NULL) {
503 mbedtls_x509_crt_free(session->peer_cert);
504 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800505 session->peer_cert = NULL;
506 }
507#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800509 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800511 session->peer_cert_digest = NULL;
512 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
513 session->peer_cert_digest_len = 0;
514 }
515#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
516}
517#endif /* MBEDTLS_X509_CRT_PARSE_C */
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800520{
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800522 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800524
525 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800527
528 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800530
531 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800533
534 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800536
537 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800539
540 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800542
543 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800545
546 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800548
549 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800551
552 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800554
555 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800557
558 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800560
561 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800563
564 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800566
567 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800569
570 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800572
573 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800575
576 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800578
579 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800581
582 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800584
585 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800587
588 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800590
591 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593
594 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800596
597 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800599
Jan Bruckner151f6422023-02-10 12:45:19 +0100600 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
601 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
602
Jerry Yu7a485c12022-10-31 13:08:18 +0800603 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800605
606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800609}
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800612{
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800614}
615
Jerry Yud25cab02022-10-31 12:48:30 +0800616#if defined(MBEDTLS_DEBUG_C)
617static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800618 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800619 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
620 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
621 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
622 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
624 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
625 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
626 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
627 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
628 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
629 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
630 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
631 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
632 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
633 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
634 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
635 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
636 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
637 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
638 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
639 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
640 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
641 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
642 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
643 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
644 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100645 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
646 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800647};
648
Chien Wong4e9683e2023-12-28 17:07:43 +0800649static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800650 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
651 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
652 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
653 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
654 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
655 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
656 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
657 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
658 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
659 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
660 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
661 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
662 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
663 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
664 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
665 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
666 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
667 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
668 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
669 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
670 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
671 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
672 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
673 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
674 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
675 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
676 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100677 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
678 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800679};
680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800682{
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return extension_name_table[
684 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800685}
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800688{
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800690 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800692 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800694 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800696 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800698 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800700 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800702 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800704 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800706}
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
709 int level, const char *file, int line,
710 int hs_msg_type, unsigned int extension_type,
711 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800712{
713 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800715 mbedtls_debug_print_msg(
716 ssl, level, file, line,
717 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 ssl_tls13_get_hs_msg_name(hs_msg_type),
719 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800720 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800722 return;
723 }
724
725 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800727 mbedtls_debug_print_msg(
728 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
730 mbedtls_ssl_get_extension_name(extension_type), extension_type,
731 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800732 return;
733 }
734
735 mbedtls_debug_print_msg(
736 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
738 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800739}
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
742 int level, const char *file, int line,
743 int hs_msg_type, uint32_t extensions_mask,
744 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800745{
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 for (unsigned i = 0;
748 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
749 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800750 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800751 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800753 }
754}
755
Pengyu Lvee455c02023-01-12 14:37:24 +0800756#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800757static const char *ticket_flag_name_table[] =
758{
759 [0] = "ALLOW_PSK_RESUMPTION",
760 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
761 [3] = "ALLOW_EARLY_DATA",
762};
763
Pengyu Lv4938a562023-01-16 11:28:49 +0800764void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
765 int level, const char *file, int line,
766 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800767{
768 size_t i;
769
770 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800771 "print ticket_flags (0x%02x)", flags);
772
773 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800774
775 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800776 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800777 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
778 ticket_flag_name_table[i]);
779 }
780 }
781}
782#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
783
Jerry Yud25cab02022-10-31 12:48:30 +0800784#endif /* MBEDTLS_DEBUG_C */
785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
787 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800788{
789 ((void) ciphersuite_info);
790
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100791#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800793 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800795#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100796#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800798 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800800#endif
801 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800803 return;
804 }
805}
806
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100807int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100808 unsigned hs_type,
809 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
811 unsigned char hs_hdr[4];
812
813 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
815 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
816 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
817 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100818
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100819 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100820}
821
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100822int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100823 unsigned hs_type,
824 unsigned char const *msg,
825 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100826{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100827 int ret;
828 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100829 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100830 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100831 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100832 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100833}
834
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100835int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800836{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100837#if defined(MBEDTLS_MD_CAN_SHA256) || \
838 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100839#if defined(MBEDTLS_USE_PSA_CRYPTO)
840 psa_status_t status;
841#else
842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
843#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100844#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800845 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100846#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100847#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100849 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
850 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200851 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100852 }
853 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
854 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200855 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800857#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100858 mbedtls_md_free(&ssl->handshake->fin_sha256);
859 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100860 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
861 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
862 0);
863 if (ret != 0) {
864 return ret;
865 }
866 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100867 if (ret != 0) {
868 return ret;
869 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800870#endif
871#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100872#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
875 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200876 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100877 }
878 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
879 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200880 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100881 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800882#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100883 mbedtls_md_free(&ssl->handshake->fin_sha384);
884 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100885 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
886 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
887 if (ret != 0) {
888 return ret;
889 }
890 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100891 if (ret != 0) {
892 return ret;
893 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800894#endif
895#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100896 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800897}
898
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100899static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100900 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800901{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100902#if defined(MBEDTLS_MD_CAN_SHA256) || \
903 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100904#if defined(MBEDTLS_USE_PSA_CRYPTO)
905 psa_status_t status;
906#else
907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
908#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100909#else /* SHA-256 or SHA-384 */
910 ((void) ssl);
911 (void) buf;
912 (void) len;
913#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100914#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800915#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100916 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
917 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200918 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100919 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800920#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100921 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100922 if (ret != 0) {
923 return ret;
924 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800925#endif
926#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100927#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800928#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
930 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200931 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100932 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800933#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100934 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100935 if (ret != 0) {
936 return ret;
937 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800938#endif
939#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100940 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800941}
942
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100943#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100944static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100945 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800946{
947#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200948 return mbedtls_md_error_from_psa(psa_hash_update(
949 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800950#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100951 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800952#endif
953}
954#endif
955
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100956#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100957static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100958 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800959{
960#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200961 return mbedtls_md_error_from_psa(psa_hash_update(
962 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800963#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100964 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800965#endif
966}
967#endif
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100973#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500974#if defined(MBEDTLS_USE_PSA_CRYPTO)
975 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100977 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500979#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100980#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500982 handshake->fin_sha384_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_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500986#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200987
988 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200993#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200994 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200996#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200997#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200998#if defined(MBEDTLS_USE_PSA_CRYPTO)
999 handshake->psa_pake_ctx = psa_pake_operation_init();
1000 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1001#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001004#if defined(MBEDTLS_SSL_CLI_C)
1005 handshake->ecjpake_cache = NULL;
1006 handshake->ecjpake_cache_len = 0;
1007#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001008#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001009
Gilles Peskineeccd8882020-03-10 12:19:08 +01001010#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001012#endif
1013
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001014#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1015 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1016#endif
Hanno Becker75173122019-02-06 16:18:31 +00001017
1018#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1019 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001021#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022}
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001025{
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001027
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001028#if defined(MBEDTLS_USE_PSA_CRYPTO)
1029 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1030 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001031#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1033 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001034#endif
1035
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001036#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001037#if defined(MBEDTLS_USE_PSA_CRYPTO)
1038 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1039 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001040#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_md_init(&transform->md_ctx_enc);
1042 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001043#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001044#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001045}
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001048{
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001050}
1051
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001052MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001053static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001054{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1056
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001057 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001058#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (ssl->transform_negotiate) {
1060 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1061 }
Jerry Yu2e199812022-12-01 18:57:19 +08001062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (ssl->session_negotiate) {
1064 mbedtls_ssl_session_free(ssl->session_negotiate);
1065 }
1066 if (ssl->handshake) {
1067 mbedtls_ssl_handshake_free(ssl);
1068 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001069
Jerry Yu2e199812022-12-01 18:57:19 +08001070#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001071 /*
1072 * Either the pointers are now NULL or cleared properly and can be freed.
1073 * Now allocate missing structures.
1074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (ssl->transform_negotiate == NULL) {
1076 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001077 }
Jerry Yu2e199812022-12-01 18:57:19 +08001078#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (ssl->session_negotiate == NULL) {
1081 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001082 }
Paul Bakker48916f92012-09-16 19:57:18 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (ssl->handshake == NULL) {
1085 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001086 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001087#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1088 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1091 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001092#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001093
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001094 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001096#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001097 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001098#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 ssl->session_negotiate == NULL) {
1100 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001103 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001104
1105#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001107 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001108#endif
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001111 ssl->session_negotiate = NULL;
1112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001114 }
1115
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001116#if defined(MBEDTLS_SSL_EARLY_DATA)
1117#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001118 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001119#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001120#if defined(MBEDTLS_SSL_SRV_C)
1121 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1122#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001123 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001124#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001125
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001126 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_ssl_session_init(ssl->session_negotiate);
1128 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001129
Jerry Yu2e199812022-12-01 18:57:19 +08001130#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001132#endif
1133
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001134 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001135 ret = mbedtls_ssl_reset_checksum(ssl);
1136 if (ret != 0) {
1137 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1138 return ret;
1139 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001140
Jerry Yud0766ec2022-09-22 10:46:57 +08001141#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1142 defined(MBEDTLS_SSL_SRV_C) && \
1143 defined(MBEDTLS_SSL_SESSION_TICKETS)
1144 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001146#endif
1147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001150 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001153 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001155 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001159 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001160#endif
1161
Brett Warrene0edc842021-08-17 09:53:13 +01001162/*
1163 * curve_list is translated to IANA TLS group identifiers here because
1164 * mbedtls_ssl_conf_curves returns void and so can't return
1165 * any error codes.
1166 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001167#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001168#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1169 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001171 size_t length;
1172 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1173
Thomas Daubney850a0792023-05-22 12:05:03 +01001174 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
Brett Warrene0edc842021-08-17 09:53:13 +01001176
1177 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1179 if (group_list == NULL) {
1180 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1181 }
Brett Warrene0edc842021-08-17 09:53:13 +01001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001184 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 curve_list[i]);
1186 if (tls_id == 0) {
1187 mbedtls_free(group_list);
1188 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001189 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001190 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001191 }
1192
1193 group_list[length] = 0;
1194
1195 ssl->handshake->group_list = group_list;
1196 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001198 ssl->handshake->group_list = ssl->conf->group_list;
1199 ssl->handshake->group_list_heap_allocated = 0;
1200 }
1201#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001202#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001203
Ronald Crone68ab4f2022-10-05 12:46:29 +02001204#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001205#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001206#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001207 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1208 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1210 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001211 const int *md;
1212 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001213 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001214 uint16_t *p;
1215
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001216 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1217 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1218 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1221 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001222 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001224#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001226#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001227
Jerry Yub476a442022-01-21 18:14:45 +08001228#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001230#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1232 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1233 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001234 }
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1237 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1238 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1241 sizeof(uint16_t));
1242 if (ssl->handshake->sig_algs == NULL) {
1243 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1244 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 p = (uint16_t *) ssl->handshake->sig_algs;
1247 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1248 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1249 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001250 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001252#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001254 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001255#endif
1256#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001258 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001259#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001260 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001261 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001262 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001265 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001266 ssl->handshake->sig_algs_heap_allocated = 0;
1267 }
Jerry Yucc539102022-06-27 16:27:35 +08001268#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001269#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001271}
1272
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001273#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001274/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001275MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001276static int ssl_cookie_write_dummy(void *ctx,
1277 unsigned char **p, unsigned char *end,
1278 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001279{
1280 ((void) ctx);
1281 ((void) p);
1282 ((void) end);
1283 ((void) cli_id);
1284 ((void) cli_id_len);
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001287}
1288
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001289MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001290static int ssl_cookie_check_dummy(void *ctx,
1291 const unsigned char *cookie, size_t cookie_len,
1292 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001293{
1294 ((void) ctx);
1295 ((void) cookie);
1296 ((void) cookie_len);
1297 ((void) cli_id);
1298 ((void) cli_id_len);
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001301}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001302#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304/*
1305 * Initialize an SSL context
1306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001308{
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001310}
1311
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001312MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001313static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001314{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001315 const mbedtls_ssl_config *conf = ssl->conf;
1316
Ronald Cron6f135e12021-12-08 16:57:54 +01001317#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1319 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1320 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1321 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001322 }
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1325 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001326 }
1327#endif
1328
1329#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1331 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1332 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001333 }
1334#endif
1335
Ronald Cron6f135e12021-12-08 16:57:54 +01001336#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1338 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1340 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001341 }
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1344 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001345 }
1346#endif
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1349 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001350}
1351
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001352MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001353static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1354{
1355 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 ret = ssl_conf_version_check(ssl);
1357 if (ret != 0) {
1358 return ret;
1359 }
Jerry Yu60835a82021-08-04 10:13:52 +08001360
Yanray Wangb422cab2023-12-01 16:18:10 +08001361 if (ssl->conf->f_rng == NULL) {
1362 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1363 return MBEDTLS_ERR_SSL_NO_RNG;
1364 }
1365
Jerry Yu60835a82021-08-04 10:13:52 +08001366 /* Space for further checks */
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001369}
1370
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001371/*
1372 * Setup an SSL context
1373 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1376 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001377{
Janos Follath865b3eb2019-12-16 11:46:15 +00001378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001379 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1380 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001382 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if ((ret = ssl_conf_check(ssl)) != 0) {
1385 return ret;
1386 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001387 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001388
Paul Bakker62f2dee2012-09-28 07:31:51 +00001389 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001390 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001391 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001392
1393 /* Set to NULL in case of an error condition */
1394 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001395
Darryl Greenb33cc762019-11-28 14:29:44 +00001396#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1397 ssl->in_buf_len = in_buf_len;
1398#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1400 if (ssl->in_buf == NULL) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001402 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001403 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001404 }
1405
Darryl Greenb33cc762019-11-28 14:29:44 +00001406#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1407 ssl->out_buf_len = out_buf_len;
1408#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1410 if (ssl->out_buf == NULL) {
1411 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001412 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001413 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 }
1415
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001416 mbedtls_ssl_reset_in_pointers(ssl);
1417 mbedtls_ssl_reset_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001418
Johan Pascalb62bb512015-12-03 21:56:45 +01001419#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001421#endif
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001424 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001428
1429error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_free(ssl->in_buf);
1431 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001432
1433 ssl->conf = NULL;
1434
Darryl Greenb33cc762019-11-28 14:29:44 +00001435#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1436 ssl->in_buf_len = 0;
1437 ssl->out_buf_len = 0;
1438#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001439 ssl->in_buf = NULL;
1440 ssl->out_buf = NULL;
1441
1442 ssl->in_hdr = NULL;
1443 ssl->in_ctr = NULL;
1444 ssl->in_len = NULL;
1445 ssl->in_iv = NULL;
1446 ssl->in_msg = NULL;
1447
1448 ssl->out_hdr = NULL;
1449 ssl->out_ctr = NULL;
1450 ssl->out_len = NULL;
1451 ssl->out_iv = NULL;
1452 ssl->out_msg = NULL;
1453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001455}
1456
1457/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001458 * Reset an initialized and used SSL context for re-use while retaining
1459 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001460 *
1461 * If partial is non-zero, keep data in the input buffer and client ID.
1462 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1465 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001466{
Darryl Greenb33cc762019-11-28 14:29:44 +00001467#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1468 size_t in_buf_len = ssl->in_buf_len;
1469 size_t out_buf_len = ssl->out_buf_len;
1470#else
1471 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1472 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1473#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001474
Hanno Beckerb0302c42021-08-03 09:39:42 +01001475#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1476 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001477#endif
1478
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001479 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001481
Deomid rojer Ryabkov96e22902025-01-26 10:43:42 +02001482 mbedtls_ssl_reset_in_pointers(ssl);
1483 mbedtls_ssl_reset_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001484
1485 /* Reset incoming message parsing */
1486 ssl->in_offt = NULL;
1487 ssl->nb_zero = 0;
1488 ssl->in_msgtype = 0;
1489 ssl->in_msglen = 0;
1490 ssl->in_hslen = 0;
Deomid rojer Ryabkovbbe87452025-02-13 13:41:51 +03001491 ssl->in_hsfraglen = 0;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001492 ssl->keep_current_message = 0;
1493 ssl->transform_in = NULL;
1494
1495#if defined(MBEDTLS_SSL_PROTO_DTLS)
1496 ssl->next_record_offset = 0;
1497 ssl->in_epoch = 0;
1498#endif
1499
1500 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001502 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504 }
1505
Ronald Cronad8c17b2022-06-10 17:18:09 +02001506 ssl->send_alert = 0;
1507
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508 /* Reset outgoing message writing */
1509 ssl->out_msgtype = 0;
1510 ssl->out_msglen = 0;
1511 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 memset(ssl->out_buf, 0, out_buf_len);
1513 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 ssl->transform_out = NULL;
1515
1516#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001518#endif
1519
XiaokangQian2b01dc32022-01-21 02:53:13 +00001520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if (ssl->transform) {
1522 mbedtls_ssl_transform_free(ssl->transform);
1523 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001524 ssl->transform = NULL;
1525 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1527
XiaokangQian2b01dc32022-01-21 02:53:13 +00001528#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 mbedtls_ssl_transform_free(ssl->transform_application);
1530 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531 ssl->transform_application = NULL;
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001534#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1536 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001538#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1541 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001542 ssl->handshake->transform_handshake = NULL;
1543 }
1544
XiaokangQian2b01dc32022-01-21 02:53:13 +00001545#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001546}
1547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001549{
1550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1551
1552 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001553 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001556
1557 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_SSL_RENEGOTIATION)
1559 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001560 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001561
1562 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1564 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001565#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001567
Hanno Beckerb0302c42021-08-03 09:39:42 +01001568 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001569 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (ssl->session) {
1571 mbedtls_ssl_session_free(ssl->session);
1572 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001573 ssl->session = NULL;
1574 }
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001577 ssl->alpn_chosen = NULL;
1578#endif
1579
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001580#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001581 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001582#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001584#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (free_cli_id) {
1586 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001587 ssl->cli_id = NULL;
1588 ssl->cli_id_len = 0;
1589 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001590#endif
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if ((ret = ssl_handshake_init(ssl)) != 0) {
1593 return ret;
1594 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001597}
1598
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001599/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001600 * Reset an initialized and used SSL context for re-use while retaining
1601 * all application-set variables, function pointers and data.
1602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001604{
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001606}
1607
1608/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 * SSL set accessors
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001613 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001614}
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001618 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001619}
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001624 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001625}
1626#endif
1627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001630 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001631}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1636 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001637{
1638 ssl->disable_datagram_packing = !allow_packing;
1639}
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1642 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001644 conf->hs_timeout_min = min;
1645 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001646}
1647#endif
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001651 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001652}
1653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001655void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1656 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1657 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001659 conf->f_vrfy = f_vrfy;
1660 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1665 int (*f_rng)(void *, unsigned char *, size_t),
1666 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001667{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001668 conf->f_rng = f_rng;
1669 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001670}
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1673 void (*f_dbg)(void *, int, const char *, int, const char *),
1674 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001675{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001676 conf->f_dbg = f_dbg;
1677 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678}
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1681 void *p_bio,
1682 mbedtls_ssl_send_t *f_send,
1683 mbedtls_ssl_recv_t *f_recv,
1684 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001685{
1686 ssl->p_bio = p_bio;
1687 ssl->f_send = f_send;
1688 ssl->f_recv = f_recv;
1689 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001690}
1691
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001692#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001693void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001694{
1695 ssl->mtu = mtu;
1696}
1697#endif
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001700{
1701 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001702}
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1705 void *p_timer,
1706 mbedtls_ssl_set_timer_t *f_set_timer,
1707 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001708{
1709 ssl->p_timer = p_timer;
1710 ssl->f_set_timer = f_set_timer;
1711 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001712
1713 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001715}
1716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001718void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1719 void *p_cache,
1720 mbedtls_ssl_cache_get_t *f_get_cache,
1721 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001722{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001723 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001724 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001725 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001730int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001731{
Janos Follath865b3eb2019-12-16 11:46:15 +00001732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001735 session == NULL ||
1736 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739 }
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (ssl->handshake->resume == 1) {
1742 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1743 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001744
Jerry Yu21092062022-10-10 21:21:31 +08001745#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron8d630842024-04-04 14:05:21 +02001747#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001748 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001752 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1754 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1755 session->ciphersuite));
1756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001757 }
Ronald Cron8d630842024-04-04 14:05:21 +02001758#else
1759 /*
1760 * If session tickets are not enabled, it is not possible to resume a
1761 * TLS 1.3 session, thus do not make any change to the SSL context in
1762 * the first place.
1763 */
1764 return 0;
1765#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001766 }
Jerry Yu21092062022-10-10 21:21:31 +08001767#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1770 session)) != 0) {
1771 return ret;
1772 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001773
Paul Bakker0a597072012-09-25 21:55:46 +00001774 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1781 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001782{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001783 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784}
1785
Ronald Cron6f135e12021-12-08 16:57:54 +01001786#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001787void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1788 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001789{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001790 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001791}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001792
1793#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001794void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1795 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001796{
1797 conf->early_data_enabled = early_data_enabled;
1798}
Jerry Yucc4e0072022-11-22 17:22:22 +08001799
1800#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001801void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001803{
Jerry Yu39da9852022-12-06 16:58:36 +08001804 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001805}
1806#endif /* MBEDTLS_SSL_SRV_C */
1807
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001808#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001809#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001812void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1813 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001814{
1815 conf->cert_profile = profile;
1816}
1817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001819{
1820 mbedtls_ssl_key_cert *cur = key_cert, *next;
1821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001823 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001825 cur = next;
1826 }
1827}
1828
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001829/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001830MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001831static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1832 mbedtls_x509_crt *cert,
1833 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001834{
niisato8ee24222018-06-25 19:05:48 +09001835 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001838 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001840 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1845 if (new_cert == NULL) {
1846 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1847 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001848
niisato8ee24222018-06-25 19:05:48 +09001849 new_cert->cert = cert;
1850 new_cert->key = key;
1851 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001852
Glenn Strauss36872db2022-01-22 05:06:31 -05001853 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001855 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001857 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001859 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 }
niisato8ee24222018-06-25 19:05:48 +09001861 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001862 }
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865}
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001868 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001870{
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001872}
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001875 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001877{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001878 conf->ca_chain = ca_chain;
1879 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001880
1881#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1882 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1883 * cannot be used together. */
1884 conf->f_ca_cb = NULL;
1885 conf->p_ca_cb = NULL;
1886#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001887}
Hanno Becker5adaad92019-03-27 16:54:37 +00001888
1889#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001890void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1891 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1892 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001893{
1894 conf->f_ca_cb = f_ca_cb;
1895 conf->p_ca_cb = p_ca_cb;
1896
1897 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1898 * cannot be used together. */
1899 conf->ca_chain = NULL;
1900 conf->ca_crl = NULL;
1901}
1902#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001904
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001905#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001906const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1907 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001908{
1909 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001911}
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1914 mbedtls_x509_crt *own_cert,
1915 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001916{
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1918 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001919}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1922 mbedtls_x509_crt *ca_chain,
1923 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001924{
1925 ssl->handshake->sni_ca_chain = ca_chain;
1926 ssl->handshake->sni_ca_crl = ca_crl;
1927}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001928
Glenn Strauss999ef702022-03-11 01:37:23 -05001929#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001930void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1931 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001932{
1933 ssl->handshake->dn_hints = crt;
1934}
1935#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1938 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001939{
1940 ssl->handshake->sni_authmode = authmode;
1941}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001942#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1943
Hanno Becker8927c832019-04-03 12:52:50 +01001944#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001945void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1946 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1947 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001948{
1949 ssl->f_vrfy = f_vrfy;
1950 ssl->p_vrfy = p_vrfy;
1951}
1952#endif
1953
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001954#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001955
Valerio Setti016f6822022-12-09 14:17:50 +01001956#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001957static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1958static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1959
Valerio Setti016f6822022-12-09 14:17:50 +01001960static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 mbedtls_ssl_context *ssl,
1962 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001963{
1964 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001965 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001966 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001967 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001968 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001969 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1971 psa_pake_cs_set_primitive(&cipher_suite,
1972 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1973 PSA_ECC_FAMILY_SECP_R1,
1974 256));
1975 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1978 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001979 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001983 user = jpake_server_id;
1984 user_len = sizeof(jpake_server_id);
1985 peer = jpake_client_id;
1986 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001988 user = jpake_client_id;
1989 user_len = sizeof(jpake_client_id);
1990 peer = jpake_server_id;
1991 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001993
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001994 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1995 if (status != PSA_SUCCESS) {
1996 return status;
1997 }
1998
1999 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002001 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 }
Valerio Setti016f6822022-12-09 14:17:50 +01002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2005 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002006 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 }
Valerio Setti016f6822022-12-09 14:17:50 +01002008
2009 ssl->handshake->psa_pake_ctx_is_ok = 1;
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002012}
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2015 const unsigned char *pw,
2016 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002017{
2018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2019 psa_status_t status;
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (ssl->handshake == NULL || ssl->conf == NULL) {
2022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002023 }
2024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 /* Empty password is not valid */
2026 if ((pw == NULL) || (pw_len == 0)) {
2027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2028 }
2029
2030 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2031 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2032 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2033
2034 status = psa_import_key(&attributes, pw, pw_len,
2035 &ssl->handshake->psa_pake_password);
2036 if (status != PSA_SUCCESS) {
2037 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2038 }
2039
2040 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2041 ssl->handshake->psa_pake_password);
2042 if (status != PSA_SUCCESS) {
2043 psa_destroy_key(ssl->handshake->psa_pake_password);
2044 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2045 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2046 }
2047
2048 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002049}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2052 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002053{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002054 psa_status_t status;
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (ssl->handshake == NULL || ssl->conf == NULL) {
2057 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002058 }
2059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (mbedtls_svc_key_id_is_null(pwd)) {
2061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2062 }
2063
2064 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2065 if (status != PSA_SUCCESS) {
2066 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2067 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2068 }
2069
2070 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002071}
2072#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2074 const unsigned char *pw,
2075 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002076{
2077 mbedtls_ecjpake_role role;
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (ssl->handshake == NULL || ssl->conf == NULL) {
2080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2081 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002082
Valerio Settic689ed82022-12-07 14:40:38 +01002083 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if ((pw == NULL) || (pw_len == 0)) {
2085 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2086 }
Valerio Settic689ed82022-12-07 14:40:38 +01002087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002089 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002091 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2095 role,
2096 MBEDTLS_MD_SHA256,
2097 MBEDTLS_ECP_DP_SECP256R1,
2098 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002099}
Valerio Setti02c25b52022-11-15 14:08:42 +01002100#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002101#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002102
Ronald Cron73fe8df2022-10-05 14:31:43 +02002103#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002104int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002105{
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (conf->psk_identity == NULL ||
2107 conf->psk_identity_len == 0) {
2108 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002109 }
2110
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002111#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2113 return 1;
2114 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002115#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (conf->psk != NULL && conf->psk_len != 0) {
2118 return 1;
2119 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002122}
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002125{
2126 /* Remove reference to existing PSK, if any. */
2127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002129 /* The maintenance of the PSK key slot is the
2130 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002131 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002133#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002135 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002136 conf->psk = NULL;
2137 conf->psk_len = 0;
2138 }
2139
2140 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (conf->psk_identity != NULL) {
2142 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002143 conf->psk_identity = NULL;
2144 conf->psk_identity_len = 0;
2145 }
2146}
2147
Hanno Becker7390c712018-11-15 13:33:04 +00002148/* This function assumes that PSK identity in the SSL config is unset.
2149 * It checks that the provided identity is well-formed and attempts
2150 * to make a copy of it in the SSL config.
2151 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002152MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002153static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2154 unsigned char const *psk_identity,
2155 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002156{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002157 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002159 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 (psk_identity_len >> 16) != 0 ||
2161 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2162 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002163 }
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2166 if (conf->psk_identity == NULL) {
2167 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2168 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002169
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002170 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2177 const unsigned char *psk, size_t psk_len,
2178 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002179{
Janos Follath865b3eb2019-12-16 11:46:15 +00002180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002181
2182 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2184 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2185 }
Hanno Becker7390c712018-11-15 13:33:04 +00002186
2187 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (psk == NULL) {
2189 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2190 }
2191 if (psk_len == 0) {
2192 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2193 }
2194 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2196 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2199 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2200 }
Hanno Becker7390c712018-11-15 13:33:04 +00002201 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002203
2204 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2206 if (ret != 0) {
2207 ssl_conf_remove_psk(conf);
2208 }
Hanno Becker7390c712018-11-15 13:33:04 +00002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002211}
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002214{
2215#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002217 /* The maintenance of the external PSK key slot is the
2218 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (ssl->handshake->psk_opaque_is_internal) {
2220 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002221 ssl->handshake->psk_opaque_is_internal = 0;
2222 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002223 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002224 }
Neil Armstronge952a302022-05-03 10:22:14 +02002225#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002227 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002229 ssl->handshake->psk_len = 0;
lhuang046d4d94f2024-06-11 12:37:02 -07002230 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002231 }
Neil Armstronge952a302022-05-03 10:22:14 +02002232#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002233}
2234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2236 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002237{
Neil Armstrong501c9322022-05-03 09:35:09 +02002238#if defined(MBEDTLS_USE_PSA_CRYPTO)
2239 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002241 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002243#endif /* MBEDTLS_USE_PSA_CRYPTO */
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 if (psk == NULL || ssl->handshake == NULL) {
2246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2247 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2250 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2251 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002254
Neil Armstrong501c9322022-05-03 09:35:09 +02002255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002256#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2258 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2259 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2260 } else {
2261 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2262 }
2263 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002264 }
2265#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002266
Jerry Yu568ec252022-07-22 21:27:34 +08002267#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2269 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2270 psa_set_key_usage_flags(&key_attributes,
2271 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002272 }
2273#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 psa_set_key_algorithm(&key_attributes, alg);
2276 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2279 if (status != PSA_SUCCESS) {
2280 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2281 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002282
2283 /* Allow calling psa_destroy_key() on psk remove */
2284 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002286#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2288 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2289 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002290
2291 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002295#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002296}
2297
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002299int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2300 mbedtls_svc_key_id_t psk,
2301 const unsigned char *psk_identity,
2302 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002303{
Janos Follath865b3eb2019-12-16 11:46:15 +00002304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002305
2306 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2308 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2309 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002310
Hanno Becker7390c712018-11-15 13:33:04 +00002311 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (mbedtls_svc_key_id_is_null(psk)) {
2313 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2314 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002315 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002316
2317 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2319 psk_identity_len);
2320 if (ret != 0) {
2321 ssl_conf_remove_psk(conf);
2322 }
Hanno Becker7390c712018-11-15 13:33:04 +00002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002325}
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2328 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002329{
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if ((mbedtls_svc_key_id_is_null(psk)) ||
2331 (ssl->handshake == NULL)) {
2332 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2333 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002336 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002338}
2339#endif /* MBEDTLS_USE_PSA_CRYPTO */
2340
Jerry Yu8897c072022-08-12 13:56:53 +08002341#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002342void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2343 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2344 size_t),
2345 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002346{
2347 conf->f_psk = f_psk;
2348 conf->p_psk = p_psk;
2349}
Jerry Yu8897c072022-08-12 13:56:53 +08002350#endif /* MBEDTLS_SSL_SRV_C */
2351
Ronald Cron73fe8df2022-10-05 14:31:43 +02002352#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002353
2354#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002355static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002357{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002358#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (alg == PSA_ALG_CBC_NO_PADDING) {
2360 return MBEDTLS_SSL_MODE_CBC;
2361 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002362#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (PSA_ALG_IS_AEAD(alg)) {
2364 return MBEDTLS_SSL_MODE_AEAD;
2365 }
2366 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002367}
2368
2369#else /* MBEDTLS_USE_PSA_CRYPTO */
2370
2371static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002373{
2374#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if (mode == MBEDTLS_MODE_CBC) {
2376 return MBEDTLS_SSL_MODE_CBC;
2377 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002378#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2379
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002380#if defined(MBEDTLS_GCM_C) || \
2381 defined(MBEDTLS_CCM_C) || \
2382 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002384 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 mode == MBEDTLS_MODE_CHACHAPOLY) {
2386 return MBEDTLS_SSL_MODE_AEAD;
2387 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002388#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002391}
Gilles Peskine301711e2022-04-26 16:57:05 +02002392#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002393
Gilles Peskinee108d982022-04-26 16:50:40 +02002394static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2395 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002397{
2398#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2400 base_mode == MBEDTLS_SSL_MODE_CBC) {
2401 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002402 }
2403#else
2404 (void) encrypt_then_mac;
2405#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002407}
2408
Neil Armstrongab555e02022-04-04 11:07:59 +02002409mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002411{
Gilles Peskinee108d982022-04-26 16:50:40 +02002412 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002413#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002415#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002417#endif
2418 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002419
Gilles Peskinee108d982022-04-26 16:50:40 +02002420 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002421#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002422 encrypt_then_mac = transform->encrypt_then_mac;
2423#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002425}
2426
Neil Armstrongab555e02022-04-04 11:07:59 +02002427mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002428#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002430#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002432{
Gilles Peskinee108d982022-04-26 16:50:40 +02002433 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2434
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002435#if defined(MBEDTLS_USE_PSA_CRYPTO)
2436 psa_status_t status;
2437 psa_algorithm_t alg;
2438 psa_key_type_t type;
2439 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002440 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2441 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 if (status == PSA_SUCCESS) {
2443 base_mode = mbedtls_ssl_get_base_mode(alg);
2444 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002445#else
2446 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002447 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002449 base_mode =
2450 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002452 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002453#endif /* MBEDTLS_USE_PSA_CRYPTO */
2454
Gilles Peskinee108d982022-04-26 16:50:40 +02002455#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2456 int encrypt_then_mac = 0;
2457#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002459}
2460
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002461#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2464 size_t taglen,
2465 psa_algorithm_t *alg,
2466 psa_key_type_t *key_type,
2467 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002468{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002469#if !defined(MBEDTLS_SSL_HAVE_CCM)
2470 (void) taglen;
2471#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002473#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002474 case MBEDTLS_CIPHER_AES_128_CBC:
2475 *alg = PSA_ALG_CBC_NO_PADDING;
2476 *key_type = PSA_KEY_TYPE_AES;
2477 *key_size = 128;
2478 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002479#endif
2480#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002481 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002483 *key_type = PSA_KEY_TYPE_AES;
2484 *key_size = 128;
2485 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002486#endif
2487#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002488 case MBEDTLS_CIPHER_AES_128_GCM:
2489 *alg = PSA_ALG_GCM;
2490 *key_type = PSA_KEY_TYPE_AES;
2491 *key_size = 128;
2492 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002493#endif
2494#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002495 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002497 *key_type = PSA_KEY_TYPE_AES;
2498 *key_size = 192;
2499 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002500#endif
2501#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002502 case MBEDTLS_CIPHER_AES_192_GCM:
2503 *alg = PSA_ALG_GCM;
2504 *key_type = PSA_KEY_TYPE_AES;
2505 *key_size = 192;
2506 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002507#endif
2508#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002509 case MBEDTLS_CIPHER_AES_256_CBC:
2510 *alg = PSA_ALG_CBC_NO_PADDING;
2511 *key_type = PSA_KEY_TYPE_AES;
2512 *key_size = 256;
2513 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002514#endif
2515#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002516 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002518 *key_type = PSA_KEY_TYPE_AES;
2519 *key_size = 256;
2520 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002521#endif
2522#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002523 case MBEDTLS_CIPHER_AES_256_GCM:
2524 *alg = PSA_ALG_GCM;
2525 *key_type = PSA_KEY_TYPE_AES;
2526 *key_size = 256;
2527 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002528#endif
2529#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002530 case MBEDTLS_CIPHER_ARIA_128_CBC:
2531 *alg = PSA_ALG_CBC_NO_PADDING;
2532 *key_type = PSA_KEY_TYPE_ARIA;
2533 *key_size = 128;
2534 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002535#endif
2536#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002537 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002539 *key_type = PSA_KEY_TYPE_ARIA;
2540 *key_size = 128;
2541 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002542#endif
2543#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002544 case MBEDTLS_CIPHER_ARIA_128_GCM:
2545 *alg = PSA_ALG_GCM;
2546 *key_type = PSA_KEY_TYPE_ARIA;
2547 *key_size = 128;
2548 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002549#endif
2550#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002551 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002553 *key_type = PSA_KEY_TYPE_ARIA;
2554 *key_size = 192;
2555 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002556#endif
2557#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002558 case MBEDTLS_CIPHER_ARIA_192_GCM:
2559 *alg = PSA_ALG_GCM;
2560 *key_type = PSA_KEY_TYPE_ARIA;
2561 *key_size = 192;
2562 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002563#endif
2564#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002565 case MBEDTLS_CIPHER_ARIA_256_CBC:
2566 *alg = PSA_ALG_CBC_NO_PADDING;
2567 *key_type = PSA_KEY_TYPE_ARIA;
2568 *key_size = 256;
2569 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002570#endif
2571#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002572 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002574 *key_type = PSA_KEY_TYPE_ARIA;
2575 *key_size = 256;
2576 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002577#endif
2578#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002579 case MBEDTLS_CIPHER_ARIA_256_GCM:
2580 *alg = PSA_ALG_GCM;
2581 *key_type = PSA_KEY_TYPE_ARIA;
2582 *key_size = 256;
2583 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002584#endif
2585#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002586 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2587 *alg = PSA_ALG_CBC_NO_PADDING;
2588 *key_type = PSA_KEY_TYPE_CAMELLIA;
2589 *key_size = 128;
2590 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002591#endif
2592#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002593 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002595 *key_type = PSA_KEY_TYPE_CAMELLIA;
2596 *key_size = 128;
2597 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002598#endif
2599#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002600 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2601 *alg = PSA_ALG_GCM;
2602 *key_type = PSA_KEY_TYPE_CAMELLIA;
2603 *key_size = 128;
2604 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002605#endif
2606#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002607 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002609 *key_type = PSA_KEY_TYPE_CAMELLIA;
2610 *key_size = 192;
2611 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002612#endif
2613#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002614 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2615 *alg = PSA_ALG_GCM;
2616 *key_type = PSA_KEY_TYPE_CAMELLIA;
2617 *key_size = 192;
2618 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002619#endif
2620#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002621 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2622 *alg = PSA_ALG_CBC_NO_PADDING;
2623 *key_type = PSA_KEY_TYPE_CAMELLIA;
2624 *key_size = 256;
2625 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002626#endif
2627#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002628 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002630 *key_type = PSA_KEY_TYPE_CAMELLIA;
2631 *key_size = 256;
2632 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002633#endif
2634#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002635 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2636 *alg = PSA_ALG_GCM;
2637 *key_type = PSA_KEY_TYPE_CAMELLIA;
2638 *key_size = 256;
2639 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002640#endif
2641#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002642 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2643 *alg = PSA_ALG_CHACHA20_POLY1305;
2644 *key_type = PSA_KEY_TYPE_CHACHA20;
2645 *key_size = 256;
2646 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002647#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002648 case MBEDTLS_CIPHER_NULL:
2649 *alg = MBEDTLS_SSL_NULL_CIPHER;
2650 *key_type = 0;
2651 *key_size = 0;
2652 break;
2653 default:
2654 return PSA_ERROR_NOT_SUPPORTED;
2655 }
2656
2657 return PSA_SUCCESS;
2658}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002659#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002660
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002661#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002662int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2663 const unsigned char *dhm_P, size_t P_len,
2664 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002665{
Janos Follath865b3eb2019-12-16 11:46:15 +00002666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 mbedtls_mpi_free(&conf->dhm_P);
2669 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2672 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2673 mbedtls_mpi_free(&conf->dhm_P);
2674 mbedtls_mpi_free(&conf->dhm_G);
2675 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002679}
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002682{
Janos Follath865b3eb2019-12-16 11:46:15 +00002683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 mbedtls_mpi_free(&conf->dhm_P);
2686 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2689 &conf->dhm_P)) != 0 ||
2690 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2691 &conf->dhm_G)) != 0) {
2692 mbedtls_mpi_free(&conf->dhm_P);
2693 mbedtls_mpi_free(&conf->dhm_G);
2694 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002695 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002696
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002698}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002699#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002700
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002701#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2702/*
2703 * Set the minimum length for Diffie-Hellman parameters
2704 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2706 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002707{
2708 conf->dhm_min_bitlen = bitlen;
2709}
2710#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2711
Ronald Crone68ab4f2022-10-05 12:46:29 +02002712#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002713#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002714/*
2715 * Set allowed/preferred hashes for handshake signatures
2716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002717void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2718 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002719{
2720 conf->sig_hashes = hashes;
2721}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002722#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002723
Jerry Yuf017ee42022-01-12 15:49:48 +08002724/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002725void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2726 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002727{
Jerry Yuf017ee42022-01-12 15:49:48 +08002728#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2729 conf->sig_hashes = NULL;
2730#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2731 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002732}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002733#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002734
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002735#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002736#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002737/*
2738 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002739 *
2740 * mbedtls_ssl_setup() takes the provided list
2741 * and translates it to a list of IANA TLS group identifiers,
2742 * stored in ssl->handshake->group_list.
2743 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002745void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2746 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002748 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002749 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002750}
Brett Warrene0edc842021-08-17 09:53:13 +01002751#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002752#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002753
Brett Warrene0edc842021-08-17 09:53:13 +01002754/*
2755 * Set the allowed groups
2756 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002757void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2758 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002759{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002760#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002761 conf->curve_list = NULL;
2762#endif
2763 conf->group_list = group_list;
2764}
2765
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002766#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002767int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002768{
Hanno Becker947194e2017-04-07 13:25:49 +01002769 /* Initialize to suppress unnecessary compiler warning */
2770 size_t hostname_len = 0;
2771
2772 /* Check if new hostname is valid before
2773 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (hostname != NULL) {
2775 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2778 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2779 }
Hanno Becker947194e2017-04-07 13:25:49 +01002780 }
2781
2782 /* Now it's clear that we will overwrite the old hostname,
2783 * so we can free it safely */
2784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002786 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002787 }
2788
2789 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002792 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 } else {
2794 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2795 if (ssl->hostname == NULL) {
2796 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2797 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002800
Hanno Becker947194e2017-04-07 13:25:49 +01002801 ssl->hostname[hostname_len] = '\0';
2802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002805}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002806#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002808#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002809void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2810 int (*f_sni)(void *, mbedtls_ssl_context *,
2811 const unsigned char *, size_t),
2812 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002813{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002814 conf->f_sni = f_sni;
2815 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002816}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002820int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002821{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002822 size_t cur_len, tot_len;
2823 const char **p;
2824
2825 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002826 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2827 * MUST NOT be truncated."
2828 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002829 */
2830 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 for (p = protos; *p != NULL; p++) {
2832 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002833 tot_len += cur_len;
2834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if ((cur_len == 0) ||
2836 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2837 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2838 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2839 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002840 }
2841
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002842 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002845}
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002848{
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002852
Johan Pascalb62bb512015-12-03 21:56:45 +01002853#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002854void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2855 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002856{
2857 conf->dtls_srtp_mki_support = support_mki_value;
2858}
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2861 unsigned char *mki_value,
2862 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002863{
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2865 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002866 }
Ron Eldor591f1622018-01-22 12:30:04 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2869 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002870 }
Ron Eldor591f1622018-01-22 12:30:04 +02002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002873 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002875}
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2878 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002879{
Johan Pascal253d0262020-09-22 13:04:45 +02002880 const mbedtls_ssl_srtp_profile *p;
2881 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002882
Johan Pascal253d0262020-09-22 13:04:45 +02002883 /* check the profiles list: all entry must be valid,
2884 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2886 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2887 p++) {
2888 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002889 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002891 /* unsupported value, stop parsing and set the size to an error value */
2892 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002893 }
2894 }
2895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2897 conf->dtls_srtp_profile_list = NULL;
2898 conf->dtls_srtp_profile_list_len = 0;
2899 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002900 }
2901
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002902 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002903 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002906}
2907
Gilles Peskine449bd832023-01-11 14:50:10 +01002908void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2909 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002910{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002911 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2912 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002914 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002916 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2918 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002919 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002920}
Johan Pascalb62bb512015-12-03 21:56:45 +01002921#endif /* MBEDTLS_SSL_DTLS_SRTP */
2922
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302923#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002924void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002925{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002926 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002927}
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002930{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002931 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002932}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302933#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002934
Janos Follath088ce432017-04-10 12:42:31 +01002935#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002936void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2937 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002938{
2939 conf->cert_req_ca_list = cert_req_ca_list;
2940}
2941#endif
2942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002944void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002945{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002946 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002947}
2948#endif
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002951void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002952{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002953 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002954}
2955#endif
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002958int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002959{
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2961 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2962 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002963 }
2964
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002965 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002972{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002973 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002974}
2975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002977void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002979 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002980}
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002984 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002985}
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2988 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002989{
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002995#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002998{
Ronald Crond67f8012024-08-28 07:45:57 +02002999 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3000 conf->session_tickets |= (use_tickets != 0) <<
3001 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003002}
Ronald Crond67f8012024-08-28 07:45:57 +02003003
Ronald Cronbedddd72024-08-27 14:18:50 +02003004#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003005void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3006 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003007{
Ronald Crond67f8012024-08-28 07:45:57 +02003008 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003009 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003010 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3011}
Ronald Cronbedddd72024-08-27 14:18:50 +02003012#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3013#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003014
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003015#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003016
Jerry Yud0766ec2022-09-22 10:46:57 +08003017#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003018void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3019 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003020{
Jerry Yud0766ec2022-09-22 10:46:57 +08003021 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003022}
3023#endif
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3026 mbedtls_ssl_ticket_write_t *f_ticket_write,
3027 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3028 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003029{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003030 conf->f_ticket_write = f_ticket_write;
3031 conf->f_ticket_parse = f_ticket_parse;
3032 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003033}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3038 mbedtls_ssl_export_keys_t *f_export_keys,
3039 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003040{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003041 ssl->f_export_keys = f_export_keys;
3042 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003043}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003044
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003045#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003046void mbedtls_ssl_conf_async_private_cb(
3047 mbedtls_ssl_config *conf,
3048 mbedtls_ssl_async_sign_t *f_async_sign,
3049 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3050 mbedtls_ssl_async_resume_t *f_async_resume,
3051 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003053{
3054 conf->f_async_sign_start = f_async_sign;
3055 conf->f_async_decrypt_start = f_async_decrypt;
3056 conf->f_async_resume = f_async_resume;
3057 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003058 conf->p_async_config_data = async_config_data;
3059}
3060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003062{
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003064}
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003067{
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 if (ssl->handshake == NULL) {
3069 return NULL;
3070 } else {
3071 return ssl->handshake->user_async_ctx;
3072 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003073}
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3076 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003077{
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003079 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003081}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003082#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003083
Paul Bakker5121ce52009-01-03 21:22:43 +00003084/*
3085 * SSL get accessors
3086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003087uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003088{
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 if (ssl->session != NULL) {
3090 return ssl->session->verify_result;
3091 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 if (ssl->session_negotiate != NULL) {
3094 return ssl->session_negotiate->verify_result;
3095 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003098}
3099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003101{
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (ssl == NULL || ssl->session == NULL) {
3103 return 0;
3104 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003107}
3108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003110{
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (ssl == NULL || ssl->session == NULL) {
3112 return NULL;
3113 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003116}
3117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3122 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003123 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003125 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003127 }
3128 }
3129#endif
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003132 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003134 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003136 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003138 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003139}
3140
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003141#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3142
3143size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3144{
3145 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3146 size_t record_size_limit = max_len;
3147
3148 if (ssl->session != NULL &&
3149 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3150 ssl->session->record_size_limit < max_len) {
3151 record_size_limit = ssl->session->record_size_limit;
3152 }
3153
3154 // TODO: this is currently untested
3155 /* During a handshake, use the value being negotiated */
3156 if (ssl->session_negotiate != NULL &&
3157 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3158 ssl->session_negotiate->record_size_limit < max_len) {
3159 record_size_limit = ssl->session_negotiate->record_size_limit;
3160 }
3161
3162 return record_size_limit;
3163}
3164#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3165
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003166#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003167size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003168{
David Horstmann95d516f2021-05-04 18:36:56 +01003169 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003170 size_t read_mfl;
3171
Jerry Yuddda0502022-12-01 19:43:12 +08003172#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003173 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3175 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3176 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003177 }
Jerry Yuddda0502022-12-01 19:43:12 +08003178#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003179
3180 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 if (ssl->session_out != NULL) {
3182 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3183 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003184 max_len = read_mfl;
3185 }
3186 }
3187
Jerry Yuddda0502022-12-01 19:43:12 +08003188 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 if (ssl->session_negotiate != NULL) {
3190 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3191 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003192 max_len = read_mfl;
3193 }
3194 }
3195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003197}
3198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003200{
3201 size_t max_len;
3202
3203 /*
3204 * Assume mfl_code is correct since it was checked when set
3205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003207
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003208 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 if (ssl->session_out != NULL &&
3210 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3211 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003212 }
3213
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003214 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 if (ssl->session_negotiate != NULL &&
3216 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3217 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003218 }
3219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003221}
3222#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3223
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003224#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003225size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003226{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003227 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3229 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3230 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3231 return 0;
3232 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3235 return ssl->mtu;
3236 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 if (ssl->mtu == 0) {
3239 return ssl->handshake->mtu;
3240 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 return ssl->mtu < ssl->handshake->mtu ?
3243 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003244}
3245#endif /* MBEDTLS_SSL_PROTO_DTLS */
3246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003248{
3249 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3250
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003251#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003252 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003253 !defined(MBEDTLS_SSL_PROTO_DTLS)
3254 (void) ssl;
3255#endif
3256
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003257#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003261 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003263#endif
3264
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003265#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3266 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3267
3268 if (max_len > record_size_limit) {
3269 max_len = record_size_limit;
3270 }
3271#endif
3272
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003273 if (ssl->transform_out != NULL &&
3274 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003275 /*
3276 * In TLS 1.3 case, when records are protected, `max_len` as computed
3277 * above is the maximum length of the TLSInnerPlaintext structure that
3278 * along the plaintext payload contains the inner content type (one byte)
3279 * and some zero padding. Given the algorithm used for padding
3280 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3281 * the plaintext payload. Round down to a multiple of
3282 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3283 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003284 */
3285 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3286 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3287 }
3288
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3291 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3292 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003293 const size_t overhead = (size_t) ret;
3294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (ret < 0) {
3296 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003297 }
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (mtu <= overhead) {
3300 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3301 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3302 }
3303
3304 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003305 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003307 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003308#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003309
Hanno Becker0defedb2018-08-10 12:35:02 +01003310#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003311 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3312 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003313 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314#endif
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003317}
3318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003320{
3321 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3322
3323#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3324 (void) ssl;
3325#endif
3326
3327#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003331 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003333#endif
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003336}
3337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003339const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003340{
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if (ssl == NULL || ssl->session == NULL) {
3342 return NULL;
3343 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003344
Hanno Beckere6824572019-02-07 13:18:46 +00003345#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003347#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003349#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003354int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3355 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003356{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003357 int ret;
3358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003360 dst == NULL ||
3361 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3363 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003364 }
3365
Hanno Beckere810bbc2021-05-14 16:01:05 +01003366 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3367 * idempotent: Each session can only be exported once.
3368 *
3369 * (This is in preparation for TLS 1.3 support where we will
3370 * need the ability to export multiple sessions (aka tickets),
3371 * which will be achieved by calling mbedtls_ssl_get_session()
3372 * multiple times until it fails.)
3373 *
3374 * Check whether we have already exported the current session,
3375 * and fail if so.
3376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (ssl->session->exported == 1) {
3378 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3379 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3382 if (ret != 0) {
3383 return ret;
3384 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003385
3386 /* Remember that we've exported the session. */
3387 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003391
David Horstmanncb01b362024-02-29 17:31:46 +00003392#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3393
3394/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003395 *
David Horstmanncb01b362024-02-29 17:31:46 +00003396 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003397 */
3398static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3399 unsigned char *buf,
3400 size_t buf_len)
3401{
3402 unsigned char *p = buf;
3403 size_t used = 0;
3404
3405#if defined(MBEDTLS_HAVE_TIME)
3406 uint64_t start;
3407#endif
3408#if defined(MBEDTLS_X509_CRT_PARSE_C)
3409#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3410 size_t cert_len;
3411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3412#endif /* MBEDTLS_X509_CRT_PARSE_C */
3413
3414 /*
3415 * Time
3416 */
3417#if defined(MBEDTLS_HAVE_TIME)
3418 used += 8;
3419
3420 if (used <= buf_len) {
3421 start = (uint64_t) session->start;
3422
3423 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3424 p += 8;
3425 }
3426#endif /* MBEDTLS_HAVE_TIME */
3427
3428 /*
3429 * Basic mandatory fields
3430 */
3431 used += 1 /* id_len */
3432 + sizeof(session->id)
3433 + sizeof(session->master)
3434 + 4; /* verify_result */
3435
3436 if (used <= buf_len) {
3437 *p++ = MBEDTLS_BYTE_0(session->id_len);
3438 memcpy(p, session->id, 32);
3439 p += 32;
3440
3441 memcpy(p, session->master, 48);
3442 p += 48;
3443
3444 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3445 p += 4;
3446 }
3447
3448 /*
3449 * Peer's end-entity certificate
3450 */
3451#if defined(MBEDTLS_X509_CRT_PARSE_C)
3452#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3453 if (session->peer_cert == NULL) {
3454 cert_len = 0;
3455 } else {
3456 cert_len = session->peer_cert->raw.len;
3457 }
3458
3459 used += 3 + cert_len;
3460
3461 if (used <= buf_len) {
3462 *p++ = MBEDTLS_BYTE_2(cert_len);
3463 *p++ = MBEDTLS_BYTE_1(cert_len);
3464 *p++ = MBEDTLS_BYTE_0(cert_len);
3465
3466 if (session->peer_cert != NULL) {
3467 memcpy(p, session->peer_cert->raw.p, cert_len);
3468 p += cert_len;
3469 }
3470 }
3471#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3472 if (session->peer_cert_digest != NULL) {
3473 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3474 if (used <= buf_len) {
3475 *p++ = (unsigned char) session->peer_cert_digest_type;
3476 *p++ = (unsigned char) session->peer_cert_digest_len;
3477 memcpy(p, session->peer_cert_digest,
3478 session->peer_cert_digest_len);
3479 p += session->peer_cert_digest_len;
3480 }
3481 } else {
3482 used += 2;
3483 if (used <= buf_len) {
3484 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3485 *p++ = 0;
3486 }
3487 }
3488#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3489#endif /* MBEDTLS_X509_CRT_PARSE_C */
3490
3491 /*
3492 * Session ticket if any, plus associated data
3493 */
3494#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3495#if defined(MBEDTLS_SSL_CLI_C)
3496 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3497 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3498
3499 if (used <= buf_len) {
3500 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3501 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3502 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3503
3504 if (session->ticket != NULL) {
3505 memcpy(p, session->ticket, session->ticket_len);
3506 p += session->ticket_len;
3507 }
3508
3509 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3510 p += 4;
3511 }
3512 }
3513#endif /* MBEDTLS_SSL_CLI_C */
3514#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3515 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3516 used += 8;
3517
3518 if (used <= buf_len) {
3519 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3520 p += 8;
3521 }
3522 }
3523#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3524#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3525
3526 /*
3527 * Misc extension-related info
3528 */
3529#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3530 used += 1;
3531
3532 if (used <= buf_len) {
3533 *p++ = session->mfl_code;
3534 }
3535#endif
3536
3537#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3538 used += 1;
3539
3540 if (used <= buf_len) {
3541 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3542 }
3543#endif
3544
3545 return used;
3546}
3547
3548MBEDTLS_CHECK_RETURN_CRITICAL
3549static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3550 const unsigned char *buf,
3551 size_t len)
3552{
3553#if defined(MBEDTLS_HAVE_TIME)
3554 uint64_t start;
3555#endif
3556#if defined(MBEDTLS_X509_CRT_PARSE_C)
3557#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3558 size_t cert_len;
3559#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3560#endif /* MBEDTLS_X509_CRT_PARSE_C */
3561
3562 const unsigned char *p = buf;
3563 const unsigned char * const end = buf + len;
3564
3565 /*
3566 * Time
3567 */
3568#if defined(MBEDTLS_HAVE_TIME)
3569 if (8 > (size_t) (end - p)) {
3570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3571 }
3572
3573 start = MBEDTLS_GET_UINT64_BE(p, 0);
3574 p += 8;
3575
3576 session->start = (time_t) start;
3577#endif /* MBEDTLS_HAVE_TIME */
3578
3579 /*
3580 * Basic mandatory fields
3581 */
3582 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3583 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3584 }
3585
3586 session->id_len = *p++;
3587 memcpy(session->id, p, 32);
3588 p += 32;
3589
3590 memcpy(session->master, p, 48);
3591 p += 48;
3592
3593 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3594 p += 4;
3595
3596 /* Immediately clear invalid pointer values that have been read, in case
3597 * we exit early before we replaced them with valid ones. */
3598#if defined(MBEDTLS_X509_CRT_PARSE_C)
3599#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3600 session->peer_cert = NULL;
3601#else
3602 session->peer_cert_digest = NULL;
3603#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3604#endif /* MBEDTLS_X509_CRT_PARSE_C */
3605#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3606 session->ticket = NULL;
3607#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3608
3609 /*
3610 * Peer certificate
3611 */
3612#if defined(MBEDTLS_X509_CRT_PARSE_C)
3613#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3614 /* Deserialize CRT from the end of the ticket. */
3615 if (3 > (size_t) (end - p)) {
3616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3617 }
3618
3619 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3620 p += 3;
3621
3622 if (cert_len != 0) {
3623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3624
3625 if (cert_len > (size_t) (end - p)) {
3626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3627 }
3628
3629 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3630
3631 if (session->peer_cert == NULL) {
3632 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3633 }
3634
3635 mbedtls_x509_crt_init(session->peer_cert);
3636
3637 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3638 p, cert_len)) != 0) {
3639 mbedtls_x509_crt_free(session->peer_cert);
3640 mbedtls_free(session->peer_cert);
3641 session->peer_cert = NULL;
3642 return ret;
3643 }
3644
3645 p += cert_len;
3646 }
3647#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3648 /* Deserialize CRT digest from the end of the ticket. */
3649 if (2 > (size_t) (end - p)) {
3650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3651 }
3652
3653 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3654 session->peer_cert_digest_len = (size_t) *p++;
3655
3656 if (session->peer_cert_digest_len != 0) {
3657 const mbedtls_md_info_t *md_info =
3658 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3659 if (md_info == NULL) {
3660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3661 }
3662 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3664 }
3665
3666 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3668 }
3669
3670 session->peer_cert_digest =
3671 mbedtls_calloc(1, session->peer_cert_digest_len);
3672 if (session->peer_cert_digest == NULL) {
3673 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3674 }
3675
3676 memcpy(session->peer_cert_digest, p,
3677 session->peer_cert_digest_len);
3678 p += session->peer_cert_digest_len;
3679 }
3680#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3681#endif /* MBEDTLS_X509_CRT_PARSE_C */
3682
3683 /*
3684 * Session ticket and associated data
3685 */
3686#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3687#if defined(MBEDTLS_SSL_CLI_C)
3688 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3689 if (3 > (size_t) (end - p)) {
3690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3691 }
3692
3693 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3694 p += 3;
3695
3696 if (session->ticket_len != 0) {
3697 if (session->ticket_len > (size_t) (end - p)) {
3698 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3699 }
3700
3701 session->ticket = mbedtls_calloc(1, session->ticket_len);
3702 if (session->ticket == NULL) {
3703 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3704 }
3705
3706 memcpy(session->ticket, p, session->ticket_len);
3707 p += session->ticket_len;
3708 }
3709
3710 if (4 > (size_t) (end - p)) {
3711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3712 }
3713
3714 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3715 p += 4;
3716 }
3717#endif /* MBEDTLS_SSL_CLI_C */
3718#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3719 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3720 if (8 > (size_t) (end - p)) {
3721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3722 }
3723 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3724 p += 8;
3725 }
3726#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3727#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3728
3729 /*
3730 * Misc extension-related info
3731 */
3732#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3733 if (1 > (size_t) (end - p)) {
3734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3735 }
3736
3737 session->mfl_code = *p++;
3738#endif
3739
3740#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3741 if (1 > (size_t) (end - p)) {
3742 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3743 }
3744
3745 session->encrypt_then_mac = *p++;
3746#endif
3747
3748 /* Done, should have consumed entire buffer */
3749 if (p != end) {
3750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3751 }
3752
3753 return 0;
3754}
3755
3756#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3757
3758#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3759/* Serialization of TLS 1.3 sessions:
3760 *
David Horstmanncb01b362024-02-29 17:31:46 +00003761 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003762 */
3763#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3764MBEDTLS_CHECK_RETURN_CRITICAL
3765static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3766 unsigned char *buf,
3767 size_t buf_len,
3768 size_t *olen)
3769{
3770 unsigned char *p = buf;
3771#if defined(MBEDTLS_SSL_CLI_C) && \
3772 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3773 size_t hostname_len = (session->hostname == NULL) ?
3774 0 : strlen(session->hostname) + 1;
3775#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003776
3777#if defined(MBEDTLS_SSL_SRV_C) && \
3778 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003779 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003780 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003781#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003782 size_t needed = 4 /* ticket_age_add */
3783 + 1 /* ticket_flags */
3784 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003785
David Horstmanne59f9702024-02-29 16:08:57 +00003786 *olen = 0;
3787
3788 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3789 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3790 }
3791 needed += session->resumption_key_len; /* resumption_key */
3792
3793#if defined(MBEDTLS_SSL_EARLY_DATA)
3794 needed += 4; /* max_early_data_size */
3795#endif
3796#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3797 needed += 2; /* record_size_limit */
3798#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3799
3800#if defined(MBEDTLS_HAVE_TIME)
3801 needed += 8; /* ticket_creation_time or ticket_reception_time */
3802#endif
3803
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003804#if defined(MBEDTLS_SSL_SRV_C)
3805 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3806#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003807 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003808 + alpn_len; /* alpn */
3809#endif
3810 }
3811#endif /* MBEDTLS_SSL_SRV_C */
3812
David Horstmanne59f9702024-02-29 16:08:57 +00003813#if defined(MBEDTLS_SSL_CLI_C)
3814 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3815#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3816 needed += 2 /* hostname_len */
3817 + hostname_len; /* hostname */
3818#endif
3819
3820 needed += 4 /* ticket_lifetime */
3821 + 2; /* ticket_len */
3822
3823 /* Check size_t overflow */
3824 if (session->ticket_len > SIZE_MAX - needed) {
3825 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3826 }
3827
3828 needed += session->ticket_len; /* ticket */
3829 }
3830#endif /* MBEDTLS_SSL_CLI_C */
3831
3832 *olen = needed;
3833 if (needed > buf_len) {
3834 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3835 }
3836
3837 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3838 p[4] = session->ticket_flags;
3839
3840 /* save resumption_key */
3841 p[5] = session->resumption_key_len;
3842 p += 6;
3843 memcpy(p, session->resumption_key, session->resumption_key_len);
3844 p += session->resumption_key_len;
3845
3846#if defined(MBEDTLS_SSL_EARLY_DATA)
3847 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3848 p += 4;
3849#endif
3850#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3851 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3852 p += 2;
3853#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3854
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003855#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003856 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003857#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003858 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3859 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003860#endif /* MBEDTLS_HAVE_TIME */
3861
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003862#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003863 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3864 p += 2;
3865
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003866 if (alpn_len > 0) {
3867 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003868 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003869 p += alpn_len;
3870 }
3871#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3872 }
3873#endif /* MBEDTLS_SSL_SRV_C */
3874
David Horstmanne59f9702024-02-29 16:08:57 +00003875#if defined(MBEDTLS_SSL_CLI_C)
3876 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3877#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3878 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3879 p += 2;
3880 if (hostname_len > 0) {
3881 /* save host name */
3882 memcpy(p, session->hostname, hostname_len);
3883 p += hostname_len;
3884 }
3885#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3886
3887#if defined(MBEDTLS_HAVE_TIME)
3888 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3889 p += 8;
3890#endif
3891 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3892 p += 4;
3893
3894 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3895 p += 2;
3896
3897 if (session->ticket != NULL && session->ticket_len > 0) {
3898 memcpy(p, session->ticket, session->ticket_len);
3899 p += session->ticket_len;
3900 }
3901 }
3902#endif /* MBEDTLS_SSL_CLI_C */
3903 return 0;
3904}
3905
3906MBEDTLS_CHECK_RETURN_CRITICAL
3907static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3908 const unsigned char *buf,
3909 size_t len)
3910{
3911 const unsigned char *p = buf;
3912 const unsigned char *end = buf + len;
3913
3914 if (end - p < 6) {
3915 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3916 }
3917 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3918 session->ticket_flags = p[4];
3919
3920 /* load resumption_key */
3921 session->resumption_key_len = p[5];
3922 p += 6;
3923
3924 if (end - p < session->resumption_key_len) {
3925 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3926 }
3927
3928 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3929 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3930 }
3931 memcpy(session->resumption_key, p, session->resumption_key_len);
3932 p += session->resumption_key_len;
3933
3934#if defined(MBEDTLS_SSL_EARLY_DATA)
3935 if (end - p < 4) {
3936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3937 }
3938 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3939 p += 4;
3940#endif
3941#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3942 if (end - p < 2) {
3943 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3944 }
3945 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3946 p += 2;
3947#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3948
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003949#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003950 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003951#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003952 if (end - p < 8) {
3953 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3954 }
3955 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3956 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003957#endif /* MBEDTLS_HAVE_TIME */
3958
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003959#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003960 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003961
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003962 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003963 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3964 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003965
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003966 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3967 p += 2;
3968
3969 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003970 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3971 }
3972
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003973 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003974 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3975 if (ret != 0) {
3976 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003977 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003978 p += alpn_len;
3979 }
3980#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3981 }
3982#endif /* MBEDTLS_SSL_SRV_C */
3983
David Horstmanne59f9702024-02-29 16:08:57 +00003984#if defined(MBEDTLS_SSL_CLI_C)
3985 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3986#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3987 size_t hostname_len;
3988 /* load host name */
3989 if (end - p < 2) {
3990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3991 }
3992 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3993 p += 2;
3994
3995 if (end - p < (long int) hostname_len) {
3996 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3997 }
3998 if (hostname_len > 0) {
3999 session->hostname = mbedtls_calloc(1, hostname_len);
4000 if (session->hostname == NULL) {
4001 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4002 }
4003 memcpy(session->hostname, p, hostname_len);
4004 p += hostname_len;
4005 }
4006#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4007
4008#if defined(MBEDTLS_HAVE_TIME)
4009 if (end - p < 8) {
4010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4011 }
4012 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4013 p += 8;
4014#endif
4015 if (end - p < 4) {
4016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4017 }
4018 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4019 p += 4;
4020
4021 if (end - p < 2) {
4022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4023 }
4024 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4025 p += 2;
4026
4027 if (end - p < (long int) session->ticket_len) {
4028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4029 }
4030 if (session->ticket_len > 0) {
4031 session->ticket = mbedtls_calloc(1, session->ticket_len);
4032 if (session->ticket == NULL) {
4033 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4034 }
4035 memcpy(session->ticket, p, session->ticket_len);
4036 p += session->ticket_len;
4037 }
4038 }
4039#endif /* MBEDTLS_SSL_CLI_C */
4040
4041 return 0;
4042
4043}
4044#else /* MBEDTLS_SSL_SESSION_TICKETS */
4045MBEDTLS_CHECK_RETURN_CRITICAL
4046static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4047 unsigned char *buf,
4048 size_t buf_len,
4049 size_t *olen)
4050{
4051 ((void) session);
4052 ((void) buf);
4053 ((void) buf_len);
4054 *olen = 0;
4055 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4056}
4057
4058static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004059 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004060 size_t buf_len)
4061{
4062 ((void) session);
4063 ((void) buf);
4064 ((void) buf_len);
4065 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4066}
4067#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4068#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4069
Paul Bakker5121ce52009-01-03 21:22:43 +00004070/*
Hanno Beckera835da52019-05-16 12:39:07 +01004071 * Define ticket header determining Mbed TLS version
4072 * and structure of the ticket.
4073 */
4074
Hanno Becker94ef3b32019-05-16 12:50:45 +01004075/*
Hanno Becker50b59662019-05-28 14:30:45 +01004076 * Define bitflag determining compile-time settings influencing
4077 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004078 */
4079
Hanno Becker50b59662019-05-28 14:30:45 +01004080#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004081#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004082#else
Hanno Becker3e088662019-05-29 11:10:18 +01004083#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004084#endif /* MBEDTLS_HAVE_TIME */
4085
4086#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004087#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004088#else
Hanno Becker3e088662019-05-29 11:10:18 +01004089#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004090#endif /* MBEDTLS_X509_CRT_PARSE_C */
4091
David Horstmann5c5a32f2024-02-13 17:53:35 +00004092#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004093#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004094#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004095#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004096#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4097
Hanno Becker94ef3b32019-05-16 12:50:45 +01004098#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004099#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004100#else
Hanno Becker3e088662019-05-29 11:10:18 +01004101#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004102#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4103
4104#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004105#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004106#else
Hanno Becker3e088662019-05-29 11:10:18 +01004107#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004108#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4109
Hanno Becker94ef3b32019-05-16 12:50:45 +01004110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004111#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004112#else
Hanno Becker3e088662019-05-29 11:10:18 +01004113#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004114#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4115
Hanno Becker94ef3b32019-05-16 12:50:45 +01004116#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4117#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4118#else
4119#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4120#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4121
David Horstmann92b258b2024-02-13 17:23:34 +00004122#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4123#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4124#else
4125#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4126#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4127
4128#if defined(MBEDTLS_SSL_EARLY_DATA)
4129#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4130#else
4131#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4132#endif /* MBEDTLS_SSL_EARLY_DATA */
4133
4134#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4135#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4136#else
4137#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4138#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4139
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004140#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4141 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004142#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4143#else
4144#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4145#endif /* MBEDTLS_SSL_ALPN */
4146
Hanno Becker3e088662019-05-29 11:10:18 +01004147#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4148#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4149#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4150#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004151#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4152#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004153#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004154#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4155#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4156#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004157#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004158
Hanno Becker50b59662019-05-28 14:30:45 +01004159#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 ((uint16_t) ( \
4161 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4162 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4163 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4164 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4165 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4166 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004167 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004168 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4169 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004170 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4171 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4172 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4173 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004174 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004175 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004176 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004177
Chien Wong4e9683e2023-12-28 17:07:43 +08004178static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004179 MBEDTLS_VERSION_MAJOR,
4180 MBEDTLS_VERSION_MINOR,
4181 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4183 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004184};
Hanno Beckera835da52019-05-16 12:39:07 +01004185
4186/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004187 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004188 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004189 *
David Horstmanncb01b362024-02-29 17:31:46 +00004190 * TLS 1.2 session:
4191 *
4192 * struct {
4193 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4194 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4195 * uint32 ticket_lifetime;
4196 * #endif
4197 * } ClientOnlyData;
4198 *
4199 * struct {
4200 * #if defined(MBEDTLS_HAVE_TIME)
4201 * uint64 start_time;
4202 * #endif
4203 * uint8 session_id_len; // at most 32
4204 * opaque session_id[32];
4205 * opaque master[48]; // fixed length in the standard
4206 * uint32 verify_result;
4207 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4208 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4209 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004210 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004211 * opaque peer_cert_digest<0..2^8-1>
4212 * #endif
4213 * select (endpoint) {
4214 * case client: ClientOnlyData;
4215 * case server: uint64 ticket_creation_time;
4216 * };
4217 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4218 * uint8 mfl_code; // up to 255 according to standard
4219 * #endif
4220 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4221 * uint8 encrypt_then_mac; // 0 or 1
4222 * #endif
4223 * } serialized_session_tls12;
4224 *
4225 *
4226 * TLS 1.3 Session:
4227 *
4228 * struct {
4229 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4230 * opaque hostname<0..2^16-1>;
4231 * #endif
4232 * #if defined(MBEDTLS_HAVE_TIME)
4233 * uint64 ticket_reception_time;
4234 * #endif
4235 * uint32 ticket_lifetime;
4236 * opaque ticket<1..2^16-1>;
4237 * } ClientOnlyData;
4238 *
4239 * struct {
4240 * uint32 ticket_age_add;
4241 * uint8 ticket_flags;
4242 * opaque resumption_key<0..255>;
4243 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4244 * uint32 max_early_data_size;
4245 * #endif
4246 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4247 * uint16 record_size_limit;
4248 * #endif
4249 * select ( endpoint ) {
4250 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004251 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004252 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004253 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004254 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004255 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004256 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004257 * #endif
4258 * };
4259 * } serialized_session_tls13;
4260 *
4261 *
4262 * SSL session:
4263 *
4264 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004265 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004266 * opaque mbedtls_version[3]; // library version: major, minor, patch
4267 * opaque session_format[2]; // library-version specific 16-bit field
4268 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004269 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004270 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004271 * Note: When updating the format, remember to keep
4272 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004273 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004274 * // In this version, `session_format` determines
4275 * // the setting of those compile-time
4276 * // configuration options which influence
4277 * // the structure of mbedtls_ssl_session.
4278 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004279 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004280 * // - TLS 1.2 (0x0303)
4281 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004282 * uint8_t endpoint;
4283 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004284 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004285 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004286 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004287 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004288 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004289 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004290 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004291 *
4292 * };
4293 *
4294 * } serialized_session;
4295 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004296 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004297
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004298MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004299static int ssl_session_save(const mbedtls_ssl_session *session,
4300 unsigned char omit_header,
4301 unsigned char *buf,
4302 size_t buf_len,
4303 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004304{
4305 unsigned char *p = buf;
4306 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004307 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004308#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4309 size_t out_len;
4310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4311#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (session == NULL) {
4313 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4314 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004315
Gilles Peskine449bd832023-01-11 14:50:10 +01004316 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004317 /*
4318 * Add Mbed TLS version identifier
4319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (used <= buf_len) {
4323 memcpy(p, ssl_serialized_session_header,
4324 sizeof(ssl_serialized_session_header));
4325 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004326 }
4327 }
4328
4329 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004330 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004331 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004332 used += 1 /* TLS version */
4333 + 1 /* endpoint */
4334 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (used <= buf_len) {
4336 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004337 *p++ = session->endpoint;
4338 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4339 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004340 }
4341
4342 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004343 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004345#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 case MBEDTLS_SSL_VERSION_TLS1_2:
4347 used += ssl_tls12_session_save(session, p, remaining_len);
4348 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004349#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4350
Jerry Yu251a12e2022-07-13 15:15:48 +08004351#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 case MBEDTLS_SSL_VERSION_TLS1_3:
4353 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4354 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4355 return ret;
4356 }
4357 used += out_len;
4358 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004359#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 default:
4362 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004363 }
4364
4365 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 if (used > buf_len) {
4367 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4368 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004369
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004371}
4372
4373/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004374 * Public wrapper for ssl_session_save()
4375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004376int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4377 unsigned char *buf,
4378 size_t buf_len,
4379 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004380{
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004382}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004383
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004384/*
4385 * Deserialize session, see mbedtls_ssl_session_save() for format.
4386 *
4387 * This internal version is wrapped by a public function that cleans up in
4388 * case of error, and has an extra option omit_header.
4389 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004390MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004391static int ssl_session_load(mbedtls_ssl_session *session,
4392 unsigned char omit_header,
4393 const unsigned char *buf,
4394 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004395{
4396 const unsigned char *p = buf;
4397 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004398 size_t remaining_len;
4399
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (session == NULL) {
4402 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4403 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004404
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004406 /*
4407 * Check Mbed TLS version identifier
4408 */
4409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4411 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004413
4414 if (memcmp(p, ssl_serialized_session_header,
4415 sizeof(ssl_serialized_session_header)) != 0) {
4416 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4417 }
4418 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004419 }
4420
4421 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004422 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004423 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004424 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4426 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004427 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004428 session->endpoint = *p++;
4429 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4430 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004431
4432 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004433 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004435#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 case MBEDTLS_SSL_VERSION_TLS1_2:
4437 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4439
Jerry Yu438ddd82022-07-07 06:55:50 +00004440#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 case MBEDTLS_SSL_VERSION_TLS1_3:
4442 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004443#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4444
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 default:
4446 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004447 }
4448}
4449
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004450/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004451 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004453int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4454 const unsigned char *buf,
4455 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004456{
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 if (ret != 0) {
4460 mbedtls_ssl_session_free(session);
4461 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004464}
4465
4466/*
Paul Bakker1961b702013-01-25 14:49:24 +01004467 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004468 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004469MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004470static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004471{
4472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4473
Ronald Cron66dbf912022-02-02 15:33:46 +01004474 /*
4475 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004476 * that were written into the output buffer by the previous handshake step,
4477 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004478 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4479 * We proceed to the next handshake step only when all data from the
4480 * previous one have been sent to the peer, thus we make sure that this is
4481 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4482 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4483 * we have to wait before to go ahead.
4484 * In the case of TLS 1.3, handshake step handlers do not send data to the
4485 * peer. Data are only sent here and through
4486 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004487 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4490 return ret;
4491 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004492
4493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4495 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4496 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4497 return ret;
4498 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004499 }
4500#endif /* MBEDTLS_SSL_PROTO_DTLS */
4501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004503}
4504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004506{
Hanno Becker41934dd2021-08-07 19:13:43 +01004507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004510 ssl->conf == NULL ||
4511 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4513 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004514 }
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 ret = ssl_prepare_handshake_step(ssl);
4517 if (ret != 0) {
4518 return ret;
4519 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 ret = mbedtls_ssl_handle_pending_alert(ssl);
4522 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004523 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 }
Jerry Yue7047812021-09-13 19:26:39 +08004525
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004526 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4527 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4528 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004530#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4532 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004533 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004536 case MBEDTLS_SSL_HELLO_REQUEST:
4537 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004538 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004539 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004540
Ronald Cron9f0fba32022-02-10 16:45:15 +01004541 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004543 break;
4544
4545 default:
4546#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4548 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4549 } else {
4550 ret = mbedtls_ssl_handshake_client_step(ssl);
4551 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004552#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004554#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004556#endif
4557 }
Jerry Yub9930e72021-08-06 17:11:51 +08004558 }
Ronald Cron6291b232023-03-08 15:51:25 +01004559#endif /* MBEDTLS_SSL_CLI_C */
4560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004563#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4564 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004566 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 ret = mbedtls_ssl_handshake_server_step(ssl);
4568 }
Ronald Cron6291b232023-03-08 15:51:25 +01004569#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4570 ret = mbedtls_ssl_handshake_server_step(ssl);
4571#else
4572 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004573#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004574 }
4575#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004578 /* handshake_step return error. And it is same
4579 * with alert_reason.
4580 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (ssl->send_alert) {
4582 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004583 goto cleanup;
4584 }
4585 }
4586
4587cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004589}
4590
4591/*
4592 * Perform the SSL handshake
4593 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004595{
4596 int ret = 0;
4597
Hanno Beckera817ea42020-10-20 15:20:23 +01004598 /* Sanity checks */
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (ssl == NULL || ssl->conf == NULL) {
4601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4602 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004603
Hanno Beckera817ea42020-10-20 15:20:23 +01004604#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4606 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4607 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4608 "mbedtls_ssl_set_timer_cb() for DTLS"));
4609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004610 }
4611#endif /* MBEDTLS_SSL_PROTO_DTLS */
4612
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004614
Hanno Beckera817ea42020-10-20 15:20:23 +01004615 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4617 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004620 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 }
Paul Bakker1961b702013-01-25 14:49:24 +01004622 }
4623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004625
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004627}
4628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629#if defined(MBEDTLS_SSL_RENEGOTIATION)
4630#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004631/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004632 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004633 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004634MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004635static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004636{
Janos Follath865b3eb2019-12-16 11:46:15 +00004637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004640
4641 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004642 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4643 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4646 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4647 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004648 }
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004655
4656/*
4657 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 * - any side: calling mbedtls_ssl_renegotiate(),
4659 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4660 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004661 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004662 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004665int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004666{
Janos Follath865b3eb2019-12-16 11:46:15 +00004667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 if ((ret = ssl_handshake_init(ssl)) != 0) {
4672 return ret;
4673 }
Paul Bakker48916f92012-09-16 19:57:18 +00004674
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004675 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4676 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4679 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4680 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004681 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004683 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004685 }
4686#endif
4687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004688 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4689 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4692 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4693 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004694 }
4695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004699}
4700
4701/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004702 * Renegotiate current connection on client,
4703 * or request renegotiation on server
4704 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004705int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (ssl == NULL || ssl->conf == NULL) {
4710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4711 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004714 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4716 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4718 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004721
4722 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (ssl->out_left != 0) {
4724 return mbedtls_ssl_flush_output(ssl);
4725 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004728 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004729#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004732 /*
4733 * On client, either start the renegotiation process or,
4734 * if already in progress, continue the handshake
4735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4737 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004739 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004740
4741 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4742 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4743 return ret;
4744 }
4745 } else {
4746 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4747 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4748 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004749 }
4750 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004755#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004758{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004759 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4760
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004762 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004764
Valerio Setti6f0441d2023-07-03 12:11:36 +02004765#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004766#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (ssl->handshake->group_list_heap_allocated) {
4768 mbedtls_free((void *) handshake->group_list);
4769 }
Brett Warrene0edc842021-08-17 09:53:13 +01004770 handshake->group_list = NULL;
4771#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004772#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004773
Ronald Crone68ab4f2022-10-05 12:46:29 +02004774#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004775#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (ssl->handshake->sig_algs_heap_allocated) {
4777 mbedtls_free((void *) handshake->sig_algs);
4778 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004779 handshake->sig_algs = NULL;
4780#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004781#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (ssl->handshake->certificate_request_context) {
4783 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004784 }
4785#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004786#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004787
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004788#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4790 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004791 handshake->async_in_progress = 0;
4792 }
4793#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4794
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004795#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004796#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004798#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004799 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004800#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004801#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004802#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004803#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004805#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004806 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004807#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004808#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004812#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004813#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004814 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004816#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004817
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004818#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004819#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004821 /*
4822 * Opaque keys are not stored in the handshake's data and it's the user
4823 * responsibility to destroy them. Clear ones, instead, are created by
4824 * the TLS library and should be destroyed at the same level
4825 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4827 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004828 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004829 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4830#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004832#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004833#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004835 handshake->ecjpake_cache = NULL;
4836 handshake->ecjpake_cache_len = 0;
4837#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004838#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004839
Valerio Setti7aeec542023-07-05 18:57:21 +02004840#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004841 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004842 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004843 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004845#endif
4846
Ronald Cron73fe8df2022-10-05 14:31:43 +02004847#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004850 /* The maintenance of the external PSK key slot is the
4851 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (ssl->handshake->psk_opaque_is_internal) {
4853 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004854 ssl->handshake->psk_opaque_is_internal = 0;
4855 }
4856 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4857 }
Neil Armstronge952a302022-05-03 10:22:14 +02004858#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004860 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004861 }
Neil Armstronge952a302022-05-03 10:22:14 +02004862#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004863#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4866 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004867 /*
4868 * Free only the linked list wrapper, not the keys themselves
4869 * since the belong to the SNI callback
4870 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004873
Gilles Peskineeccd8882020-03-10 12:19:08 +01004874#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4876 if (handshake->ecrs_peer_cert != NULL) {
4877 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4878 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004879 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004880#endif
4881
Hanno Becker75173122019-02-06 16:18:31 +00004882#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4883 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004885#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4886
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004887#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4889 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004890#endif /* MBEDTLS_SSL_CLI_C &&
4891 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004892
4893#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 mbedtls_ssl_flight_free(handshake->flight);
4895 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004896#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004897
Valerio Settiea59c432023-07-25 11:14:03 +02004898#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004899 if (handshake->xxdh_psa_privkey_is_external == 0) {
4900 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 }
Valerio Settiea59c432023-07-25 11:14:03 +02004902#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004903
Ronald Cron6f135e12021-12-08 16:57:54 +01004904#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 mbedtls_ssl_transform_free(handshake->transform_handshake);
4906 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004907#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4909 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004910#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004911#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004912
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004913
4914#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4915 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4916 * processes datagrams and the fact that a datagram is allowed to have
4917 * several records in it, it is possible that the I/O buffers are not
4918 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4920 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004921#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004922
Jerry Yuba9c7272021-10-30 11:54:10 +08004923 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 mbedtls_platform_zeroize(handshake,
4925 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004926}
4927
Gilles Peskine449bd832023-01-11 14:50:10 +01004928void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004929{
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004931 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004936#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004937
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004938#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004939#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4940 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004942#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004944#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004945
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004946#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4947 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004948 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004949#endif
4950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004952}
4953
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004954#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004955
4956#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4957#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4958#else
4959#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4960#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4961
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004962#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004963
4964#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4965#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4966#else
4967#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4968#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4969
4970#if defined(MBEDTLS_SSL_ALPN)
4971#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4972#else
4973#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4974#endif /* MBEDTLS_SSL_ALPN */
4975
4976#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4977#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4978#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4979#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4980
4981#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 ((uint32_t) ( \
4983 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4984 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4985 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4986 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4987 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4988 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4989 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4990 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004991
Chien Wong4e9683e2023-12-28 17:07:43 +08004992static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004993 MBEDTLS_VERSION_MAJOR,
4994 MBEDTLS_VERSION_MINOR,
4995 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4997 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4998 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4999 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5000 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005001};
5002
Paul Bakker5121ce52009-01-03 21:22:43 +00005003/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005004 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005005 *
5006 * The format of the serialized data is:
5007 * (in the presentation language of TLS, RFC 8446 section 3)
5008 *
5009 * // header
5010 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005011 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005012 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005013 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005014 * Note: When updating the format, remember to keep these
5015 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005016 *
5017 * // session sub-structure
5018 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5019 * // transform sub-structure
5020 * uint8 random[64]; // ServerHello.random+ClientHello.random
5021 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5022 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5023 * // fields from ssl_context
5024 * uint32 badmac_seen; // DTLS: number of records with failing MAC
5025 * uint64 in_window_top; // DTLS: last validated record seq_num
5026 * uint64 in_window; // DTLS: bitmask for replay protection
5027 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5028 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5029 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5030 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5031 *
5032 * Note that many fields of the ssl_context or sub-structures are not
5033 * serialized, as they fall in one of the following categories:
5034 *
5035 * 1. forced value (eg in_left must be 0)
5036 * 2. pointer to dynamically-allocated memory (eg session, transform)
5037 * 3. value can be re-derived from other data (eg session keys from MS)
5038 * 4. value was temporary (eg content of input buffer)
5039 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005041int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5042 unsigned char *buf,
5043 size_t buf_len,
5044 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005045{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005046 unsigned char *p = buf;
5047 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005048 size_t session_len;
5049 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005050
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005051 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005052 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5053 * this function's documentation.
5054 *
5055 * These are due to assumptions/limitations in the implementation. Some of
5056 * them are likely to stay (no handshake in progress) some might go away
5057 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005058 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005059 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5061 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5062 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (ssl->handshake != NULL) {
5065 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5066 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005067 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005068 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (ssl->transform == NULL || ssl->session == NULL) {
5070 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5071 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005072 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005073 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (mbedtls_ssl_check_pending(ssl) != 0) {
5075 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005077 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 if (ssl->out_left != 0) {
5079 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005081 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005082 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5084 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5085 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005086 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005087 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5089 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5090 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005091 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005092 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5094 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5095 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005096 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005097 /* Renegotiation must not be enabled */
5098#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5100 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5101 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005102 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005103#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005104
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005105 /*
5106 * Version and format identifier
5107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005109
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 if (used <= buf_len) {
5111 memcpy(p, ssl_serialized_context_header,
5112 sizeof(ssl_serialized_context_header));
5113 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005114 }
5115
5116 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005117 * Session (length + data)
5118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5120 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5121 return ret;
5122 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005123
5124 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 if (used <= buf_len) {
5126 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005127 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 ret = ssl_session_save(ssl->session, 1,
5130 p, session_len, &session_len);
5131 if (ret != 0) {
5132 return ret;
5133 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005134
5135 p += session_len;
5136 }
5137
5138 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005139 * Transform
5140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 used += sizeof(ssl->transform->randbytes);
5142 if (used <= buf_len) {
5143 memcpy(p, ssl->transform->randbytes,
5144 sizeof(ssl->transform->randbytes));
5145 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005146 }
5147
5148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005149 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005151 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005153 p += ssl->transform->in_cid_len;
5154
5155 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005157 p += ssl->transform->out_cid_len;
5158 }
5159#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5160
5161 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005162 * Saved fields from top-level ssl_context structure
5163 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005164 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 if (used <= buf_len) {
5166 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005167 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005168 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005169
5170#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5171 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (used <= buf_len) {
5173 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005174 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005177 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005178 }
5179#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5180
5181#if defined(MBEDTLS_SSL_PROTO_DTLS)
5182 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005184 *p++ = ssl->disable_datagram_packing;
5185 }
5186#endif /* MBEDTLS_SSL_PROTO_DTLS */
5187
Jerry Yuae0b2e22021-10-08 15:21:19 +08005188 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (used <= buf_len) {
5190 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005191 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005192 }
5193
5194#if defined(MBEDTLS_SSL_PROTO_DTLS)
5195 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (used <= buf_len) {
5197 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005198 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005199 }
5200#endif /* MBEDTLS_SSL_PROTO_DTLS */
5201
5202#if defined(MBEDTLS_SSL_ALPN)
5203 {
5204 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005206 : 0;
5207
5208 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005210 *p++ = alpn_len;
5211
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (ssl->alpn_chosen != NULL) {
5213 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005214 p += alpn_len;
5215 }
5216 }
5217 }
5218#endif /* MBEDTLS_SSL_ALPN */
5219
5220 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005221 * Done
5222 */
5223 *olen = used;
5224
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (used > buf_len) {
5226 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5227 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005232}
5233
5234/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005235 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005236 *
5237 * This internal version is wrapped by a public function that cleans up in
5238 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005239 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005241static int ssl_context_load(mbedtls_ssl_context *ssl,
5242 const unsigned char *buf,
5243 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005244{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005245 const unsigned char *p = buf;
5246 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005247 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005250 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005251#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005252
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005253 /*
5254 * The context should have been freshly setup or reset.
5255 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005256 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005257 * renegotiating, or if the user mistakenly loaded a session first.)
5258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5260 ssl->session != NULL) {
5261 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005262 }
5263
5264 /*
5265 * We can't check that the config matches the initial one, but we can at
5266 * least check it matches the requirements for serializing.
5267 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005268 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005269#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005270 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005271#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005272 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5273 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5274 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5275 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005277 }
5278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005280
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005281 /*
5282 * Check version identifier
5283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5285 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005286 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005287
5288 if (memcmp(p, ssl_serialized_context_header,
5289 sizeof(ssl_serialized_context_header)) != 0) {
5290 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5291 }
5292 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005293
5294 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005295 * Session
5296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 if ((size_t) (end - p) < 4) {
5298 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5299 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005300
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005301 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005302 p += 4;
5303
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005304 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005305 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005306 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005307 ssl->session_in = ssl->session;
5308 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005309 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if ((size_t) (end - p) < session_len) {
5312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5313 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005314
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 ret = ssl_session_load(ssl->session, 1, p, session_len);
5316 if (ret != 0) {
5317 mbedtls_ssl_session_free(ssl->session);
5318 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005319 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005320
5321 p += session_len;
5322
5323 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005324 * Transform
5325 */
5326
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005327 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005328 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005329#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005330 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005331 ssl->transform_in = ssl->transform;
5332 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005333 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005334#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005335
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5338 if (prf_func == NULL) {
5339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5340 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005341
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005342 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5344 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5345 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 ret = ssl_tls12_populate_transform(ssl->transform,
5348 ssl->session->ciphersuite,
5349 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005350#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005352#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 prf_func,
5354 p, /* currently pointing to randbytes */
5355 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5356 ssl->conf->endpoint,
5357 ssl);
5358 if (ret != 0) {
5359 return ret;
5360 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005361#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005363
5364#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5365 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 if ((size_t) (end - p) < 1) {
5367 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5368 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005369
5370 ssl->transform->in_cid_len = *p++;
5371
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5373 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5374 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005377 p += ssl->transform->in_cid_len;
5378
5379 ssl->transform->out_cid_len = *p++;
5380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5382 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5383 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005384
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005386 p += ssl->transform->out_cid_len;
5387#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5388
5389 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005390 * Saved fields from top-level ssl_context structure
5391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if ((size_t) (end - p) < 4) {
5393 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5394 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005395
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005396 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005397 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005398
5399#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 if ((size_t) (end - p) < 16) {
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->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005405 p += 8;
5406
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005407 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005408 p += 8;
5409#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5410
5411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if ((size_t) (end - p) < 1) {
5413 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5414 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005415
5416 ssl->disable_datagram_packing = *p++;
5417#endif /* MBEDTLS_SSL_PROTO_DTLS */
5418
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5420 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5421 }
5422 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5423 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005424
5425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 if ((size_t) (end - p) < 2) {
5427 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5428 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005429
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005430 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005431 p += 2;
5432#endif /* MBEDTLS_SSL_PROTO_DTLS */
5433
5434#if defined(MBEDTLS_SSL_ALPN)
5435 {
5436 uint8_t alpn_len;
5437 const char **cur;
5438
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 if ((size_t) (end - p) < 1) {
5440 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5441 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005442
5443 alpn_len = *p++;
5444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005446 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5448 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005449 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005450 ssl->alpn_chosen = *cur;
5451 break;
5452 }
5453 }
5454 }
5455
5456 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5458 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5459 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005460
5461 p += alpn_len;
5462 }
5463#endif /* MBEDTLS_SSL_ALPN */
5464
5465 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005466 * Forced fields from top-level ssl_context structure
5467 *
5468 * Most of them already set to the correct value by mbedtls_ssl_init() and
5469 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5470 */
5471 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005472 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005473
Hanno Becker361b10d2019-08-30 10:42:49 +01005474 /* Adjust pointers for header fields of outgoing records to
5475 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005477
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005478#if defined(MBEDTLS_SSL_PROTO_DTLS)
5479 ssl->in_epoch = 1;
5480#endif
5481
5482 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5483 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005484 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5485 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 if (ssl->handshake != NULL) {
5487 mbedtls_ssl_handshake_free(ssl);
5488 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005489 ssl->handshake = NULL;
5490 }
5491
5492 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005493 * Done - should have consumed entire buffer
5494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 if (p != end) {
5496 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5497 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005498
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005500}
5501
5502/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005503 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005505int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5506 const unsigned char *buf,
5507 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005508{
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (ret != 0) {
5512 mbedtls_ssl_free(context);
5513 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005516}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005517#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005518
5519/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005520 * Free an SSL context
5521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005522void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005523{
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005525 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005529
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005531#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5532 size_t out_buf_len = ssl->out_buf_len;
5533#else
5534 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5535#endif
5536
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005537 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005538 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 }
5540
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005542#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5543 size_t in_buf_len = ssl->in_buf_len;
5544#else
5545 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5546#endif
5547
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005548 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005549 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005550 }
5551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 if (ssl->transform) {
5553 mbedtls_ssl_transform_free(ssl->transform);
5554 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005555 }
5556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (ssl->handshake) {
5558 mbedtls_ssl_handshake_free(ssl);
5559 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005560
5561#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5563 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005564#endif
5565
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 mbedtls_ssl_session_free(ssl->session_negotiate);
5567 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005568 }
5569
Ronald Cron6f135e12021-12-08 16:57:54 +01005570#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 mbedtls_ssl_transform_free(ssl->transform_application);
5572 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005573#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (ssl->session) {
5576 mbedtls_ssl_session_free(ssl->session);
5577 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005578 }
5579
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005580#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005582 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005583 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005588#endif
5589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005591
Paul Bakker86f04f42013-02-14 11:20:09 +01005592 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005594}
5595
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005596/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005597 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005599void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005600{
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005602}
5603
Gilles Peskineae270bf2021-06-02 00:05:29 +02005604/* The selection should be the same as mbedtls_x509_crt_profile_default in
5605 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005606 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005607 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005608 * about this list.
5609 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005610static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005611#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005612 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005613#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005614#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005615 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005616#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005617#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005618 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005619#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005620#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005621 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005622#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005623#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005624 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005625#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005626#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005627 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005628#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005629#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005630 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005631#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005632#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005633 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005634#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005635#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005636 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5637 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5638 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5639 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5640 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5641#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005642 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005643};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005644
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005645static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005646 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5647 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5648 0
5649};
5650
Ronald Crone68ab4f2022-10-05 12:46:29 +02005651#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005652
Jerry Yu909df7b2022-01-22 11:56:27 +08005653/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005654 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005655 * rules SHOULD be upheld.
5656 * - No duplicate entries.
5657 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005658 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005659 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005660 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005661static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005662
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005663#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005664 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005665 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005666 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005667 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5668#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005669
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005670#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005671 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005672 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005673 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005674 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5675#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005676
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005677#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005678 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005679 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005680 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005681 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5682#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005683
Yanray Wang1136fad2023-11-22 16:54:31 +08005684#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005685 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005686#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005687
Yanray Wang1136fad2023-11-22 16:54:31 +08005688#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005689 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005690#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005691
Yanray Wang1136fad2023-11-22 16:54:31 +08005692#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005693 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005694#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005695
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005696#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005697 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005698#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005699
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005700#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005701 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005702#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005703
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005704#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005705 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005706#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005707
Gabor Mezei15b95a62022-05-09 16:37:58 +02005708 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005709};
5710
5711/* NOTICE: see above */
5712#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5713static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005714
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005715#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005716#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005718#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005719#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5720 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005721#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005722#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005724#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005725#endif /* MBEDTLS_MD_CAN_SHA512 */
5726
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005727#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005728#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005730#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005731#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5732 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005733#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005734#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005736#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005737#endif /* MBEDTLS_MD_CAN_SHA384 */
5738
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005739#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005740#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005742#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005743#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5744 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005745#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005746#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005748#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005749#endif /* MBEDTLS_MD_CAN_SHA256 */
5750
Gabor Mezei15b95a62022-05-09 16:37:58 +02005751 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005752};
5753#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005754
Jerry Yu909df7b2022-01-22 11:56:27 +08005755/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005756static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005757
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005758#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005759 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005760 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005761 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005762 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5763#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005764
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005765#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005766 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005767 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005768 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005769 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5770#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005771
Gabor Mezei15b95a62022-05-09 16:37:58 +02005772 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005773};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005774
Jerry Yu909df7b2022-01-22 11:56:27 +08005775/* NOTICE: see above */
5776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5777static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005778
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005779#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005780#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005781 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005782#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005783#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005784
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005785#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005786#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005788#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005789#endif /* MBEDTLS_MD_CAN_SHA384 */
5790
Gabor Mezei15b95a62022-05-09 16:37:58 +02005791 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005792};
Jerry Yu909df7b2022-01-22 11:56:27 +08005793#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5794
Ronald Crone68ab4f2022-10-05 12:46:29 +02005795#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005796
Chien Wong4e9683e2023-12-28 17:07:43 +08005797static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005798#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005799 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005800#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005801#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005802 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005803#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005804 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005805};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005806
Ronald Crone68ab4f2022-10-05 12:46:29 +02005807#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005808/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005809 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005810MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005811static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005812{
5813 size_t i, j;
5814 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5817 for (j = 0; j < i; j++) {
5818 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005819 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 }
5821 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5822 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5823 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005824 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005825 }
5826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005828}
5829
Ronald Crone68ab4f2022-10-05 12:46:29 +02005830#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005831
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005832/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005833 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005835int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5836 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005837{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005838#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005840#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005841
Ronald Crone68ab4f2022-10-05 12:46:29 +02005842#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5844 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5845 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005846 }
5847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5849 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5850 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005851 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005852
5853#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5855 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5856 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005857 }
5858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5860 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5861 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005862 }
5863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005864#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005865
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005866 /* Use the functions here so that they are covered in tests,
5867 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 mbedtls_ssl_conf_endpoint(conf, endpoint);
5869 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005870
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005871 /*
5872 * Things that are common to all presets
5873 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005874#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005876 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5877#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005878 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005879#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005880 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5881 * handling is disabled by default in Mbed TLS 3.6.x for backward
5882 * compatibility with client applications developed using Mbed TLS 3.5
5883 * or earlier with the default configuration.
5884 *
5885 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5886 * disabled, and a Mbed TLS client with the default configuration would
5887 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5888 * server.
5889 *
5890 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5891 * an Mbed TLS client with the default configuration establishes a
5892 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5893 * following the handshake the TLS 1.3 server sends NewSessionTicket
5894 * messages and the Mbed TLS client processes them, this results in
5895 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5896 * mbedtls_ssl_handshake(), ...) to eventually return an
5897 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5898 * (see the documentation of mbedtls_ssl_read() for more information on
5899 * that error code). Applications unaware of that TLS 1.3 specific non
5900 * fatal error code are then failing.
5901 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005902 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5903 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005904#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005905#endif
5906 }
5907#endif
5908
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005909#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5910 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5911#endif
5912
5913#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5914 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5915#endif
5916
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005917#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005918 conf->f_cookie_write = ssl_cookie_write_dummy;
5919 conf->f_cookie_check = ssl_cookie_check_dummy;
5920#endif
5921
5922#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5923 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5924#endif
5925
Janos Follath088ce432017-04-10 12:42:31 +01005926#if defined(MBEDTLS_SSL_SRV_C)
5927 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005928 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005929#endif
5930
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005931#if defined(MBEDTLS_SSL_PROTO_DTLS)
5932 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5933 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5934#endif
5935
5936#if defined(MBEDTLS_SSL_RENEGOTIATION)
5937 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 memset(conf->renego_period, 0x00, 2);
5939 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005940#endif
5941
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005942#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005944 const unsigned char dhm_p[] =
5945 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5946 const unsigned char dhm_g[] =
5947 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5950 dhm_p, sizeof(dhm_p),
5951 dhm_g, sizeof(dhm_g))) != 0) {
5952 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005953 }
5954 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005955#endif
5956
Ronald Cron6f135e12021-12-08 16:57:54 +01005957#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005958
5959#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005960 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005961#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005962 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005963#endif
5964#endif /* MBEDTLS_SSL_EARLY_DATA */
5965
Jerry Yud0766ec2022-09-22 10:46:57 +08005966#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005967 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005969#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005970 /*
5971 * Allow all TLS 1.3 key exchange modes by default.
5972 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005973 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005974#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005977#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005978 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005979 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005980#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005982#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005984#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005985 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5986 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005987#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5988 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5989 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5990#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5991 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5992 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5993#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005995#endif
5996 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005997
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005998 /*
5999 * Preset-specific defaults
6000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006002 /*
6003 * NSA Suite B
6004 */
6005 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006006
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006007 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006008
6009#if defined(MBEDTLS_X509_CRT_PARSE_C)
6010 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006011#endif
6012
Ronald Crone68ab4f2022-10-05 12:46:29 +02006013#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006014#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006016 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006018#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006020#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006021
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006022#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006023 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006024#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006025 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006026 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006027
6028 /*
6029 * Default
6030 */
6031 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006032
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006033 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006034
6035#if defined(MBEDTLS_X509_CRT_PARSE_C)
6036 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6037#endif
6038
Ronald Crone68ab4f2022-10-05 12:46:29 +02006039#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006040#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006042 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006044#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006046#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006047
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006048#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006049 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006050#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006051 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006052
6053#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6054 conf->dhm_min_bitlen = 1024;
6055#endif
6056 }
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006059}
6060
6061/*
6062 * Free mbedtls_ssl_config
6063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006064void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006065{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006066 if (conf == NULL) {
6067 return;
6068 }
6069
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006070#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 mbedtls_mpi_free(&conf->dhm_P);
6072 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006073#endif
6074
Ronald Cron73fe8df2022-10-05 14:31:43 +02006075#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006076#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006078 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6079 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006080#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006082 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006083 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006084 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006085 }
6086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006088 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006089 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006090 conf->psk_identity_len = 0;
6091 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006092#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006093
6094#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006095 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006096#endif
6097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006099}
6100
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006101#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006102 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006103/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006106unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6110 return MBEDTLS_SSL_SIG_RSA;
6111 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006112#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006113#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6115 return MBEDTLS_SSL_SIG_ECDSA;
6116 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006117#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006119}
6120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006122{
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006124 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006126 case MBEDTLS_PK_ECDSA:
6127 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006129 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006131 }
6132}
6133
Gilles Peskine449bd832023-01-11 14:50:10 +01006134mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006135{
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137#if defined(MBEDTLS_RSA_C)
6138 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006140#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006141#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006144#endif
6145 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006147 }
6148}
Valerio Settie9646ec2023-08-02 20:02:28 +02006149#endif /* MBEDTLS_PK_C &&
6150 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006151
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006152/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006153 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006155mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006156{
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006158#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006161#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006162#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006163 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006165#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006166#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006169#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006170#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 return MBEDTLS_MD_SHA256;
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_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006177#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006178#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006179 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006180 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006181#endif
6182 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006184 }
6185}
6186
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006187/*
6188 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006190unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006191{
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006193#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006194 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006196#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006197#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006198 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006200#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006201#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006202 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006204#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006205#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006206 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 return MBEDTLS_SSL_HASH_SHA256;
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_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006210 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006212#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006213#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006214 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006216#endif
6217 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006219 }
6220}
6221
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006222/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006223 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006224 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006226int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006227{
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 if (group_list == NULL) {
6231 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006232 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 for (; *group_list != 0; group_list++) {
6235 if (*group_list == tls_id) {
6236 return 0;
6237 }
6238 }
6239
6240 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006241}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006242
Valerio Setti49e69072023-06-27 17:27:51 +02006243#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006244/*
6245 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006247int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006248{
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006252 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006256}
Valerio Setti49e69072023-06-27 17:27:51 +02006257#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006258
Valerio Setti18c9fed2022-12-30 17:44:24 +01006259static const struct {
6260 uint16_t tls_id;
6261 mbedtls_ecp_group_id ecp_group_id;
6262 psa_ecc_family_t psa_family;
6263 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006264} tls_id_match_table[] =
6265{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006266#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006267 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006268#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006269#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006270 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006271#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006272#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006273 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006274#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006275#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006276 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006277#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006278#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006279 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006280#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006281#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006282 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006283#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006284#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006285 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006286#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006287#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006288 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006289#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006290#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006291 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006292#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006293#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006294 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006295#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006296#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006297 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006298#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006299#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006300 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006301#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006302#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006303 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006304#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006305 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006306};
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006309 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006311{
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6313 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006314 if (type != NULL) {
6315 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 }
6317 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006318 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006320 return PSA_SUCCESS;
6321 }
6322 }
6323
6324 return PSA_ERROR_NOT_SUPPORTED;
6325}
6326
Gilles Peskine449bd832023-01-11 14:50:10 +01006327mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006328{
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6330 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006331 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006333 }
6334
6335 return MBEDTLS_ECP_DP_NONE;
6336}
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006339{
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6341 i++) {
6342 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006343 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006345 }
6346
6347 return 0;
6348}
6349
Valerio Setti67419f02023-01-04 16:12:42 +01006350#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006351static const struct {
6352 uint16_t tls_id;
6353 const char *name;
6354} tls_id_curve_name_table[] =
6355{
Valerio Setti54e23792023-07-07 10:49:27 +02006356 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6357 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6358 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6359 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6360 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6361 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6362 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6363 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6364 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6365 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6366 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6367 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6368 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006369 { 0, NULL },
6370};
6371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006373{
Valerio Settiacd32c02023-06-29 18:06:29 +02006374 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6375 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6376 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006378 }
6379
6380 return NULL;
6381}
Valerio Setti67419f02023-01-04 16:12:42 +01006382#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006383
Jerry Yu148165c2021-09-24 23:20:59 +08006384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006385int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6386 const mbedtls_md_type_t md,
6387 unsigned char *dst,
6388 size_t dst_len,
6389 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006390{
Ronald Cronf6893e12022-01-07 22:09:01 +01006391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6392 psa_hash_operation_t *hash_operation_to_clone;
6393 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6394
Jerry Yu148165c2021-09-24 23:20:59 +08006395 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006398#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 case MBEDTLS_MD_SHA384:
6400 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6401 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006402#endif
6403
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006404#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 case MBEDTLS_MD_SHA256:
6406 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6407 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006408#endif
6409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 default:
6411 goto exit;
6412 }
6413
6414 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6415 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006416 goto exit;
6417 }
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6420 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006421 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006423
6424exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006425#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6426 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006427 (void) ssl;
6428#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006429 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006430}
6431#else /* MBEDTLS_USE_PSA_CRYPTO */
6432
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006433#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006435static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6436 unsigned char *dst,
6437 size_t dst_len,
6438 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006439{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006440 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006441 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 if (dst_len < 48) {
6444 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6445 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006446
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006447 mbedtls_md_init(&sha384);
6448 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006449 if (ret != 0) {
6450 goto exit;
6451 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006452 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006453 if (ret != 0) {
6454 goto exit;
6455 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006456
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006457 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006458 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006459 goto exit;
6460 }
6461
6462 *olen = 48;
6463
6464exit:
6465
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006466 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006468}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006469#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006470
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006471#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006472MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006473static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6474 unsigned char *dst,
6475 size_t dst_len,
6476 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006477{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006478 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006479 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 if (dst_len < 32) {
6482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6483 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006484
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006485 mbedtls_md_init(&sha256);
6486 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6487 if (ret != 0) {
6488 goto exit;
6489 }
6490 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6491 if (ret != 0) {
6492 goto exit;
6493 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006494
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006495 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6496 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006497 goto exit;
6498 }
6499
6500 *olen = 32;
6501
6502exit:
6503
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006504 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006506}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006507#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6510 const mbedtls_md_type_t md,
6511 unsigned char *dst,
6512 size_t dst_len,
6513 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006514{
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006516
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006517#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 case MBEDTLS_MD_SHA384:
6519 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006520#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006521
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006522#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 case MBEDTLS_MD_SHA256:
6524 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006525#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006526
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006528#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6529 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 (void) ssl;
6531 (void) dst;
6532 (void) dst_len;
6533 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006534#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006536 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006538}
XiaokangQian647719a2021-12-07 09:16:29 +00006539
Jerry Yu148165c2021-09-24 23:20:59 +08006540#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006541
Ronald Crone68ab4f2022-10-05 12:46:29 +02006542#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006543/* mbedtls_ssl_parse_sig_alg_ext()
6544 *
6545 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6546 * value (TLS 1.3 RFC8446):
6547 * enum {
6548 * ....
6549 * ecdsa_secp256r1_sha256( 0x0403 ),
6550 * ecdsa_secp384r1_sha384( 0x0503 ),
6551 * ecdsa_secp521r1_sha512( 0x0603 ),
6552 * ....
6553 * } SignatureScheme;
6554 *
6555 * struct {
6556 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6557 * } SignatureSchemeList;
6558 *
6559 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6560 * value (TLS 1.2 RFC5246):
6561 * enum {
6562 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6563 * sha512(6), (255)
6564 * } HashAlgorithm;
6565 *
6566 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6567 * SignatureAlgorithm;
6568 *
6569 * struct {
6570 * HashAlgorithm hash;
6571 * SignatureAlgorithm signature;
6572 * } SignatureAndHashAlgorithm;
6573 *
6574 * SignatureAndHashAlgorithm
6575 * supported_signature_algorithms<2..2^16-2>;
6576 *
6577 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6578 * generalization of the TLS 1.2 signature algorithm extension.
6579 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6580 * `SignatureScheme` field of TLS 1.3
6581 *
6582 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006583int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6584 const unsigned char *buf,
6585 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006586{
6587 const unsigned char *p = buf;
6588 size_t supported_sig_algs_len = 0;
6589 const unsigned char *supported_sig_algs_end;
6590 uint16_t sig_alg;
6591 uint32_t common_idx = 0;
6592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6594 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006595 p += 2;
6596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 memset(ssl->handshake->received_sig_algs, 0,
6598 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006601 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 while (p < supported_sig_algs_end) {
6603 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6604 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006605 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6607 sig_alg,
6608 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006609#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6611 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6612 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006613 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006614 }
6615#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6618 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006621 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6622 common_idx += 1;
6623 }
6624 }
6625 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (p != end) {
6627 MBEDTLS_SSL_DEBUG_MSG(1,
6628 ("Signature algorithms extension length misaligned"));
6629 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6630 MBEDTLS_ERR_SSL_DECODE_ERROR);
6631 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006632 }
6633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (common_idx == 0) {
6635 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6636 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6637 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6638 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006639 }
6640
Gabor Mezei15b95a62022-05-09 16:37:58 +02006641 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006643}
6644
Ronald Crone68ab4f2022-10-05 12:46:29 +02006645#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006646
Jerry Yudc7bd172022-02-17 13:44:15 +08006647#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6648
6649#if defined(MBEDTLS_USE_PSA_CRYPTO)
6650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6652 mbedtls_svc_key_id_t key,
6653 psa_algorithm_t alg,
6654 const unsigned char *raw_psk, size_t raw_psk_length,
6655 const unsigned char *seed, size_t seed_length,
6656 const unsigned char *label, size_t label_length,
6657 const unsigned char *other_secret,
6658 size_t other_secret_length,
6659 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006660{
6661 psa_status_t status;
6662
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 status = psa_key_derivation_setup(derivation, alg);
6664 if (status != PSA_SUCCESS) {
6665 return status;
6666 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6669 status = psa_key_derivation_input_bytes(derivation,
6670 PSA_KEY_DERIVATION_INPUT_SEED,
6671 seed, seed_length);
6672 if (status != PSA_SUCCESS) {
6673 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006674 }
6675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 if (other_secret != NULL) {
6677 status = psa_key_derivation_input_bytes(derivation,
6678 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6679 other_secret, other_secret_length);
6680 if (status != PSA_SUCCESS) {
6681 return status;
6682 }
6683 }
6684
6685 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006686 status = psa_key_derivation_input_bytes(
6687 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 raw_psk, raw_psk_length);
6689 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006690 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006692 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if (status != PSA_SUCCESS) {
6694 return status;
6695 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006696
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 status = psa_key_derivation_input_bytes(derivation,
6698 PSA_KEY_DERIVATION_INPUT_LABEL,
6699 label, label_length);
6700 if (status != PSA_SUCCESS) {
6701 return status;
6702 }
6703 } else {
6704 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006705 }
6706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 status = psa_key_derivation_set_capacity(derivation, capacity);
6708 if (status != PSA_SUCCESS) {
6709 return status;
6710 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006713}
6714
Andrzej Kurek57d10632022-10-24 10:32:01 -04006715#if defined(PSA_WANT_ALG_SHA_384) || \
6716 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006717MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006718static int tls_prf_generic(mbedtls_md_type_t md_type,
6719 const unsigned char *secret, size_t slen,
6720 const char *label,
6721 const unsigned char *random, size_t rlen,
6722 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006723{
6724 psa_status_t status;
6725 psa_algorithm_t alg;
6726 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6727 psa_key_derivation_operation_t derivation =
6728 PSA_KEY_DERIVATION_OPERATION_INIT;
6729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006731 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006733 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006735
6736 /* Normally a "secret" should be long enough to be impossible to
6737 * find by brute force, and in particular should not be empty. But
6738 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6739 * and for this use case it makes sense to have a 0-length "secret".
6740 * Since the key API doesn't allow importing a key of length 0,
6741 * keep master_key=0, which setup_psa_key_derivation() understands
6742 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006744 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6746 psa_set_key_algorithm(&key_attributes, alg);
6747 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6750 if (status != PSA_SUCCESS) {
6751 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6752 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006753 }
6754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 status = setup_psa_key_derivation(&derivation,
6756 master_key, alg,
6757 NULL, 0,
6758 random, rlen,
6759 (unsigned char const *) label,
6760 (size_t) strlen(label),
6761 NULL, 0,
6762 dlen);
6763 if (status != PSA_SUCCESS) {
6764 psa_key_derivation_abort(&derivation);
6765 psa_destroy_key(master_key);
6766 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006767 }
6768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6770 if (status != PSA_SUCCESS) {
6771 psa_key_derivation_abort(&derivation);
6772 psa_destroy_key(master_key);
6773 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006774 }
6775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 status = psa_key_derivation_abort(&derivation);
6777 if (status != PSA_SUCCESS) {
6778 psa_destroy_key(master_key);
6779 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006780 }
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 if (!mbedtls_svc_key_id_is_null(master_key)) {
6783 status = psa_destroy_key(master_key);
6784 }
6785 if (status != PSA_SUCCESS) {
6786 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6787 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006788
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006790}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006791#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006792#else /* MBEDTLS_USE_PSA_CRYPTO */
6793
Andrzej Kurek57d10632022-10-24 10:32:01 -04006794#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006795 (defined(MBEDTLS_MD_CAN_SHA256) || \
6796 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006797MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006798static int tls_prf_generic(mbedtls_md_type_t md_type,
6799 const unsigned char *secret, size_t slen,
6800 const char *label,
6801 const unsigned char *random, size_t rlen,
6802 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006803{
6804 size_t nb;
6805 size_t i, j, k, md_len;
6806 unsigned char *tmp;
6807 size_t tmp_len = 0;
6808 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6809 const mbedtls_md_info_t *md_info;
6810 mbedtls_md_context_t md_ctx;
6811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6812
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6816 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6817 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 tmp_len = md_len + strlen(label) + rlen;
6822 tmp = mbedtls_calloc(1, tmp_len);
6823 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006824 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6825 goto exit;
6826 }
6827
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 nb = strlen(label);
6829 memcpy(tmp + md_len, label, nb);
6830 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006831 nb += rlen;
6832
6833 /*
6834 * Compute P_<hash>(secret, label + random)[0..dlen]
6835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006837 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6841 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 }
6844 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6845 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006846 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 }
6848 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6849 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006850 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 for (i = 0; i < dlen; i += md_len) {
6854 ret = mbedtls_md_hmac_reset(&md_ctx);
6855 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006856 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 }
6858 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6859 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006860 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 }
6862 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6863 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006864 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 ret = mbedtls_md_hmac_reset(&md_ctx);
6868 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006869 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 }
6871 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6872 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006873 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 }
6875 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6876 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006877 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006878 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006883 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006885 }
6886
6887exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 if (tmp != NULL) {
6891 mbedtls_platform_zeroize(tmp, tmp_len);
6892 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006899}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006900#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006901#endif /* MBEDTLS_USE_PSA_CRYPTO */
6902
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006903#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006904MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006905static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6906 const char *label,
6907 const unsigned char *random, size_t rlen,
6908 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006909{
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6911 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006912}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006913#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006914
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006915#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006916MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006917static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6918 const char *label,
6919 const unsigned char *random, size_t rlen,
6920 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006921{
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6923 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006924}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006925#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006926
Jerry Yuf009d862022-02-17 14:01:37 +08006927/*
6928 * Set appropriate PRF function and other SSL / TLS1.2 functions
6929 *
6930 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006931 * - hash associated with the ciphersuite (only used by TLS 1.2)
6932 *
6933 * Outputs:
6934 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6935 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006936MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006937static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6938 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006939{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006940#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006942 handshake->tls_prf = tls_prf_sha384;
6943 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6944 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006946#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006947#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006948 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006949 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006950 handshake->tls_prf = tls_prf_sha256;
6951 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6952 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6953 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006954#else
Jerry Yuf009d862022-02-17 14:01:37 +08006955 {
Jerry Yuf009d862022-02-17 14:01:37 +08006956 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006957 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006959 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006960#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006963}
Jerry Yud6ab2352022-02-17 14:03:43 +08006964
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006965/*
6966 * Compute master secret if needed
6967 *
6968 * Parameters:
6969 * [in/out] handshake
6970 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6971 * (PSA-PSK) ciphersuite_info, psk_opaque
6972 * [out] premaster (cleared)
6973 * [out] master
6974 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6975 * debug: conf->f_dbg, conf->p_dbg
6976 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006977 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006978 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006979MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006980static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6981 unsigned char *master,
6982 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006983{
6984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6985
6986 /* cf. RFC 5246, Section 8.1:
6987 * "The master secret is always exactly 48 bytes in length." */
6988 size_t const master_secret_len = 48;
6989
6990#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6991 unsigned char session_hash[48];
6992#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6993
6994 /* The label for the KDF used for key expansion.
6995 * This is either "master secret" or "extended master secret"
6996 * depending on whether the Extended Master Secret extension
6997 * is used. */
6998 char const *lbl = "master secret";
6999
Przemek Stekielae4ed302022-04-05 17:15:55 +02007000 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007001 * - If the Extended Master Secret extension is not used,
7002 * this is ClientHello.Random + ServerHello.Random
7003 * (see Sect. 8.1 in RFC 5246).
7004 * - If the Extended Master Secret extension is used,
7005 * this is the transcript of the handshake so far.
7006 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007007 unsigned char const *seed = handshake->randbytes;
7008 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007009
7010#if !defined(MBEDTLS_DEBUG_C) && \
7011 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7012 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007014 ssl = NULL; /* make sure we don't use it except for those cases */
7015 (void) ssl;
7016#endif
7017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 if (handshake->resume != 0) {
7019 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7020 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007021 }
7022
7023#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007025 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007026 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007027 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7028 if (ret != 0) {
7029 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7030 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007031
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7033 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007034 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007035#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007036
Przemek Stekiel99114f32022-04-22 11:20:09 +02007037#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007038 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007040 /* Perform PSK-to-MS expansion in a single step. */
7041 psa_status_t status;
7042 psa_algorithm_t alg;
7043 mbedtls_svc_key_id_t psk;
7044 psa_key_derivation_operation_t derivation =
7045 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007046 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007049
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007053 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007055 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007057
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007058 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007062 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007063 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007064 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007065 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007066 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007067 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007068 other_secret_len = 48;
7069 other_secret = handshake->premaster + 2;
7070 break;
7071 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007072 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007073 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7074 other_secret = handshake->premaster + 2;
7075 break;
7076 default:
7077 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007078 }
7079
Gilles Peskine449bd832023-01-11 14:50:10 +01007080 status = setup_psa_key_derivation(&derivation, psk, alg,
7081 ssl->conf->psk, ssl->conf->psk_len,
7082 seed, seed_len,
7083 (unsigned char const *) lbl,
7084 (size_t) strlen(lbl),
7085 other_secret, other_secret_len,
7086 master_secret_len);
7087 if (status != PSA_SUCCESS) {
7088 psa_key_derivation_abort(&derivation);
7089 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007090 }
7091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 status = psa_key_derivation_output_bytes(&derivation,
7093 master,
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_abort(&derivation);
7101 if (status != PSA_SUCCESS) {
7102 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7103 }
7104 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007105#endif
7106 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007107#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7109 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007110 psa_status_t status;
7111 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7112 psa_key_derivation_operation_t derivation =
7113 PSA_KEY_DERIVATION_OPERATION_INIT;
7114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007116
7117 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 status = psa_key_derivation_setup(&derivation, alg);
7120 if (status != PSA_SUCCESS) {
7121 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007122 }
7123
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 status = psa_key_derivation_set_capacity(&derivation,
7125 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7126 if (status != PSA_SUCCESS) {
7127 psa_key_derivation_abort(&derivation);
7128 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007129 }
7130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7132 &derivation);
7133 if (status != PSA_SUCCESS) {
7134 psa_key_derivation_abort(&derivation);
7135 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007136 }
7137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 status = psa_key_derivation_output_bytes(&derivation,
7139 handshake->premaster,
7140 handshake->pmslen);
7141 if (status != PSA_SUCCESS) {
7142 psa_key_derivation_abort(&derivation);
7143 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7144 }
7145
7146 status = psa_key_derivation_abort(&derivation);
7147 if (status != PSA_SUCCESS) {
7148 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007149 }
7150 }
7151#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7153 lbl, seed, seed_len,
7154 master,
7155 master_secret_len);
7156 if (ret != 0) {
7157 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7158 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007159 }
7160
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7162 handshake->premaster,
7163 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007164
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 mbedtls_platform_zeroize(handshake->premaster,
7166 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007167 }
7168
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007170}
7171
Gilles Peskine449bd832023-01-11 14:50:10 +01007172int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007173{
7174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7175 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7176 ssl->handshake->ciphersuite_info;
7177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007179
7180 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007182 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 if (ret != 0) {
7184 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7185 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007186 }
7187
7188 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 ret = ssl_compute_master(ssl->handshake,
7190 ssl->session_negotiate->master,
7191 ssl);
7192 if (ret != 0) {
7193 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7194 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007195 }
7196
7197 /* Swap the client and server random values:
7198 * - MS derivation wanted client+server (RFC 5246 8.1)
7199 * - key derivation wants server+client (RFC 5246 6.3) */
7200 {
7201 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 memcpy(tmp, ssl->handshake->randbytes, 64);
7203 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7204 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7205 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007206 }
7207
7208 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7210 ssl->session_negotiate->ciphersuite,
7211 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007214#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 ssl->handshake->tls_prf,
7216 ssl->handshake->randbytes,
7217 ssl->tls_version,
7218 ssl->conf->endpoint,
7219 ssl);
7220 if (ret != 0) {
7221 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7222 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007223 }
7224
7225 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7227 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007228
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007232}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007235{
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007237#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007238 case MBEDTLS_SSL_HASH_SHA384:
7239 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7240 break;
7241#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007242#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007243 case MBEDTLS_SSL_HASH_SHA256:
7244 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7245 break;
7246#endif
7247 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007249 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007250#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7251 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007252 (void) ssl;
7253#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007255}
7256
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007257#if defined(MBEDTLS_USE_PSA_CRYPTO)
7258static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7259 const psa_hash_operation_t *hs_op,
7260 size_t buffer_size,
7261 unsigned char *hash,
7262 size_t *hlen)
7263{
7264 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007265 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007266
7267#if !defined(MBEDTLS_DEBUG_C)
7268 (void) ssl;
7269#endif
7270 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007271 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007272 if (status != PSA_SUCCESS) {
7273 goto exit;
7274 }
7275
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007276 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007277 if (status != PSA_SUCCESS) {
7278 goto exit;
7279 }
7280
7281 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7282 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7283
7284exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007285 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007286 return mbedtls_md_error_from_psa(status);
7287}
7288#else
7289static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7290 const mbedtls_md_context_t *hs_ctx,
7291 unsigned char *hash,
7292 size_t *hlen)
7293{
7294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007295 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007296
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007297 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007298
7299#if !defined(MBEDTLS_DEBUG_C)
7300 (void) ssl;
7301#endif
7302 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7303
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007304 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007305 if (ret != 0) {
7306 goto exit;
7307 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007308 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007309 if (ret != 0) {
7310 goto exit;
7311 }
7312
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007313 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007314 if (ret != 0) {
7315 goto exit;
7316 }
7317
7318 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7319
7320 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7321 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7322
7323exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007324 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007325 return ret;
7326}
7327#endif /* MBEDTLS_USE_PSA_CRYPTO */
7328
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007329#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007330int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7331 unsigned char *hash,
7332 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007333{
7334#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007335 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7336 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007337#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007338 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7339 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007340#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007341}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007342#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007343
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007344#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007345int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7346 unsigned char *hash,
7347 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007348{
7349#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007350 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7351 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007352#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007353 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7354 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007355#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007356}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007357#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007358
Neil Armstrong80f6f322022-05-03 17:56:38 +02007359#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7360 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007361int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007362{
7363 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007365 const unsigned char *psk = NULL;
7366 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007368
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007370 /*
7371 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007372 * checked before calling this function.
7373 *
7374 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007375 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7378 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7379 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007380 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007381 }
7382
7383 /*
7384 * PMS = struct {
7385 * opaque other_secret<0..2^16-1>;
7386 * opaque psk<0..2^16-1>;
7387 * };
7388 * with "other_secret" depending on the particular key exchange
7389 */
7390#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7392 if (end - p < 2) {
7393 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7394 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007397 p += 2;
7398
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if (end < p || (size_t) (end - p) < psk_len) {
7400 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7401 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007404 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007406#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7407#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007409 /*
7410 * other_secret already set by the ClientKeyExchange message,
7411 * and is 48 bytes long
7412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 if (end - p < 2) {
7414 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7415 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007416
7417 *p++ = 0;
7418 *p++ = 48;
7419 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007421#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7422#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7425 size_t len;
7426
7427 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007429 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7432 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007433 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007435 p += 2 + len;
7436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7438 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007439#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007440#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7443 size_t zlen;
7444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007446 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7448 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7449 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007450 }
7451
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007453 p += 2 + zlen;
7454
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7456 MBEDTLS_DEBUG_ECDH_Z);
7457 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007458#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007459 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7461 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007462 }
7463
7464 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if (end - p < 2) {
7466 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7467 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007470 p += 2;
7471
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (end < p || (size_t) (end - p) < psk_len) {
7473 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7474 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007477 p += psk_len;
7478
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007479 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007480
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007482}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007483#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007484
7485#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007486MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007487static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007488
7489#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007490int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007491{
7492 /* If renegotiation is not enforced, retransmit until we would reach max
7493 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007495 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7496 unsigned char doublings = 1;
7497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007499 ++doublings;
7500 ratio >>= 1;
7501 }
7502
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if (++ssl->renego_records_seen > doublings) {
7504 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7505 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007506 }
7507 }
7508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007510}
7511#endif
7512#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007513
Jerry Yud9526692022-02-17 14:23:47 +08007514/*
7515 * Handshake functions
7516 */
7517#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7518/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007519int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007520{
7521 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7522 ssl->handshake->ciphersuite_info;
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7527 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007528 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007530 }
7531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7533 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007534}
7535
Gilles Peskine449bd832023-01-11 14:50:10 +01007536int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007537{
7538 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7539 ssl->handshake->ciphersuite_info;
7540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007542
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7544 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007545 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007547 }
7548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7550 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007551}
7552
7553#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7554/* Some certificate support -> implement write and parse */
7555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007557{
7558 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7559 size_t i, n;
7560 const mbedtls_x509_crt *crt;
7561 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7562 ssl->handshake->ciphersuite_info;
7563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7567 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007568 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007570 }
7571
7572#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7574 if (ssl->handshake->client_auth == 0) {
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#endif /* MBEDTLS_SSL_CLI_C */
7581#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7583 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007584 /* Should never happen because we shouldn't have picked the
7585 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007587 }
7588 }
7589#endif
7590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007592
7593 /*
7594 * 0 . 0 handshake type
7595 * 1 . 3 handshake length
7596 * 4 . 6 length of all certs
7597 * 7 . 9 length of cert. 1
7598 * 10 . n-1 peer certificate
7599 * n . n+2 length of cert. 2
7600 * n+3 . ... upper level cert, etc.
7601 */
7602 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007604
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007606 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7608 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7609 " > %" MBEDTLS_PRINTF_SIZET,
7610 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7611 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007612 }
7613
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7615 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7616 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007619 i += n; crt = crt->next;
7620 }
7621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7623 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7624 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007625
7626 ssl->out_msglen = i;
7627 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7628 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7629
7630 ssl->state++;
7631
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7633 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7634 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007635 }
7636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007640}
7641
7642#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7643
7644#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007645MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007646static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7647 unsigned char *crt_buf,
7648 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007649{
7650 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 if (peer_crt == NULL) {
7653 return -1;
7654 }
Jerry Yud9526692022-02-17 14:23:47 +08007655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 if (peer_crt->raw.len != crt_buf_len) {
7657 return -1;
7658 }
Jerry Yud9526692022-02-17 14:23:47 +08007659
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007661}
7662#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007664static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7665 unsigned char *crt_buf,
7666 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007667{
7668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7669 unsigned char const * const peer_cert_digest =
7670 ssl->session->peer_cert_digest;
7671 mbedtls_md_type_t const peer_cert_digest_type =
7672 ssl->session->peer_cert_digest_type;
7673 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007675 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7676 size_t digest_len;
7677
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 if (peer_cert_digest == NULL || digest_info == NULL) {
7679 return -1;
7680 }
Jerry Yud9526692022-02-17 14:23:47 +08007681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 digest_len = mbedtls_md_get_size(digest_info);
7683 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7684 return -1;
7685 }
Jerry Yud9526692022-02-17 14:23:47 +08007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7688 if (ret != 0) {
7689 return -1;
7690 }
Jerry Yud9526692022-02-17 14:23:47 +08007691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007693}
7694#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7695#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7696
7697/*
7698 * Once the certificate message is read, parse it into a cert chain and
7699 * perform basic checks, but leave actual verification to the caller
7700 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007702static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7703 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007704{
7705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7706#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007708#endif
7709 size_t i, n;
7710 uint8_t alert;
7711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7713 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7714 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7715 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7716 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007717 }
7718
Gilles Peskine449bd832023-01-11 14:50:10 +01007719 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7720 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7721 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7722 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007723 }
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7726 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7727 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7728 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7729 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007730 }
7731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007733
7734 /*
7735 * Same message structure as in mbedtls_ssl_write_certificate()
7736 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007737 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 if (ssl->in_msg[i] != 0 ||
7740 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7741 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7742 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7743 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7744 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007745 }
7746
7747 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7748 i += 3;
7749
7750 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007752 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 if (i + 3 > ssl->in_hslen) {
7754 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7755 mbedtls_ssl_send_alert_message(ssl,
7756 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7757 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7758 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007759 }
7760 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7761 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 if (ssl->in_msg[i] != 0) {
7763 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7764 mbedtls_ssl_send_alert_message(ssl,
7765 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7766 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7767 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007768 }
7769
7770 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007771 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007772 i += 3;
7773
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 if (n < 128 || i + n > ssl->in_hslen) {
7775 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7776 mbedtls_ssl_send_alert_message(ssl,
7777 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7778 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7779 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007780 }
7781
7782 /* Check if we're handling the first CRT in the chain. */
7783#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007785 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007787 /* During client-side renegotiation, check that the server's
7788 * end-CRTs hasn't changed compared to the initial handshake,
7789 * mitigating the triple handshake attack. On success, reuse
7790 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7792 if (ssl_check_peer_crt_unchanged(ssl,
7793 &ssl->in_msg[i],
7794 n) != 0) {
7795 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7796 mbedtls_ssl_send_alert_message(ssl,
7797 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7798 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7799 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007800 }
7801
7802 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007804 }
7805#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7806
7807 /* Parse the next certificate in the chain. */
7808#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007810#else
7811 /* If we don't need to store the CRT chain permanently, parse
7812 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007813 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007814#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007816 case 0: /*ok*/
7817 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7818 /* Ignore certificate with an unknown algorithm: maybe a
7819 prior certificate was already trusted. */
7820 break;
7821
7822 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7823 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7824 goto crt_parse_der_failed;
7825
7826 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7827 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7828 goto crt_parse_der_failed;
7829
7830 default:
7831 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007832crt_parse_der_failed:
7833 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7834 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7835 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007836 }
7837
7838 i += n;
7839 }
7840
Gilles Peskine449bd832023-01-11 14:50:10 +01007841 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7842 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007843}
7844
7845#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007846MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007847static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007848{
Gilles Peskine449bd832023-01-11 14:50:10 +01007849 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7850 return -1;
7851 }
Jerry Yud9526692022-02-17 14:23:47 +08007852
Gilles Peskine449bd832023-01-11 14:50:10 +01007853 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007854 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7855 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7857 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7858 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007861}
7862#endif /* MBEDTLS_SSL_SRV_C */
7863
7864/* Check if a certificate message is expected.
7865 * Return either
7866 * - SSL_CERTIFICATE_EXPECTED, or
7867 * - SSL_CERTIFICATE_SKIP
7868 * indicating whether a Certificate message is expected or not.
7869 */
7870#define SSL_CERTIFICATE_EXPECTED 0
7871#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007872MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007873static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7874 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007875{
7876 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7877 ssl->handshake->ciphersuite_info;
7878
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7880 return SSL_CERTIFICATE_SKIP;
7881 }
Jerry Yud9526692022-02-17 14:23:47 +08007882
7883#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7885 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7886 return SSL_CERTIFICATE_SKIP;
7887 }
Jerry Yud9526692022-02-17 14:23:47 +08007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007890 ssl->session_negotiate->verify_result =
7891 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007893 }
7894 }
7895#else
7896 ((void) authmode);
7897#endif /* MBEDTLS_SSL_SRV_C */
7898
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007900}
7901
Jerry Yud9526692022-02-17 14:23:47 +08007902#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007903MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007904static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7905 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007906{
7907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7908 /* Remember digest of the peer's end-CRT. */
7909 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7911 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7912 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7913 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7914 mbedtls_ssl_send_alert_message(ssl,
7915 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7916 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007919 }
7920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 ret = mbedtls_md(mbedtls_md_info_from_type(
7922 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7923 start, len,
7924 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007925
7926 ssl->session_negotiate->peer_cert_digest_type =
7927 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7928 ssl->session_negotiate->peer_cert_digest_len =
7929 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7930
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007932}
7933
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007934MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007935static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7936 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007937{
7938 unsigned char *end = start + len;
7939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7940
7941 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7943 ret = mbedtls_pk_parse_subpubkey(&start, end,
7944 &ssl->handshake->peer_pubkey);
7945 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007946 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007948 }
7949
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007951}
7952#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7953
Gilles Peskine449bd832023-01-11 14:50:10 +01007954int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007955{
7956 int ret = 0;
7957 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02007958 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007959#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7960 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7961 ? ssl->handshake->sni_authmode
7962 : ssl->conf->authmode;
7963#else
7964 const int authmode = ssl->conf->authmode;
7965#endif
7966 void *rs_ctx = NULL;
7967 mbedtls_x509_crt *chain = NULL;
7968
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007970
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7972 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7973 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007974 goto exit;
7975 }
7976
7977#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 if (ssl->handshake->ecrs_enabled &&
7979 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007980 chain = ssl->handshake->ecrs_peer_cert;
7981 ssl->handshake->ecrs_peer_cert = NULL;
7982 goto crt_verify;
7983 }
7984#endif
7985
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007987 /* mbedtls_ssl_read_record may have sent an alert already. We
7988 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007990 goto exit;
7991 }
7992
7993#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007995 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007998 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 }
Jerry Yud9526692022-02-17 14:23:47 +08008000
8001 goto exit;
8002 }
8003#endif /* MBEDTLS_SSL_SRV_C */
8004
8005 /* Clear existing peer CRT structure in case we tried to
8006 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008008
Gilles Peskine449bd832023-01-11 14:50:10 +01008009 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8010 if (chain == NULL) {
8011 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8012 sizeof(mbedtls_x509_crt)));
8013 mbedtls_ssl_send_alert_message(ssl,
8014 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8015 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008016
8017 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8018 goto exit;
8019 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008021
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 ret = ssl_parse_certificate_chain(ssl, chain);
8023 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 }
Jerry Yud9526692022-02-17 14:23:47 +08008026
8027#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008029 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 }
Jerry Yud9526692022-02-17 14:23:47 +08008031
8032crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008033 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008034 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 }
Jerry Yud9526692022-02-17 14:23:47 +08008036#endif
8037
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008038 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8039 ssl->handshake->ciphersuite_info,
8040 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008042 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 }
Jerry Yud9526692022-02-17 14:23:47 +08008044
8045#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8046 {
8047 unsigned char *crt_start, *pk_start;
8048 size_t crt_len, pk_len;
8049
8050 /* We parse the CRT chain without copying, so
8051 * these pointers point into the input buffer,
8052 * and are hence still valid after freeing the
8053 * CRT chain. */
8054
8055 crt_start = chain->raw.p;
8056 crt_len = chain->raw.len;
8057
8058 pk_start = chain->pk_raw.p;
8059 pk_len = chain->pk_raw.len;
8060
8061 /* Free the CRT structures before computing
8062 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008063 mbedtls_x509_crt_free(chain);
8064 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008065 chain = NULL;
8066
Gilles Peskine449bd832023-01-11 14:50:10 +01008067 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8068 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008069 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008070 }
Jerry Yud9526692022-02-17 14:23:47 +08008071
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8073 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008074 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008075 }
Jerry Yud9526692022-02-17 14:23:47 +08008076 }
8077#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8078 /* Pass ownership to session structure. */
8079 ssl->session_negotiate->peer_cert = chain;
8080 chain = NULL;
8081#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8082
Gilles Peskine449bd832023-01-11 14:50:10 +01008083 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008084
8085exit:
8086
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008088 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 }
Jerry Yud9526692022-02-17 14:23:47 +08008090
8091#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008093 ssl->handshake->ecrs_peer_cert = chain;
8094 chain = NULL;
8095 }
8096#endif
8097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (chain != NULL) {
8099 mbedtls_x509_crt_free(chain);
8100 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008101 }
8102
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008104}
8105#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8106
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008107static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8108 unsigned char *padbuf, size_t hlen,
8109 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008110{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008111 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008112 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008114 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008115 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008116 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008117 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008118#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008120 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008121 mbedtls_md_context_t cloned_ctx;
8122 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008123#endif
8124
8125 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008127 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008129
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008131 ? "client finished"
8132 : "server finished";
8133
8134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008135 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008136
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008137 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008139 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008140 }
8141
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008142 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008144 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008145 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008146 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008147#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008148 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008149
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008150 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008151 if (ret != 0) {
8152 goto exit;
8153 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008154 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008155 if (ret != 0) {
8156 goto exit;
8157 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008158
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008159 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008160 if (ret != 0) {
8161 goto exit;
8162 }
8163#endif /* MBEDTLS_USE_PSA_CRYPTO */
8164
8165 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8166
Jerry Yu615bd6f2022-02-17 14:25:15 +08008167 /*
8168 * TLSv1.2:
8169 * hash = PRF( master, finished_label,
8170 * Hash( handshake ) )[0.11]
8171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008173 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008174
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008176
Paul Elliottecb95be2023-08-11 16:41:04 +01008177 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008178
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008179 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008180
8181exit:
8182#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008183 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008184 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008185#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008186 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008187 return ret;
8188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008189}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008190
8191#if defined(MBEDTLS_MD_CAN_SHA256)
8192static int ssl_calc_finished_tls_sha256(
8193 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8194{
8195 unsigned char padbuf[32];
8196 return ssl_calc_finished_tls_generic(ssl,
8197#if defined(MBEDTLS_USE_PSA_CRYPTO)
8198 &ssl->handshake->fin_sha256_psa,
8199#else
8200 &ssl->handshake->fin_sha256,
8201#endif
8202 padbuf, sizeof(padbuf),
8203 buf, from);
8204}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008205#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008206
Jerry Yub7ba49e2022-02-17 14:25:53 +08008207
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008208#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008209static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008211{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008212 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008213 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008214#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008215 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008216#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008217 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008218#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008219 padbuf, sizeof(padbuf),
8220 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008221}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008222#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008223
Gilles Peskine449bd832023-01-11 14:50:10 +01008224void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008225{
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008227
8228 /*
8229 * Free our handshake params
8230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 mbedtls_ssl_handshake_free(ssl);
8232 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008233 ssl->handshake = NULL;
8234
8235 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008236 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 if (ssl->transform) {
8239 mbedtls_ssl_transform_free(ssl->transform);
8240 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008241 }
8242 ssl->transform = ssl->transform_negotiate;
8243 ssl->transform_negotiate = NULL;
8244
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008246}
8247
Gilles Peskine449bd832023-01-11 14:50:10 +01008248void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008249{
8250 int resume = ssl->handshake->resume;
8251
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008253
8254#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008256 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8257 ssl->renego_records_seen = 0;
8258 }
8259#endif
8260
8261 /*
8262 * Free the previous session and switch in the current one
8263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008265#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8266 /* RFC 7366 3.1: keep the EtM state */
8267 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008269#endif
8270
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 mbedtls_ssl_session_free(ssl->session);
8272 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008273 }
8274 ssl->session = ssl->session_negotiate;
8275 ssl->session_negotiate = NULL;
8276
8277 /*
8278 * Add cache entry
8279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008281 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 resume == 0) {
8283 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8284 ssl->session->id,
8285 ssl->session->id_len,
8286 ssl->session) != 0) {
8287 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8288 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008289 }
8290
8291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8293 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008294 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008296
8297 /* Keep last flight around in case we need to resend it:
8298 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8300 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008301#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008303
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008304 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008305
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008307}
8308
Gilles Peskine449bd832023-01-11 14:50:10 +01008309int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008310{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008311 int ret;
8312 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008315
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008317
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008318 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8319 if (ret != 0) {
8320 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8321 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008322
8323 /*
8324 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8325 * may define some other value. Currently (early 2016), no defined
8326 * ciphersuite does this (and this is unlikely to change as activity has
8327 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8328 */
8329 hash_len = 12;
8330
8331#if defined(MBEDTLS_SSL_RENEGOTIATION)
8332 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008334#endif
8335
8336 ssl->out_msglen = 4 + hash_len;
8337 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8338 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8339
8340 /*
8341 * In case of session resuming, invert the client and server
8342 * ChangeCipherSpec messages order.
8343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008345#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008347 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008348 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008349#endif
8350#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008352 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008354#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008356 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008358
8359 /*
8360 * Switch to our negotiated transform and session parameters for outbound
8361 * data.
8362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008364
8365#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008367 unsigned char i;
8368
8369 /* Remember current epoch settings for resending */
8370 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8372 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008373
8374 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008376
8377
8378 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 for (i = 2; i > 0; i--) {
8380 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008381 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008382 }
8383 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008384
8385 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 if (i == 0) {
8387 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8388 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008389 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008393
8394 ssl->transform_out = ssl->transform_negotiate;
8395 ssl->session_out = ssl->session_negotiate;
8396
8397#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8399 mbedtls_ssl_send_flight_completed(ssl);
8400 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008401#endif
8402
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8404 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8405 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008406 }
8407
8408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8410 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8411 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8412 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008413 }
8414#endif
8415
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008417
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008419}
8420
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008421#define SSL_MAX_HASH_LEN 12
8422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008424{
8425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8426 unsigned int hash_len = 12;
8427 unsigned char buf[SSL_MAX_HASH_LEN];
8428
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008430
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008431 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8432 if (ret != 0) {
8433 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8434 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008435
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8437 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008438 goto exit;
8439 }
8440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8442 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8443 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8444 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008445 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8446 goto exit;
8447 }
8448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8450 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8451 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008452 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8453 goto exit;
8454 }
8455
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8457 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8458 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8459 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008460 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8461 goto exit;
8462 }
8463
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8465 buf, hash_len) != 0) {
8466 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8467 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8468 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008469 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8470 goto exit;
8471 }
8472
8473#if defined(MBEDTLS_SSL_RENEGOTIATION)
8474 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008476#endif
8477
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008479#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008481 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008483#endif
8484#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008486 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008488#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008490 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008492
8493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8495 mbedtls_ssl_recv_flight_completed(ssl);
8496 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008497#endif
8498
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008500
8501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 mbedtls_platform_zeroize(buf, hash_len);
8503 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008504}
8505
Jerry Yu392112c2022-02-17 14:34:10 +08008506#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8507/*
8508 * Helper to get TLS 1.2 PRF from ciphersuite
8509 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008511static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008512{
Jerry Yu392112c2022-02-17 14:34:10 +08008513 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008515#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8517 return tls_prf_sha384;
8518 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008519#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008520#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008521 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8523 return tls_prf_sha256;
8524 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008525 }
8526#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008527#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8528 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008529 (void) ciphersuite_info;
8530#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008531
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008533}
8534#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008537{
8538 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008539#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if (tls_prf == tls_prf_sha384) {
8541 return MBEDTLS_SSL_TLS_PRF_SHA384;
8542 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008543#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008544#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 if (tls_prf == tls_prf_sha256) {
8546 return MBEDTLS_SSL_TLS_PRF_SHA256;
8547 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008548#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008550}
8551
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008552/*
8553 * Populate a transform structure with session keys and all the other
8554 * necessary information.
8555 *
8556 * Parameters:
8557 * - [in/out]: transform: structure to populate
8558 * [in] must be just initialised with mbedtls_ssl_transform_init()
8559 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8560 * - [in] ciphersuite
8561 * - [in] master
8562 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008563 * - [in] tls_prf: pointer to PRF to use for key derivation
8564 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008565 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008566 * - [in] endpoint: client or server
8567 * - [in] ssl: used for:
8568 * - ssl->conf->{f,p}_export_keys
8569 * [in] optionally used for:
8570 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8571 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008572MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008573static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8574 int ciphersuite,
8575 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008576#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008578#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 ssl_tls_prf_t tls_prf,
8580 const unsigned char randbytes[64],
8581 mbedtls_ssl_protocol_version tls_version,
8582 unsigned endpoint,
8583 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008584{
8585 int ret = 0;
8586 unsigned char keyblk[256];
8587 unsigned char *key1;
8588 unsigned char *key2;
8589 unsigned char *mac_enc;
8590 unsigned char *mac_dec;
8591 size_t mac_key_len = 0;
8592 size_t iv_copy_len;
8593 size_t keylen;
8594 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008595 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008596#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008597 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008598 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008599#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008600
8601#if defined(MBEDTLS_USE_PSA_CRYPTO)
8602 psa_key_type_t key_type;
8603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8604 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008605 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008606 size_t key_bits;
8607 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8608#endif
8609
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008610 /*
8611 * Some data just needs copying into the structure
8612 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008613#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008614 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008615#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008616 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008617
8618#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008620#endif
8621
8622#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008624 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8625 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008627 }
8628#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8629
8630 /*
8631 * Get various info structures
8632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8634 if (ciphersuite_info == NULL) {
8635 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8636 ciphersuite));
8637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008638 }
8639
Neil Armstrongab555e02022-04-04 11:07:59 +02008640 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008641#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008643#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008645
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008647 transform->taglen =
8648 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008650
8651#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008652 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 transform->taglen,
8654 &alg,
8655 &key_type,
8656 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008657 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008659 goto end;
8660 }
8661#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008662 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 if (cipher_info == NULL) {
8664 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8665 ciphersuite_info->cipher));
8666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008667 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008668#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008669
Neil Armstronge4512952022-03-08 09:08:22 +01008670#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008671 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008673 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 (unsigned) ciphersuite_info->mac));
8675 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008676 }
8677#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008678 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 if (md_info == NULL) {
8680 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8681 (unsigned) ciphersuite_info->mac));
8682 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008683 }
Neil Armstronge4512952022-03-08 09:08:22 +01008684#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008685
8686#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8687 /* Copy own and peer's CID if the use of the CID
8688 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8690 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008691
8692 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8694 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8695 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008696
8697 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8699 ssl->handshake->peer_cid_len);
8700 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8701 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008702 }
8703#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8704
8705 /*
8706 * Compute key block using the PRF
8707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8709 if (ret != 0) {
8710 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8711 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008712 }
8713
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8715 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8716 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8717 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8718 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008719
8720 /*
8721 * Determine the appropriate key, IV and MAC length.
8722 */
8723
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008724#if defined(MBEDTLS_USE_PSA_CRYPTO)
8725 keylen = PSA_BITS_TO_BYTES(key_bits);
8726#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008728#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008729
Valerio Settie5707042023-10-11 11:54:42 +02008730#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008732 size_t explicit_ivlen;
8733
8734 transform->maclen = 0;
8735 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008736
8737 /* All modes haves 96-bit IVs, but the length of the static parts vary
8738 * with mode and version:
8739 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8740 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8741 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8742 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8743 * sequence number).
8744 */
8745 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008746
8747 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008750#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8752 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008753#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008756 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008758 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008760
8761 /* Minimum length of encrypted record */
8762 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8763 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 } else
Valerio Settie5707042023-10-11 11:54:42 +02008765#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008768 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008770#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008772#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008773 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008774#endif /* MBEDTLS_USE_PSA_CRYPTO */
8775
8776#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008777 /* Get MAC length */
8778 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8779#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008780 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8782 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8783 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008784 goto end;
8785 }
8786
8787 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008789#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008790 transform->maclen = mac_key_len;
8791
8792 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008795#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008796 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008797#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008798
8799 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008801 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008803 /*
8804 * GenericBlockCipher:
8805 * 1. if EtM is in use: one block plus MAC
8806 * otherwise: * first multiple of blocklen greater than maclen
8807 * 2. IV
8808 */
8809#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008811 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 + block_size;
8813 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008814#endif
8815 {
8816 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 + block_size
8818 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008819 }
8820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008822 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 } else {
8824 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008825 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8826 goto end;
8827 }
8828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008830#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8831 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8833 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008834 }
8835
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8837 (unsigned) keylen,
8838 (unsigned) transform->minlen,
8839 (unsigned) transform->ivlen,
8840 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008841
8842 /*
8843 * Finally setup the cipher contexts, IVs and MAC secrets.
8844 */
8845#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008847 key1 = keyblk + mac_key_len * 2;
8848 key2 = keyblk + mac_key_len * 2 + keylen;
8849
8850 mac_enc = keyblk;
8851 mac_dec = keyblk + mac_key_len;
8852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 iv_copy_len = (transform->fixed_ivlen) ?
8854 transform->fixed_ivlen : transform->ivlen;
8855 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8856 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8857 iv_copy_len);
8858 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008859#endif /* MBEDTLS_SSL_CLI_C */
8860#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008861 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008862 key1 = keyblk + mac_key_len * 2 + keylen;
8863 key2 = keyblk + mac_key_len * 2;
8864
8865 mac_enc = keyblk + mac_key_len;
8866 mac_dec = keyblk;
8867
Gilles Peskine449bd832023-01-11 14:50:10 +01008868 iv_copy_len = (transform->fixed_ivlen) ?
8869 transform->fixed_ivlen : transform->ivlen;
8870 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8871 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8872 iv_copy_len);
8873 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008874#endif /* MBEDTLS_SSL_SRV_C */
8875 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008877 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8878 goto end;
8879 }
8880
Paul Elliott4fb19552023-10-18 12:15:30 +01008881 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 ssl->f_export_keys(ssl->p_export_keys,
8883 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8884 master, 48,
8885 randbytes + 32,
8886 randbytes,
8887 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008888 }
8889
8890#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008891 transform->psa_alg = alg;
8892
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8894 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8895 psa_set_key_algorithm(&attributes, alg);
8896 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if ((status = psa_import_key(&attributes,
8899 key1,
8900 PSA_BITS_TO_BYTES(key_bits),
8901 &transform->psa_key_enc)) != PSA_SUCCESS) {
8902 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008903 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008905 goto end;
8906 }
8907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008909
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 if ((status = psa_import_key(&attributes,
8911 key2,
8912 PSA_BITS_TO_BYTES(key_bits),
8913 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008914 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008916 goto end;
8917 }
8918 }
8919#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8921 cipher_info)) != 0) {
8922 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008923 goto end;
8924 }
8925
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8927 cipher_info)) != 0) {
8928 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008929 goto end;
8930 }
8931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8933 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8934 MBEDTLS_ENCRYPT)) != 0) {
8935 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008936 goto end;
8937 }
8938
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8940 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8941 MBEDTLS_DECRYPT)) != 0) {
8942 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008943 goto end;
8944 }
8945
8946#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8948 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8949 MBEDTLS_PADDING_NONE)) != 0) {
8950 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008951 goto end;
8952 }
8953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8955 MBEDTLS_PADDING_NONE)) != 0) {
8956 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008957 goto end;
8958 }
8959 }
8960#endif /* MBEDTLS_CIPHER_MODE_CBC */
8961#endif /* MBEDTLS_USE_PSA_CRYPTO */
8962
Neil Armstrong29c0c042022-03-17 17:47:28 +01008963#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8964 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8965 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008969
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8971 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8972 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if ((status = psa_import_key(&attributes,
8975 mac_enc, mac_key_len,
8976 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008977 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008979 goto end;
8980 }
8981
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8983 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008984#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008986#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008988 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8990 PSA_KEY_USAGE_VERIFY_HASH);
8991 } else {
8992 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8993 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 if ((status = psa_import_key(&attributes,
8996 mac_dec, mac_key_len,
8997 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008998 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009000 goto end;
9001 }
9002#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9004 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009005 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 }
9007 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9008 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009009 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009011#endif /* MBEDTLS_USE_PSA_CRYPTO */
9012 }
9013#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9014
9015 ((void) mac_dec);
9016 ((void) mac_enc);
9017
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009018end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9020 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009021}
9022
Valerio Settia08b1a42022-11-17 15:10:02 +01009023#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9024 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009025int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 psa_pake_operation_t *pake_ctx,
9027 const unsigned char *buf,
9028 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009029{
9030 psa_status_t status;
9031 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009032 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009033 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9034 * At round two perform a single cycle
9035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009037
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 for (; remaining_steps > 0; remaining_steps--) {
9039 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009040 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009042 /* Length is stored at the first byte */
9043 size_t length = buf[input_offset];
9044 input_offset += 1;
9045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009047 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9048 }
9049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 status = psa_pake_input(pake_ctx, step,
9051 buf + input_offset, length);
9052 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009053 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009054 }
9055
9056 input_offset += length;
9057 }
9058 }
9059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009061 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009065}
9066
Valerio Setti6b3dab02022-11-17 17:14:54 +01009067int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 psa_pake_operation_t *pake_ctx,
9069 unsigned char *buf,
9070 size_t len, size_t *olen,
9071 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009072{
9073 psa_status_t status;
9074 size_t output_offset = 0;
9075 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009076 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009077 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9078 * At round two perform a single cycle
9079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009081
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 for (; remaining_steps > 0; remaining_steps--) {
9083 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9084 step <= PSA_PAKE_STEP_ZK_PROOF;
9085 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009086 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009087 * For each step, prepend 1 byte with the length of the data as
9088 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 status = psa_pake_output(pake_ctx, step,
9091 buf + output_offset + 1,
9092 len - output_offset - 1,
9093 &output_len);
9094 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009095 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009096 }
9097
Valerio Setti99d88c12022-11-22 16:03:43 +01009098 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009099
9100 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009101 }
9102 }
9103
9104 *olen = output_offset;
9105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009107}
Valerio Settia08b1a42022-11-17 15:10:02 +01009108#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9109
Jerry Yuee40f9d2022-02-17 14:55:16 +08009110#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009111int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9112 unsigned char *hash, size_t *hashlen,
9113 unsigned char *data, size_t data_len,
9114 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009115{
9116 psa_status_t status;
9117 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009118 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009119
Gilles Peskine449bd832023-01-11 14:50:10 +01009120 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009121
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 if ((status = psa_hash_setup(&hash_operation,
9123 hash_alg)) != PSA_SUCCESS) {
9124 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009125 goto exit;
9126 }
9127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9129 64)) != PSA_SUCCESS) {
9130 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009131 goto exit;
9132 }
9133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 if ((status = psa_hash_update(&hash_operation,
9135 data, data_len)) != PSA_SUCCESS) {
9136 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009137 goto exit;
9138 }
9139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9141 hashlen)) != PSA_SUCCESS) {
9142 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9143 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009144 }
9145
9146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if (status != PSA_SUCCESS) {
9148 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9149 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9150 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009151 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009153 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9154 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009156 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009158 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009160 }
9161 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009163}
9164
9165#else
9166
Gilles Peskine449bd832023-01-11 14:50:10 +01009167int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9168 unsigned char *hash, size_t *hashlen,
9169 unsigned char *data, size_t data_len,
9170 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009171{
9172 int ret = 0;
9173 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9175 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009176
Gilles Peskine449bd832023-01-11 14:50:10 +01009177 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009178
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009180
9181 /*
9182 * digitally-signed struct {
9183 * opaque client_random[32];
9184 * opaque server_random[32];
9185 * ServerDHParams params;
9186 * };
9187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9189 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009190 goto exit;
9191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9193 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009194 goto exit;
9195 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9197 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009198 goto exit;
9199 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9201 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009202 goto exit;
9203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9205 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009206 goto exit;
9207 }
9208
9209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (ret != 0) {
9213 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9214 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9215 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009218}
9219#endif /* MBEDTLS_USE_PSA_CRYPTO */
9220
Jerry Yud9d91da2022-02-17 14:57:06 +08009221#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9222
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009223/* Find the preferred hash for a given signature algorithm. */
9224unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 mbedtls_ssl_context *ssl,
9226 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009227{
Gabor Mezei078e8032022-04-27 21:17:56 +02009228 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009229 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9232 return MBEDTLS_SSL_HASH_NONE;
9233 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009236 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9238 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009239 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9241 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009242
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009243 mbedtls_md_type_t md_alg =
9244 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9245 if (md_alg == MBEDTLS_MD_NONE) {
9246 continue;
9247 }
9248
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009252 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009253 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009254
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9256 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9257 PSA_ALG_ECDSA(psa_hash_alg),
9258 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009259 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009261
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9263 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9264 PSA_ALG_RSA_PKCS1V15_SIGN(
9265 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 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009270#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009271
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009273 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009274 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009275
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009277}
9278
9279#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9280
Jerry Yudc7bd172022-02-17 13:44:15 +08009281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9282
XiaokangQian75d40ef2022-04-20 11:05:24 +00009283int mbedtls_ssl_validate_ciphersuite(
9284 const mbedtls_ssl_context *ssl,
9285 const mbedtls_ssl_ciphersuite_t *suite_info,
9286 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009288{
9289 (void) ssl;
9290
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 if (suite_info == NULL) {
9292 return -1;
9293 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009294
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 if ((suite_info->min_tls_version > max_tls_version) ||
9296 (suite_info->max_tls_version < min_tls_version)) {
9297 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009298 }
9299
XiaokangQian060d8672022-04-21 09:24:56 +00009300#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009301#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009302#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9304 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009305#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9307 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009308#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009309 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009311 }
9312#endif
9313
9314 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9315#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9317 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9318 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009319 }
9320#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9321#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9322
Gilles Peskine449bd832023-01-11 14:50:10 +01009323 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009324}
9325
Ronald Crone68ab4f2022-10-05 12:46:29 +02009326#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009327/*
9328 * Function for writing a signature algorithm extension.
9329 *
9330 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9331 * value (TLS 1.3 RFC8446):
9332 * enum {
9333 * ....
9334 * ecdsa_secp256r1_sha256( 0x0403 ),
9335 * ecdsa_secp384r1_sha384( 0x0503 ),
9336 * ecdsa_secp521r1_sha512( 0x0603 ),
9337 * ....
9338 * } SignatureScheme;
9339 *
9340 * struct {
9341 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9342 * } SignatureSchemeList;
9343 *
9344 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9345 * value (TLS 1.2 RFC5246):
9346 * enum {
9347 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9348 * sha512(6), (255)
9349 * } HashAlgorithm;
9350 *
9351 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9352 * SignatureAlgorithm;
9353 *
9354 * struct {
9355 * HashAlgorithm hash;
9356 * SignatureAlgorithm signature;
9357 * } SignatureAndHashAlgorithm;
9358 *
9359 * SignatureAndHashAlgorithm
9360 * supported_signature_algorithms<2..2^16-2>;
9361 *
9362 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9363 * generalization of the TLS 1.2 signature algorithm extension.
9364 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9365 * `SignatureScheme` field of TLS 1.3
9366 *
9367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009368int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9369 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009370{
9371 unsigned char *p = buf;
9372 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9373 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9374
9375 *out_len = 0;
9376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009378
9379 /* Check if we have space for header and length field:
9380 * - extension_type (2 bytes)
9381 * - extension_data_length (2 bytes)
9382 * - supported_signature_algorithms_length (2 bytes)
9383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009385 p += 6;
9386
9387 /*
9388 * Write supported_signature_algorithms
9389 */
9390 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9392 if (sig_alg == NULL) {
9393 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9394 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009395
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9397 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9398 *sig_alg,
9399 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9400 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009401 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 }
9403 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9404 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009405 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9407 *sig_alg,
9408 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009409 }
9410
9411 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009412 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 if (supported_sig_alg_len == 0) {
9414 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9415 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009416 }
9417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9419 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9420 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009421
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009422 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009423
9424#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009426#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009429}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009430#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009431
XiaokangQian40a35232022-05-07 09:02:40 +00009432#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009433/*
9434 * mbedtls_ssl_parse_server_name_ext
9435 *
9436 * Structure of server_name extension:
9437 *
9438 * enum {
9439 * host_name(0), (255)
9440 * } NameType;
9441 * opaque HostName<1..2^16-1>;
9442 *
9443 * struct {
9444 * NameType name_type;
9445 * select (name_type) {
9446 * case host_name: HostName;
9447 * } name;
9448 * } ServerName;
9449 * struct {
9450 * ServerName server_name_list<1..2^16-1>
9451 * } ServerNameList;
9452 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009454int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9455 const unsigned char *buf,
9456 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009457{
9458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9459 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009460 size_t server_name_list_len, hostname_len;
9461 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009464
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9466 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009467 p += 2;
9468
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009470 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 while (p < server_name_list_end) {
9472 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9473 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9474 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9475 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009476
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009478 /* sni_name is intended to be used only during the parsing of the
9479 * ClientHello message (it is reset to NULL before the end of
9480 * the message parsing). Thus it is ok to just point to the
9481 * reception buffer and not make a copy of it.
9482 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009483 ssl->handshake->sni_name = p + 3;
9484 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 if (ssl->conf->f_sni == NULL) {
9486 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9489 ssl, p + 3, hostname_len);
9490 if (ret != 0) {
9491 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9492 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9493 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9494 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9495 }
9496 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009497 }
9498
9499 p += hostname_len + 3;
9500 }
9501
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009503}
9504#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9505
XiaokangQianacb39922022-06-17 10:18:48 +00009506#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009508int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9509 const unsigned char *buf,
9510 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009511{
9512 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009513 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009514 const unsigned char *protocol_name_list;
9515 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009516 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009517
9518 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 if (ssl->conf->alpn_list == NULL) {
9520 return 0;
9521 }
XiaokangQianacb39922022-06-17 10:18:48 +00009522
9523 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009524 * RFC7301, section 3.1
9525 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009526 *
XiaokangQianc7403452022-06-23 03:24:12 +00009527 * struct {
9528 * ProtocolName protocol_name_list<2..2^16-1>
9529 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009530 */
9531
XiaokangQianc7403452022-06-23 03:24:12 +00009532 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009533 * protocol_name_list_len 2 bytes
9534 * protocol_name_len 1 bytes
9535 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009538
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009540 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009542 protocol_name_list = p;
9543 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009544
9545 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009547 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9549 protocol_name_len);
9550 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009551 MBEDTLS_SSL_PEND_FATAL_ALERT(
9552 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9554 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009555 }
9556
9557 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009558 }
9559
9560 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9562 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009565 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 if (protocol_name_len == alpn_len &&
9567 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009568 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009570 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009571
9572 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009573 }
9574 }
9575
XiaokangQian95d5f542022-06-24 02:29:26 +00009576 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009577 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9579 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9580 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009581}
9582
Gilles Peskine449bd832023-01-11 14:50:10 +01009583int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9584 unsigned char *buf,
9585 unsigned char *end,
9586 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009587{
9588 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009589 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009590 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 if (ssl->alpn_chosen == NULL) {
9593 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009594 }
9595
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 protocol_name_len = strlen(ssl->alpn_chosen);
9597 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009600 /*
9601 * 0 . 1 ext identifier
9602 * 2 . 3 ext length
9603 * 4 . 5 protocol list length
9604 * 6 . 6 protocol name length
9605 * 7 . 7+n protocol name
9606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009608
XiaokangQian95d5f542022-06-24 02:29:26 +00009609 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9612 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009613 /* Note: the length of the chosen protocol has been checked to be less
9614 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009617
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009619
9620#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009622#endif
9623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009625}
9626#endif /* MBEDTLS_SSL_ALPN */
9627
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009628#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009629 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009630 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009631 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009632int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9633 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009634{
9635 /* Initialize to suppress unnecessary compiler warning */
9636 size_t hostname_len = 0;
9637
9638 /* Check if new hostname is valid before
9639 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 if (hostname != NULL) {
9641 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009642
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9645 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009646 }
9647
9648 /* Now it's clear that we will overwrite the old hostname,
9649 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009651 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009652 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009653 }
9654
9655 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009656 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009657 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 } else {
9659 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9660 if (session->hostname == NULL) {
9661 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9662 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009663
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009665 }
9666
Gilles Peskine449bd832023-01-11 14:50:10 +01009667 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009668}
Xiaokang Qian03409292022-10-12 02:49:52 +00009669#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9670 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009671 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009672 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009673
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009674#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9675 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009676int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9677 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009678{
9679 size_t alpn_len = 0;
9680
9681 if (alpn != NULL) {
9682 alpn_len = strlen(alpn);
9683
9684 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9685 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9686 }
9687 }
9688
9689 if (session->ticket_alpn != NULL) {
9690 mbedtls_zeroize_and_free(session->ticket_alpn,
9691 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009692 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009693 }
9694
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009695 if (alpn != NULL) {
9696 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009697 if (session->ticket_alpn == NULL) {
9698 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9699 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009700 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009701 }
9702
9703 return 0;
9704}
9705#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009706
9707/*
9708 * The following functions are used by 1.2 and 1.3, client and server.
9709 */
9710#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9711int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9712 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9713 int recv_endpoint,
9714 mbedtls_ssl_protocol_version tls_version,
9715 uint32_t *flags)
9716{
9717 int ret = 0;
9718 unsigned int usage = 0;
9719 const char *ext_oid;
9720 size_t ext_len;
9721
9722 /*
9723 * keyUsage
9724 */
9725
9726 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9727 * to check what a compliant client will think while choosing which cert
9728 * to send to the client. */
9729#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9730 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9731 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9732 /* TLS 1.2 server part of the key exchange */
9733 switch (ciphersuite->key_exchange) {
9734 case MBEDTLS_KEY_EXCHANGE_RSA:
9735 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9736 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9737 break;
9738
9739 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9740 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9741 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9742 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9743 break;
9744
9745 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9746 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9747 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9748 break;
9749
9750 /* Don't use default: we want warnings when adding new values */
9751 case MBEDTLS_KEY_EXCHANGE_NONE:
9752 case MBEDTLS_KEY_EXCHANGE_PSK:
9753 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9754 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9755 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9756 usage = 0;
9757 }
9758 } else
9759#endif
9760 {
9761 /* This is either TLS 1.3 authentication, which always uses signatures,
9762 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9763 * options we implement, both using signatures. */
9764 (void) tls_version;
9765 (void) ciphersuite;
9766 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9767 }
9768
9769 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9770 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9771 ret = -1;
9772 }
9773
9774 /*
9775 * extKeyUsage
9776 */
9777
9778 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9779 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9780 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9781 } else {
9782 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9783 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9784 }
9785
9786 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9787 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9788 ret = -1;
9789 }
9790
9791 return ret;
9792}
9793
9794int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9795 int authmode,
9796 mbedtls_x509_crt *chain,
9797 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9798 void *rs_ctx)
9799{
9800 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9801 return 0;
9802 }
9803
9804 /*
9805 * Primary check: use the appropriate X.509 verification function
9806 */
9807 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9808 void *p_vrfy;
9809 if (ssl->f_vrfy != NULL) {
9810 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9811 f_vrfy = ssl->f_vrfy;
9812 p_vrfy = ssl->p_vrfy;
9813 } else {
9814 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9815 f_vrfy = ssl->conf->f_vrfy;
9816 p_vrfy = ssl->conf->p_vrfy;
9817 }
9818
9819 int ret = 0;
9820 int have_ca_chain_or_callback = 0;
9821#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9822 if (ssl->conf->f_ca_cb != NULL) {
9823 ((void) rs_ctx);
9824 have_ca_chain_or_callback = 1;
9825
9826 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9827 ret = mbedtls_x509_crt_verify_with_ca_cb(
9828 chain,
9829 ssl->conf->f_ca_cb,
9830 ssl->conf->p_ca_cb,
9831 ssl->conf->cert_profile,
9832 ssl->hostname,
9833 &ssl->session_negotiate->verify_result,
9834 f_vrfy, p_vrfy);
9835 } else
9836#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9837 {
9838 mbedtls_x509_crt *ca_chain;
9839 mbedtls_x509_crl *ca_crl;
9840#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9841 if (ssl->handshake->sni_ca_chain != NULL) {
9842 ca_chain = ssl->handshake->sni_ca_chain;
9843 ca_crl = ssl->handshake->sni_ca_crl;
9844 } else
9845#endif
9846 {
9847 ca_chain = ssl->conf->ca_chain;
9848 ca_crl = ssl->conf->ca_crl;
9849 }
9850
9851 if (ca_chain != NULL) {
9852 have_ca_chain_or_callback = 1;
9853 }
9854
9855 ret = mbedtls_x509_crt_verify_restartable(
9856 chain,
9857 ca_chain, ca_crl,
9858 ssl->conf->cert_profile,
9859 ssl->hostname,
9860 &ssl->session_negotiate->verify_result,
9861 f_vrfy, p_vrfy, rs_ctx);
9862 }
9863
9864 if (ret != 0) {
9865 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9866 }
9867
9868#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9869 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9870 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9871 }
9872#endif
9873
9874 /*
9875 * Secondary checks: always done, but change 'ret' only if it was 0
9876 */
9877
9878 /* With TLS 1.2 and ECC certs, check that the curve used by the
9879 * certificate is on our list of acceptable curves.
9880 *
9881 * With TLS 1.3 this is not needed because the curve is part of the
9882 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9883 * we validate the signature made with the key associated to this cert.
9884 */
9885#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9886 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9887 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9888 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9889 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9890 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9891 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9892 if (ret == 0) {
9893 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9894 }
9895 }
9896 }
9897#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9898
9899 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9900 if (mbedtls_ssl_check_cert_usage(chain,
9901 ciphersuite_info,
9902 ssl->conf->endpoint,
9903 ssl->tls_version,
9904 &ssl->session_negotiate->verify_result) != 0) {
9905 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9906 if (ret == 0) {
9907 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9908 }
9909 }
9910
9911 /* With authmode optional, we want to keep going if the certificate was
9912 * unacceptable, but still fail on other errors (out of memory etc),
9913 * including fatal errors from the f_vrfy callback.
9914 *
9915 * The only acceptable errors are:
9916 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9917 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9918 * Anything else is a fatal error. */
9919 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9920 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9921 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9922 ret = 0;
9923 }
9924
9925 /* Return a specific error as this is a user error: inconsistent
9926 * configuration - can't verify without trust anchors. */
9927 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9928 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9929 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9930 }
9931
9932 if (ret != 0) {
9933 uint8_t alert;
9934
9935 /* The certificate may have been rejected for several reasons.
9936 Pick one and send the corresponding alert. Which alert to send
9937 may be a subject of debate in some cases. */
9938 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9939 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9940 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9941 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9942 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9943 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9944 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9945 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9946 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9947 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9948 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9949 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9950 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9951 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9952 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9953 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9954 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9955 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9956 } else {
9957 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9958 }
9959 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9960 alert);
9961 }
9962
9963#if defined(MBEDTLS_DEBUG_C)
9964 if (ssl->session_negotiate->verify_result != 0) {
9965 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9966 (unsigned int) ssl->session_negotiate->verify_result));
9967 } else {
9968 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9969 }
9970#endif /* MBEDTLS_DEBUG_C */
9971
9972 return ret;
9973}
9974#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976#endif /* MBEDTLS_SSL_TLS_C */