blob: 7f742482526608bfb56ff0050d0dd8777b447fdc [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;
1491 ssl->keep_current_message = 0;
1492 ssl->transform_in = NULL;
1493
Gilles Peskinecb72cd22025-02-17 16:36:36 +01001494 /* TLS: reset in_hsfraglen, which is part of message parsing.
1495 * DTLS: on a client reconnect, don't reset badmac_seen. */
1496 if (!partial) {
1497 ssl->badmac_seen_or_in_hsfraglen = 0;
1498 }
1499
Hanno Beckerb0302c42021-08-03 09:39:42 +01001500#if defined(MBEDTLS_SSL_PROTO_DTLS)
1501 ssl->next_record_offset = 0;
1502 ssl->in_epoch = 0;
1503#endif
1504
1505 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001507 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001509 }
1510
Ronald Cronad8c17b2022-06-10 17:18:09 +02001511 ssl->send_alert = 0;
1512
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 /* Reset outgoing message writing */
1514 ssl->out_msgtype = 0;
1515 ssl->out_msglen = 0;
1516 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 memset(ssl->out_buf, 0, out_buf_len);
1518 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001519 ssl->transform_out = NULL;
1520
1521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001523#endif
1524
XiaokangQian2b01dc32022-01-21 02:53:13 +00001525#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ssl->transform) {
1527 mbedtls_ssl_transform_free(ssl->transform);
1528 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001529 ssl->transform = NULL;
1530 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1532
XiaokangQian2b01dc32022-01-21 02:53:13 +00001533#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 mbedtls_ssl_transform_free(ssl->transform_application);
1535 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001536 ssl->transform_application = NULL;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001539#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1541 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001542 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001543#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1546 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001547 ssl->handshake->transform_handshake = NULL;
1548 }
1549
XiaokangQian2b01dc32022-01-21 02:53:13 +00001550#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001551}
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554{
1555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1556
1557 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001558 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001561
1562 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_SSL_RENEGOTIATION)
1564 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001565 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001566
1567 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1569 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001572
Hanno Beckerb0302c42021-08-03 09:39:42 +01001573 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001574 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (ssl->session) {
1576 mbedtls_ssl_session_free(ssl->session);
1577 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001578 ssl->session = NULL;
1579 }
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001582 ssl->alpn_chosen = NULL;
1583#endif
1584
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001585#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001586 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001587#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001589#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (free_cli_id) {
1591 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001592 ssl->cli_id = NULL;
1593 ssl->cli_id_len = 0;
1594 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001595#endif
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if ((ret = ssl_handshake_init(ssl)) != 0) {
1598 return ret;
1599 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001602}
1603
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001604/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001605 * Reset an initialized and used SSL context for re-use while retaining
1606 * all application-set variables, function pointers and data.
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001609{
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001611}
1612
1613/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 * SSL set accessors
1615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001618 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001623 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001624}
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001627void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001629 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001630}
1631#endif
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001635 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001636}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1641 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001642{
1643 ssl->disable_datagram_packing = !allow_packing;
1644}
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1647 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001649 conf->hs_timeout_min = min;
1650 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001651}
1652#endif
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001655{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001656 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657}
1658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001660void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1661 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1662 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001664 conf->f_vrfy = f_vrfy;
1665 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1670 int (*f_rng)(void *, unsigned char *, size_t),
1671 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001673 conf->f_rng = f_rng;
1674 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001675}
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1678 void (*f_dbg)(void *, int, const char *, int, const char *),
1679 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001681 conf->f_dbg = f_dbg;
1682 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1686 void *p_bio,
1687 mbedtls_ssl_send_t *f_send,
1688 mbedtls_ssl_recv_t *f_recv,
1689 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001690{
1691 ssl->p_bio = p_bio;
1692 ssl->f_send = f_send;
1693 ssl->f_recv = f_recv;
1694 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001695}
1696
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001698void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001699{
1700 ssl->mtu = mtu;
1701}
1702#endif
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001705{
1706 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001707}
1708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1710 void *p_timer,
1711 mbedtls_ssl_set_timer_t *f_set_timer,
1712 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001713{
1714 ssl->p_timer = p_timer;
1715 ssl->f_set_timer = f_set_timer;
1716 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001717
1718 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001720}
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001723void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1724 void *p_cache,
1725 mbedtls_ssl_cache_get_t *f_get_cache,
1726 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001727{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001728 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001729 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001730 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001735int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001736{
Janos Follath865b3eb2019-12-16 11:46:15 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001740 session == NULL ||
1741 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001744 }
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (ssl->handshake->resume == 1) {
1747 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1748 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001749
Jerry Yu21092062022-10-10 21:21:31 +08001750#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron8d630842024-04-04 14:05:21 +02001752#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001753 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001757 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1759 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1760 session->ciphersuite));
1761 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001762 }
Ronald Cron8d630842024-04-04 14:05:21 +02001763#else
1764 /*
1765 * If session tickets are not enabled, it is not possible to resume a
1766 * TLS 1.3 session, thus do not make any change to the SSL context in
1767 * the first place.
1768 */
1769 return 0;
1770#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001771 }
Jerry Yu21092062022-10-10 21:21:31 +08001772#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1775 session)) != 0) {
1776 return ret;
1777 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001778
Paul Bakker0a597072012-09-25 21:55:46 +00001779 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1786 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001787{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001788 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001789}
1790
Ronald Cron6f135e12021-12-08 16:57:54 +01001791#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1793 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001794{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001795 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001796}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001797
1798#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001799void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1800 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001801{
1802 conf->early_data_enabled = early_data_enabled;
1803}
Jerry Yucc4e0072022-11-22 17:22:22 +08001804
1805#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001806void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001808{
Jerry Yu39da9852022-12-06 16:58:36 +08001809 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001810}
1811#endif /* MBEDTLS_SSL_SRV_C */
1812
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001813#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001814#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001817void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1818 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001819{
1820 conf->cert_profile = profile;
1821}
1822
Gilles Peskine449bd832023-01-11 14:50:10 +01001823static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001824{
1825 mbedtls_ssl_key_cert *cur = key_cert, *next;
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001828 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001830 cur = next;
1831 }
1832}
1833
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001834/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001835MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001836static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1837 mbedtls_x509_crt *cert,
1838 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001839{
niisato8ee24222018-06-25 19:05:48 +09001840 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001843 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001845 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001847 }
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1850 if (new_cert == NULL) {
1851 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1852 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001853
niisato8ee24222018-06-25 19:05:48 +09001854 new_cert->cert = cert;
1855 new_cert->key = key;
1856 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001857
Glenn Strauss36872db2022-01-22 05:06:31 -05001858 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001860 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001862 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001864 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 }
niisato8ee24222018-06-25 19:05:48 +09001866 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001867 }
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001870}
1871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001873 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001875{
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001877}
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001880 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001882{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001883 conf->ca_chain = ca_chain;
1884 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001885
1886#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1887 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1888 * cannot be used together. */
1889 conf->f_ca_cb = NULL;
1890 conf->p_ca_cb = NULL;
1891#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001892}
Hanno Becker5adaad92019-03-27 16:54:37 +00001893
1894#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001895void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1896 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1897 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001898{
1899 conf->f_ca_cb = f_ca_cb;
1900 conf->p_ca_cb = p_ca_cb;
1901
1902 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1903 * cannot be used together. */
1904 conf->ca_chain = NULL;
1905 conf->ca_crl = NULL;
1906}
1907#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001909
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001910#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001911const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1912 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001913{
1914 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001916}
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1919 mbedtls_x509_crt *own_cert,
1920 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001921{
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1923 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001924}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1927 mbedtls_x509_crt *ca_chain,
1928 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001929{
1930 ssl->handshake->sni_ca_chain = ca_chain;
1931 ssl->handshake->sni_ca_crl = ca_crl;
1932}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001933
Glenn Strauss999ef702022-03-11 01:37:23 -05001934#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001935void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1936 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001937{
1938 ssl->handshake->dn_hints = crt;
1939}
1940#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1943 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001944{
1945 ssl->handshake->sni_authmode = authmode;
1946}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001947#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1948
Hanno Becker8927c832019-04-03 12:52:50 +01001949#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001950void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1951 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1952 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001953{
1954 ssl->f_vrfy = f_vrfy;
1955 ssl->p_vrfy = p_vrfy;
1956}
1957#endif
1958
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001959#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001960
Valerio Setti016f6822022-12-09 14:17:50 +01001961#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001962static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1963static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1964
Valerio Setti016f6822022-12-09 14:17:50 +01001965static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 mbedtls_ssl_context *ssl,
1967 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001968{
1969 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001970 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001971 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001972 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001973 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001974 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1976 psa_pake_cs_set_primitive(&cipher_suite,
1977 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1978 PSA_ECC_FAMILY_SECP_R1,
1979 256));
1980 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1983 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001984 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001988 user = jpake_server_id;
1989 user_len = sizeof(jpake_server_id);
1990 peer = jpake_client_id;
1991 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001993 user = jpake_client_id;
1994 user_len = sizeof(jpake_client_id);
1995 peer = jpake_server_id;
1996 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001998
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001999 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2000 if (status != PSA_SUCCESS) {
2001 return status;
2002 }
2003
2004 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 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
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2010 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002011 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 }
Valerio Setti016f6822022-12-09 14:17:50 +01002013
2014 ssl->handshake->psa_pake_ctx_is_ok = 1;
2015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002017}
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2020 const unsigned char *pw,
2021 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002022{
2023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2024 psa_status_t status;
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (ssl->handshake == NULL || ssl->conf == NULL) {
2027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002028 }
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 /* Empty password is not valid */
2031 if ((pw == NULL) || (pw_len == 0)) {
2032 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2033 }
2034
2035 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2036 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2037 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2038
2039 status = psa_import_key(&attributes, pw, pw_len,
2040 &ssl->handshake->psa_pake_password);
2041 if (status != PSA_SUCCESS) {
2042 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2043 }
2044
2045 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2046 ssl->handshake->psa_pake_password);
2047 if (status != PSA_SUCCESS) {
2048 psa_destroy_key(ssl->handshake->psa_pake_password);
2049 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2050 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2051 }
2052
2053 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002054}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2057 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002058{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002059 psa_status_t status;
2060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (ssl->handshake == NULL || ssl->conf == NULL) {
2062 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002063 }
2064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (mbedtls_svc_key_id_is_null(pwd)) {
2066 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2067 }
2068
2069 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2070 if (status != PSA_SUCCESS) {
2071 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2072 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2073 }
2074
2075 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002076}
2077#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2079 const unsigned char *pw,
2080 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002081{
2082 mbedtls_ecjpake_role role;
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (ssl->handshake == NULL || ssl->conf == NULL) {
2085 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2086 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002087
Valerio Settic689ed82022-12-07 14:40:38 +01002088 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if ((pw == NULL) || (pw_len == 0)) {
2090 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2091 }
Valerio Settic689ed82022-12-07 14:40:38 +01002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002094 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002096 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2100 role,
2101 MBEDTLS_MD_SHA256,
2102 MBEDTLS_ECP_DP_SECP256R1,
2103 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002104}
Valerio Setti02c25b52022-11-15 14:08:42 +01002105#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002106#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002107
Ronald Cron73fe8df2022-10-05 14:31:43 +02002108#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002109int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002110{
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (conf->psk_identity == NULL ||
2112 conf->psk_identity_len == 0) {
2113 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002114 }
2115
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002116#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2118 return 1;
2119 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002120#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (conf->psk != NULL && conf->psk_len != 0) {
2123 return 1;
2124 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002127}
2128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002130{
2131 /* Remove reference to existing PSK, if any. */
2132#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134 /* The maintenance of the PSK key slot is the
2135 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002136 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002137 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002138#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002140 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002141 conf->psk = NULL;
2142 conf->psk_len = 0;
2143 }
2144
2145 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (conf->psk_identity != NULL) {
2147 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002148 conf->psk_identity = NULL;
2149 conf->psk_identity_len = 0;
2150 }
2151}
2152
Hanno Becker7390c712018-11-15 13:33:04 +00002153/* This function assumes that PSK identity in the SSL config is unset.
2154 * It checks that the provided identity is well-formed and attempts
2155 * to make a copy of it in the SSL config.
2156 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002157MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002158static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2159 unsigned char const *psk_identity,
2160 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002161{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002162 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002164 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 (psk_identity_len >> 16) != 0 ||
2166 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2167 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002168 }
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2171 if (conf->psk_identity == NULL) {
2172 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2173 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002174
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002175 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002179}
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2182 const unsigned char *psk, size_t psk_len,
2183 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002184{
Janos Follath865b3eb2019-12-16 11:46:15 +00002185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002186
2187 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2189 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2190 }
Hanno Becker7390c712018-11-15 13:33:04 +00002191
2192 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (psk == NULL) {
2194 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2195 }
2196 if (psk_len == 0) {
2197 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2198 }
2199 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2200 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2201 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2204 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2205 }
Hanno Becker7390c712018-11-15 13:33:04 +00002206 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002208
2209 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2211 if (ret != 0) {
2212 ssl_conf_remove_psk(conf);
2213 }
Hanno Becker7390c712018-11-15 13:33:04 +00002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002216}
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002219{
2220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002222 /* The maintenance of the external PSK key slot is the
2223 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (ssl->handshake->psk_opaque_is_internal) {
2225 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002226 ssl->handshake->psk_opaque_is_internal = 0;
2227 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002228 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002229 }
Neil Armstronge952a302022-05-03 10:22:14 +02002230#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002232 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002234 ssl->handshake->psk_len = 0;
lhuang046d4d94f2024-06-11 12:37:02 -07002235 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002236 }
Neil Armstronge952a302022-05-03 10:22:14 +02002237#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002238}
2239
Gilles Peskine449bd832023-01-11 14:50:10 +01002240int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2241 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002242{
Neil Armstrong501c9322022-05-03 09:35:09 +02002243#if defined(MBEDTLS_USE_PSA_CRYPTO)
2244 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002246 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002248#endif /* MBEDTLS_USE_PSA_CRYPTO */
2249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if (psk == NULL || ssl->handshake == NULL) {
2251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2252 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2255 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2256 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002259
Neil Armstrong501c9322022-05-03 09:35:09 +02002260#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002261#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2263 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2264 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2265 } else {
2266 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2267 }
2268 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002269 }
2270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002271
Jerry Yu568ec252022-07-22 21:27:34 +08002272#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2274 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2275 psa_set_key_usage_flags(&key_attributes,
2276 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002277 }
2278#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 psa_set_key_algorithm(&key_attributes, alg);
2281 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2284 if (status != PSA_SUCCESS) {
2285 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2286 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002287
2288 /* Allow calling psa_destroy_key() on psk remove */
2289 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002291#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2293 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2294 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002295
2296 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002300#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002301}
2302
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002304int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2305 mbedtls_svc_key_id_t psk,
2306 const unsigned char *psk_identity,
2307 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002308{
Janos Follath865b3eb2019-12-16 11:46:15 +00002309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002310
2311 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2313 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2314 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002315
Hanno Becker7390c712018-11-15 13:33:04 +00002316 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (mbedtls_svc_key_id_is_null(psk)) {
2318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2319 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002320 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002321
2322 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2324 psk_identity_len);
2325 if (ret != 0) {
2326 ssl_conf_remove_psk(conf);
2327 }
Hanno Becker7390c712018-11-15 13:33:04 +00002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002330}
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2333 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334{
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if ((mbedtls_svc_key_id_is_null(psk)) ||
2336 (ssl->handshake == NULL)) {
2337 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2338 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002341 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002343}
2344#endif /* MBEDTLS_USE_PSA_CRYPTO */
2345
Jerry Yu8897c072022-08-12 13:56:53 +08002346#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002347void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2348 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2349 size_t),
2350 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002351{
2352 conf->f_psk = f_psk;
2353 conf->p_psk = p_psk;
2354}
Jerry Yu8897c072022-08-12 13:56:53 +08002355#endif /* MBEDTLS_SSL_SRV_C */
2356
Ronald Cron73fe8df2022-10-05 14:31:43 +02002357#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002358
2359#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002360static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002362{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002363#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (alg == PSA_ALG_CBC_NO_PADDING) {
2365 return MBEDTLS_SSL_MODE_CBC;
2366 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002367#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 if (PSA_ALG_IS_AEAD(alg)) {
2369 return MBEDTLS_SSL_MODE_AEAD;
2370 }
2371 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002372}
2373
2374#else /* MBEDTLS_USE_PSA_CRYPTO */
2375
2376static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002378{
2379#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (mode == MBEDTLS_MODE_CBC) {
2381 return MBEDTLS_SSL_MODE_CBC;
2382 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002383#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2384
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002385#if defined(MBEDTLS_GCM_C) || \
2386 defined(MBEDTLS_CCM_C) || \
2387 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002389 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mode == MBEDTLS_MODE_CHACHAPOLY) {
2391 return MBEDTLS_SSL_MODE_AEAD;
2392 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002393#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002396}
Gilles Peskine301711e2022-04-26 16:57:05 +02002397#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002398
Gilles Peskinee108d982022-04-26 16:50:40 +02002399static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2400 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002402{
2403#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2405 base_mode == MBEDTLS_SSL_MODE_CBC) {
2406 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002407 }
2408#else
2409 (void) encrypt_then_mac;
2410#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002412}
2413
Neil Armstrongab555e02022-04-04 11:07:59 +02002414mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002416{
Gilles Peskinee108d982022-04-26 16:50:40 +02002417 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002418#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002420#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002422#endif
2423 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002424
Gilles Peskinee108d982022-04-26 16:50:40 +02002425 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002427 encrypt_then_mac = transform->encrypt_then_mac;
2428#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002430}
2431
Neil Armstrongab555e02022-04-04 11:07:59 +02002432mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002433#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002435#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002437{
Gilles Peskinee108d982022-04-26 16:50:40 +02002438 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2439
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002440#if defined(MBEDTLS_USE_PSA_CRYPTO)
2441 psa_status_t status;
2442 psa_algorithm_t alg;
2443 psa_key_type_t type;
2444 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002445 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2446 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (status == PSA_SUCCESS) {
2448 base_mode = mbedtls_ssl_get_base_mode(alg);
2449 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002450#else
2451 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002452 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002454 base_mode =
2455 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002457 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002458#endif /* MBEDTLS_USE_PSA_CRYPTO */
2459
Gilles Peskinee108d982022-04-26 16:50:40 +02002460#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2461 int encrypt_then_mac = 0;
2462#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002464}
2465
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002466#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2469 size_t taglen,
2470 psa_algorithm_t *alg,
2471 psa_key_type_t *key_type,
2472 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002473{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002474#if !defined(MBEDTLS_SSL_HAVE_CCM)
2475 (void) taglen;
2476#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002478#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002479 case MBEDTLS_CIPHER_AES_128_CBC:
2480 *alg = PSA_ALG_CBC_NO_PADDING;
2481 *key_type = PSA_KEY_TYPE_AES;
2482 *key_size = 128;
2483 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002484#endif
2485#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002486 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002488 *key_type = PSA_KEY_TYPE_AES;
2489 *key_size = 128;
2490 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002491#endif
2492#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002493 case MBEDTLS_CIPHER_AES_128_GCM:
2494 *alg = PSA_ALG_GCM;
2495 *key_type = PSA_KEY_TYPE_AES;
2496 *key_size = 128;
2497 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002498#endif
2499#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002500 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002502 *key_type = PSA_KEY_TYPE_AES;
2503 *key_size = 192;
2504 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002505#endif
2506#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002507 case MBEDTLS_CIPHER_AES_192_GCM:
2508 *alg = PSA_ALG_GCM;
2509 *key_type = PSA_KEY_TYPE_AES;
2510 *key_size = 192;
2511 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002512#endif
2513#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002514 case MBEDTLS_CIPHER_AES_256_CBC:
2515 *alg = PSA_ALG_CBC_NO_PADDING;
2516 *key_type = PSA_KEY_TYPE_AES;
2517 *key_size = 256;
2518 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002519#endif
2520#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002521 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002523 *key_type = PSA_KEY_TYPE_AES;
2524 *key_size = 256;
2525 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002526#endif
2527#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002528 case MBEDTLS_CIPHER_AES_256_GCM:
2529 *alg = PSA_ALG_GCM;
2530 *key_type = PSA_KEY_TYPE_AES;
2531 *key_size = 256;
2532 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002533#endif
2534#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002535 case MBEDTLS_CIPHER_ARIA_128_CBC:
2536 *alg = PSA_ALG_CBC_NO_PADDING;
2537 *key_type = PSA_KEY_TYPE_ARIA;
2538 *key_size = 128;
2539 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002540#endif
2541#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002542 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002544 *key_type = PSA_KEY_TYPE_ARIA;
2545 *key_size = 128;
2546 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002547#endif
2548#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002549 case MBEDTLS_CIPHER_ARIA_128_GCM:
2550 *alg = PSA_ALG_GCM;
2551 *key_type = PSA_KEY_TYPE_ARIA;
2552 *key_size = 128;
2553 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002554#endif
2555#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002556 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002558 *key_type = PSA_KEY_TYPE_ARIA;
2559 *key_size = 192;
2560 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002561#endif
2562#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002563 case MBEDTLS_CIPHER_ARIA_192_GCM:
2564 *alg = PSA_ALG_GCM;
2565 *key_type = PSA_KEY_TYPE_ARIA;
2566 *key_size = 192;
2567 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002568#endif
2569#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002570 case MBEDTLS_CIPHER_ARIA_256_CBC:
2571 *alg = PSA_ALG_CBC_NO_PADDING;
2572 *key_type = PSA_KEY_TYPE_ARIA;
2573 *key_size = 256;
2574 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002575#endif
2576#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002577 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002579 *key_type = PSA_KEY_TYPE_ARIA;
2580 *key_size = 256;
2581 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002582#endif
2583#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002584 case MBEDTLS_CIPHER_ARIA_256_GCM:
2585 *alg = PSA_ALG_GCM;
2586 *key_type = PSA_KEY_TYPE_ARIA;
2587 *key_size = 256;
2588 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002589#endif
2590#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002591 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2592 *alg = PSA_ALG_CBC_NO_PADDING;
2593 *key_type = PSA_KEY_TYPE_CAMELLIA;
2594 *key_size = 128;
2595 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002596#endif
2597#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002598 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002600 *key_type = PSA_KEY_TYPE_CAMELLIA;
2601 *key_size = 128;
2602 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002603#endif
2604#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002605 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2606 *alg = PSA_ALG_GCM;
2607 *key_type = PSA_KEY_TYPE_CAMELLIA;
2608 *key_size = 128;
2609 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002610#endif
2611#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002612 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002614 *key_type = PSA_KEY_TYPE_CAMELLIA;
2615 *key_size = 192;
2616 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002617#endif
2618#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002619 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2620 *alg = PSA_ALG_GCM;
2621 *key_type = PSA_KEY_TYPE_CAMELLIA;
2622 *key_size = 192;
2623 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002624#endif
2625#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002626 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2627 *alg = PSA_ALG_CBC_NO_PADDING;
2628 *key_type = PSA_KEY_TYPE_CAMELLIA;
2629 *key_size = 256;
2630 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002631#endif
2632#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002633 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002635 *key_type = PSA_KEY_TYPE_CAMELLIA;
2636 *key_size = 256;
2637 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002638#endif
2639#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002640 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2641 *alg = PSA_ALG_GCM;
2642 *key_type = PSA_KEY_TYPE_CAMELLIA;
2643 *key_size = 256;
2644 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002645#endif
2646#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002647 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2648 *alg = PSA_ALG_CHACHA20_POLY1305;
2649 *key_type = PSA_KEY_TYPE_CHACHA20;
2650 *key_size = 256;
2651 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002652#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002653 case MBEDTLS_CIPHER_NULL:
2654 *alg = MBEDTLS_SSL_NULL_CIPHER;
2655 *key_type = 0;
2656 *key_size = 0;
2657 break;
2658 default:
2659 return PSA_ERROR_NOT_SUPPORTED;
2660 }
2661
2662 return PSA_SUCCESS;
2663}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002664#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002665
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002666#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002667int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2668 const unsigned char *dhm_P, size_t P_len,
2669 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002670{
Janos Follath865b3eb2019-12-16 11:46:15 +00002671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 mbedtls_mpi_free(&conf->dhm_P);
2674 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2677 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2678 mbedtls_mpi_free(&conf->dhm_P);
2679 mbedtls_mpi_free(&conf->dhm_G);
2680 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002681 }
2682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002684}
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002687{
Janos Follath865b3eb2019-12-16 11:46:15 +00002688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 mbedtls_mpi_free(&conf->dhm_P);
2691 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2694 &conf->dhm_P)) != 0 ||
2695 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2696 &conf->dhm_G)) != 0) {
2697 mbedtls_mpi_free(&conf->dhm_P);
2698 mbedtls_mpi_free(&conf->dhm_G);
2699 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002700 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002703}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002704#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002705
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002706#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2707/*
2708 * Set the minimum length for Diffie-Hellman parameters
2709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2711 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002712{
2713 conf->dhm_min_bitlen = bitlen;
2714}
2715#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2716
Ronald Crone68ab4f2022-10-05 12:46:29 +02002717#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002718#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002719/*
2720 * Set allowed/preferred hashes for handshake signatures
2721 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002722void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2723 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002724{
2725 conf->sig_hashes = hashes;
2726}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002727#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002728
Jerry Yuf017ee42022-01-12 15:49:48 +08002729/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2731 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002732{
Jerry Yuf017ee42022-01-12 15:49:48 +08002733#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2734 conf->sig_hashes = NULL;
2735#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2736 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002737}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002738#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002739
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002740#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002741#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002742/*
2743 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002744 *
2745 * mbedtls_ssl_setup() takes the provided list
2746 * and translates it to a list of IANA TLS group identifiers,
2747 * stored in ssl->handshake->group_list.
2748 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002750void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2751 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002752{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002753 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002754 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002755}
Brett Warrene0edc842021-08-17 09:53:13 +01002756#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002757#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002758
Brett Warrene0edc842021-08-17 09:53:13 +01002759/*
2760 * Set the allowed groups
2761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002762void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2763 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002764{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002765#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002766 conf->curve_list = NULL;
2767#endif
2768 conf->group_list = group_list;
2769}
2770
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002771#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002772int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002773{
Hanno Becker947194e2017-04-07 13:25:49 +01002774 /* Initialize to suppress unnecessary compiler warning */
2775 size_t hostname_len = 0;
2776
2777 /* Check if new hostname is valid before
2778 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 if (hostname != NULL) {
2780 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2783 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2784 }
Hanno Becker947194e2017-04-07 13:25:49 +01002785 }
2786
2787 /* Now it's clear that we will overwrite the old hostname,
2788 * so we can free it safely */
2789
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002791 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002792 }
2793
2794 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002797 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 } else {
2799 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2800 if (ssl->hostname == NULL) {
2801 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2802 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002805
Hanno Becker947194e2017-04-07 13:25:49 +01002806 ssl->hostname[hostname_len] = '\0';
2807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002810}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002811#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002813#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002814void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2815 int (*f_sni)(void *, mbedtls_ssl_context *,
2816 const unsigned char *, size_t),
2817 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002818{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002819 conf->f_sni = f_sni;
2820 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002825int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002826{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002827 size_t cur_len, tot_len;
2828 const char **p;
2829
2830 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002831 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2832 * MUST NOT be truncated."
2833 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002834 */
2835 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 for (p = protos; *p != NULL; p++) {
2837 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002838 tot_len += cur_len;
2839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 if ((cur_len == 0) ||
2841 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2842 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2844 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002845 }
2846
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002847 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002848
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002850}
2851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002853{
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002857
Johan Pascalb62bb512015-12-03 21:56:45 +01002858#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002859void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2860 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002861{
2862 conf->dtls_srtp_mki_support = support_mki_value;
2863}
2864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2866 unsigned char *mki_value,
2867 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002868{
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2870 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002871 }
Ron Eldor591f1622018-01-22 12:30:04 +02002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2874 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002875 }
Ron Eldor591f1622018-01-22 12:30:04 +02002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002878 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002880}
2881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2883 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002884{
Johan Pascal253d0262020-09-22 13:04:45 +02002885 const mbedtls_ssl_srtp_profile *p;
2886 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002887
Johan Pascal253d0262020-09-22 13:04:45 +02002888 /* check the profiles list: all entry must be valid,
2889 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2891 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2892 p++) {
2893 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002894 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002896 /* unsupported value, stop parsing and set the size to an error value */
2897 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002898 }
2899 }
2900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2902 conf->dtls_srtp_profile_list = NULL;
2903 conf->dtls_srtp_profile_list_len = 0;
2904 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002905 }
2906
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002907 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002908 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002911}
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2914 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002915{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002916 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2917 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002919 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002921 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2923 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002924 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002925}
Johan Pascalb62bb512015-12-03 21:56:45 +01002926#endif /* MBEDTLS_SSL_DTLS_SRTP */
2927
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302928#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002929void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002930{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002931 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002932}
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002935{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002936 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002937}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302938#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002939
Janos Follath088ce432017-04-10 12:42:31 +01002940#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002941void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2942 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002943{
2944 conf->cert_req_ca_list = cert_req_ca_list;
2945}
2946#endif
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002949void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002951 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002952}
2953#endif
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002956void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002958 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002959}
2960#endif
2961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002963int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002964{
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2966 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2967 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002968 }
2969
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002970 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002973}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002977{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002978 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002979}
2980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002982void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002984 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002985}
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002989 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002990}
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2993 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002994{
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002996}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003000#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003003{
Ronald Crond67f8012024-08-28 07:45:57 +02003004 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3005 conf->session_tickets |= (use_tickets != 0) <<
3006 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003007}
Ronald Crond67f8012024-08-28 07:45:57 +02003008
Ronald Cronbedddd72024-08-27 14:18:50 +02003009#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003010void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3011 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003012{
Ronald Crond67f8012024-08-28 07:45:57 +02003013 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003014 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003015 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3016}
Ronald Cronbedddd72024-08-27 14:18:50 +02003017#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3018#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003019
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003020#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003021
Jerry Yud0766ec2022-09-22 10:46:57 +08003022#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3024 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003025{
Jerry Yud0766ec2022-09-22 10:46:57 +08003026 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003027}
3028#endif
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3031 mbedtls_ssl_ticket_write_t *f_ticket_write,
3032 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3033 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003034{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003035 conf->f_ticket_write = f_ticket_write;
3036 conf->f_ticket_parse = f_ticket_parse;
3037 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003038}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3043 mbedtls_ssl_export_keys_t *f_export_keys,
3044 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003045{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003046 ssl->f_export_keys = f_export_keys;
3047 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003048}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003049
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003050#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003051void mbedtls_ssl_conf_async_private_cb(
3052 mbedtls_ssl_config *conf,
3053 mbedtls_ssl_async_sign_t *f_async_sign,
3054 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3055 mbedtls_ssl_async_resume_t *f_async_resume,
3056 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003058{
3059 conf->f_async_sign_start = f_async_sign;
3060 conf->f_async_decrypt_start = f_async_decrypt;
3061 conf->f_async_resume = f_async_resume;
3062 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003063 conf->p_async_config_data = async_config_data;
3064}
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003067{
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003069}
3070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003072{
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (ssl->handshake == NULL) {
3074 return NULL;
3075 } else {
3076 return ssl->handshake->user_async_ctx;
3077 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3081 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003082{
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003084 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003086}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003087#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089/*
3090 * SSL get accessors
3091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003093{
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (ssl->session != NULL) {
3095 return ssl->session->verify_result;
3096 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ssl->session_negotiate != NULL) {
3099 return ssl->session_negotiate->verify_result;
3100 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103}
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003106{
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (ssl == NULL || ssl->session == NULL) {
3108 return 0;
3109 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003112}
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003115{
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ssl == NULL || ssl->session == NULL) {
3117 return NULL;
3118 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003121}
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3127 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003128 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003130 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003132 }
3133 }
3134#endif
3135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003137 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003139 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003143 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003144}
3145
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003146#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3147
3148size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3149{
3150 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3151 size_t record_size_limit = max_len;
3152
3153 if (ssl->session != NULL &&
3154 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3155 ssl->session->record_size_limit < max_len) {
3156 record_size_limit = ssl->session->record_size_limit;
3157 }
3158
3159 // TODO: this is currently untested
3160 /* During a handshake, use the value being negotiated */
3161 if (ssl->session_negotiate != NULL &&
3162 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3163 ssl->session_negotiate->record_size_limit < max_len) {
3164 record_size_limit = ssl->session_negotiate->record_size_limit;
3165 }
3166
3167 return record_size_limit;
3168}
3169#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3170
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003171#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003172size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003173{
David Horstmann95d516f2021-05-04 18:36:56 +01003174 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003175 size_t read_mfl;
3176
Jerry Yuddda0502022-12-01 19:43:12 +08003177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003178 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3180 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3181 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003182 }
Jerry Yuddda0502022-12-01 19:43:12 +08003183#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003184
3185 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if (ssl->session_out != NULL) {
3187 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3188 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003189 max_len = read_mfl;
3190 }
3191 }
3192
Jerry Yuddda0502022-12-01 19:43:12 +08003193 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 if (ssl->session_negotiate != NULL) {
3195 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3196 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003197 max_len = read_mfl;
3198 }
3199 }
3200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003205{
3206 size_t max_len;
3207
3208 /*
3209 * Assume mfl_code is correct since it was checked when set
3210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003212
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003213 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (ssl->session_out != NULL &&
3215 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3216 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003217 }
3218
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003219 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 if (ssl->session_negotiate != NULL &&
3221 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3222 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003223 }
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003226}
3227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3228
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003230size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003231{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003232 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3234 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3235 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3236 return 0;
3237 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3240 return ssl->mtu;
3241 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (ssl->mtu == 0) {
3244 return ssl->handshake->mtu;
3245 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return ssl->mtu < ssl->handshake->mtu ?
3248 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003249}
3250#endif /* MBEDTLS_SSL_PROTO_DTLS */
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003253{
3254 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3255
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003256#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003257 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003258 !defined(MBEDTLS_SSL_PROTO_DTLS)
3259 (void) ssl;
3260#endif
3261
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003262#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003266 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003268#endif
3269
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003270#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3271 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3272
3273 if (max_len > record_size_limit) {
3274 max_len = record_size_limit;
3275 }
3276#endif
3277
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003278 if (ssl->transform_out != NULL &&
3279 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003280 /*
3281 * In TLS 1.3 case, when records are protected, `max_len` as computed
3282 * above is the maximum length of the TLSInnerPlaintext structure that
3283 * along the plaintext payload contains the inner content type (one byte)
3284 * and some zero padding. Given the algorithm used for padding
3285 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3286 * the plaintext payload. Round down to a multiple of
3287 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3288 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003289 */
3290 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3291 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3292 }
3293
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3296 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3297 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003298 const size_t overhead = (size_t) ret;
3299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 if (ret < 0) {
3301 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302 }
3303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 if (mtu <= overhead) {
3305 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3307 }
3308
3309 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003310 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003312 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003313#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314
Hanno Becker0defedb2018-08-10 12:35:02 +01003315#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003316 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3317 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003318 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319#endif
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322}
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003325{
3326 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3327
3328#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3329 (void) ssl;
3330#endif
3331
3332#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003336 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003338#endif
3339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003341}
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003344const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003345{
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl == NULL || ssl->session == NULL) {
3347 return NULL;
3348 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003349
Hanno Beckere6824572019-02-07 13:18:46 +00003350#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003352#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003354#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003359int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3360 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003361{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003362 int ret;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003365 dst == NULL ||
3366 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003369 }
3370
Hanno Beckere810bbc2021-05-14 16:01:05 +01003371 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3372 * idempotent: Each session can only be exported once.
3373 *
3374 * (This is in preparation for TLS 1.3 support where we will
3375 * need the ability to export multiple sessions (aka tickets),
3376 * which will be achieved by calling mbedtls_ssl_get_session()
3377 * multiple times until it fails.)
3378 *
3379 * Check whether we have already exported the current session,
3380 * and fail if so.
3381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (ssl->session->exported == 1) {
3383 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3384 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3387 if (ret != 0) {
3388 return ret;
3389 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003390
3391 /* Remember that we've exported the session. */
3392 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003396
David Horstmanncb01b362024-02-29 17:31:46 +00003397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3398
3399/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003400 *
David Horstmanncb01b362024-02-29 17:31:46 +00003401 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003402 */
3403static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3404 unsigned char *buf,
3405 size_t buf_len)
3406{
3407 unsigned char *p = buf;
3408 size_t used = 0;
3409
3410#if defined(MBEDTLS_HAVE_TIME)
3411 uint64_t start;
3412#endif
3413#if defined(MBEDTLS_X509_CRT_PARSE_C)
3414#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3415 size_t cert_len;
3416#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3417#endif /* MBEDTLS_X509_CRT_PARSE_C */
3418
3419 /*
3420 * Time
3421 */
3422#if defined(MBEDTLS_HAVE_TIME)
3423 used += 8;
3424
3425 if (used <= buf_len) {
3426 start = (uint64_t) session->start;
3427
3428 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3429 p += 8;
3430 }
3431#endif /* MBEDTLS_HAVE_TIME */
3432
3433 /*
3434 * Basic mandatory fields
3435 */
3436 used += 1 /* id_len */
3437 + sizeof(session->id)
3438 + sizeof(session->master)
3439 + 4; /* verify_result */
3440
3441 if (used <= buf_len) {
3442 *p++ = MBEDTLS_BYTE_0(session->id_len);
3443 memcpy(p, session->id, 32);
3444 p += 32;
3445
3446 memcpy(p, session->master, 48);
3447 p += 48;
3448
3449 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3450 p += 4;
3451 }
3452
3453 /*
3454 * Peer's end-entity certificate
3455 */
3456#if defined(MBEDTLS_X509_CRT_PARSE_C)
3457#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3458 if (session->peer_cert == NULL) {
3459 cert_len = 0;
3460 } else {
3461 cert_len = session->peer_cert->raw.len;
3462 }
3463
3464 used += 3 + cert_len;
3465
3466 if (used <= buf_len) {
3467 *p++ = MBEDTLS_BYTE_2(cert_len);
3468 *p++ = MBEDTLS_BYTE_1(cert_len);
3469 *p++ = MBEDTLS_BYTE_0(cert_len);
3470
3471 if (session->peer_cert != NULL) {
3472 memcpy(p, session->peer_cert->raw.p, cert_len);
3473 p += cert_len;
3474 }
3475 }
3476#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3477 if (session->peer_cert_digest != NULL) {
3478 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3479 if (used <= buf_len) {
3480 *p++ = (unsigned char) session->peer_cert_digest_type;
3481 *p++ = (unsigned char) session->peer_cert_digest_len;
3482 memcpy(p, session->peer_cert_digest,
3483 session->peer_cert_digest_len);
3484 p += session->peer_cert_digest_len;
3485 }
3486 } else {
3487 used += 2;
3488 if (used <= buf_len) {
3489 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3490 *p++ = 0;
3491 }
3492 }
3493#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3494#endif /* MBEDTLS_X509_CRT_PARSE_C */
3495
3496 /*
3497 * Session ticket if any, plus associated data
3498 */
3499#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3500#if defined(MBEDTLS_SSL_CLI_C)
3501 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3502 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3503
3504 if (used <= buf_len) {
3505 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3506 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3507 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3508
3509 if (session->ticket != NULL) {
3510 memcpy(p, session->ticket, session->ticket_len);
3511 p += session->ticket_len;
3512 }
3513
3514 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3515 p += 4;
3516 }
3517 }
3518#endif /* MBEDTLS_SSL_CLI_C */
3519#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3520 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3521 used += 8;
3522
3523 if (used <= buf_len) {
3524 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3525 p += 8;
3526 }
3527 }
3528#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3529#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3530
3531 /*
3532 * Misc extension-related info
3533 */
3534#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3535 used += 1;
3536
3537 if (used <= buf_len) {
3538 *p++ = session->mfl_code;
3539 }
3540#endif
3541
3542#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3543 used += 1;
3544
3545 if (used <= buf_len) {
3546 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3547 }
3548#endif
3549
3550 return used;
3551}
3552
3553MBEDTLS_CHECK_RETURN_CRITICAL
3554static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3555 const unsigned char *buf,
3556 size_t len)
3557{
3558#if defined(MBEDTLS_HAVE_TIME)
3559 uint64_t start;
3560#endif
3561#if defined(MBEDTLS_X509_CRT_PARSE_C)
3562#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3563 size_t cert_len;
3564#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3565#endif /* MBEDTLS_X509_CRT_PARSE_C */
3566
3567 const unsigned char *p = buf;
3568 const unsigned char * const end = buf + len;
3569
3570 /*
3571 * Time
3572 */
3573#if defined(MBEDTLS_HAVE_TIME)
3574 if (8 > (size_t) (end - p)) {
3575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3576 }
3577
3578 start = MBEDTLS_GET_UINT64_BE(p, 0);
3579 p += 8;
3580
3581 session->start = (time_t) start;
3582#endif /* MBEDTLS_HAVE_TIME */
3583
3584 /*
3585 * Basic mandatory fields
3586 */
3587 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3589 }
3590
3591 session->id_len = *p++;
3592 memcpy(session->id, p, 32);
3593 p += 32;
3594
3595 memcpy(session->master, p, 48);
3596 p += 48;
3597
3598 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3599 p += 4;
3600
3601 /* Immediately clear invalid pointer values that have been read, in case
3602 * we exit early before we replaced them with valid ones. */
3603#if defined(MBEDTLS_X509_CRT_PARSE_C)
3604#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3605 session->peer_cert = NULL;
3606#else
3607 session->peer_cert_digest = NULL;
3608#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3609#endif /* MBEDTLS_X509_CRT_PARSE_C */
3610#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3611 session->ticket = NULL;
3612#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3613
3614 /*
3615 * Peer certificate
3616 */
3617#if defined(MBEDTLS_X509_CRT_PARSE_C)
3618#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3619 /* Deserialize CRT from the end of the ticket. */
3620 if (3 > (size_t) (end - p)) {
3621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3622 }
3623
3624 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3625 p += 3;
3626
3627 if (cert_len != 0) {
3628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3629
3630 if (cert_len > (size_t) (end - p)) {
3631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3632 }
3633
3634 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3635
3636 if (session->peer_cert == NULL) {
3637 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3638 }
3639
3640 mbedtls_x509_crt_init(session->peer_cert);
3641
3642 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3643 p, cert_len)) != 0) {
3644 mbedtls_x509_crt_free(session->peer_cert);
3645 mbedtls_free(session->peer_cert);
3646 session->peer_cert = NULL;
3647 return ret;
3648 }
3649
3650 p += cert_len;
3651 }
3652#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3653 /* Deserialize CRT digest from the end of the ticket. */
3654 if (2 > (size_t) (end - p)) {
3655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3656 }
3657
3658 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3659 session->peer_cert_digest_len = (size_t) *p++;
3660
3661 if (session->peer_cert_digest_len != 0) {
3662 const mbedtls_md_info_t *md_info =
3663 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3664 if (md_info == NULL) {
3665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3666 }
3667 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3669 }
3670
3671 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3673 }
3674
3675 session->peer_cert_digest =
3676 mbedtls_calloc(1, session->peer_cert_digest_len);
3677 if (session->peer_cert_digest == NULL) {
3678 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3679 }
3680
3681 memcpy(session->peer_cert_digest, p,
3682 session->peer_cert_digest_len);
3683 p += session->peer_cert_digest_len;
3684 }
3685#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3686#endif /* MBEDTLS_X509_CRT_PARSE_C */
3687
3688 /*
3689 * Session ticket and associated data
3690 */
3691#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3692#if defined(MBEDTLS_SSL_CLI_C)
3693 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3694 if (3 > (size_t) (end - p)) {
3695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3696 }
3697
3698 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3699 p += 3;
3700
3701 if (session->ticket_len != 0) {
3702 if (session->ticket_len > (size_t) (end - p)) {
3703 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3704 }
3705
3706 session->ticket = mbedtls_calloc(1, session->ticket_len);
3707 if (session->ticket == NULL) {
3708 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3709 }
3710
3711 memcpy(session->ticket, p, session->ticket_len);
3712 p += session->ticket_len;
3713 }
3714
3715 if (4 > (size_t) (end - p)) {
3716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3717 }
3718
3719 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3720 p += 4;
3721 }
3722#endif /* MBEDTLS_SSL_CLI_C */
3723#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3724 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3725 if (8 > (size_t) (end - p)) {
3726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3727 }
3728 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3729 p += 8;
3730 }
3731#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3732#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3733
3734 /*
3735 * Misc extension-related info
3736 */
3737#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3738 if (1 > (size_t) (end - p)) {
3739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3740 }
3741
3742 session->mfl_code = *p++;
3743#endif
3744
3745#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3746 if (1 > (size_t) (end - p)) {
3747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3748 }
3749
3750 session->encrypt_then_mac = *p++;
3751#endif
3752
3753 /* Done, should have consumed entire buffer */
3754 if (p != end) {
3755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3756 }
3757
3758 return 0;
3759}
3760
3761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3762
3763#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3764/* Serialization of TLS 1.3 sessions:
3765 *
David Horstmanncb01b362024-02-29 17:31:46 +00003766 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003767 */
3768#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3769MBEDTLS_CHECK_RETURN_CRITICAL
3770static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3771 unsigned char *buf,
3772 size_t buf_len,
3773 size_t *olen)
3774{
3775 unsigned char *p = buf;
3776#if defined(MBEDTLS_SSL_CLI_C) && \
3777 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3778 size_t hostname_len = (session->hostname == NULL) ?
3779 0 : strlen(session->hostname) + 1;
3780#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003781
3782#if defined(MBEDTLS_SSL_SRV_C) && \
3783 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003784 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003785 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003786#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003787 size_t needed = 4 /* ticket_age_add */
3788 + 1 /* ticket_flags */
3789 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003790
David Horstmanne59f9702024-02-29 16:08:57 +00003791 *olen = 0;
3792
3793 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3794 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3795 }
3796 needed += session->resumption_key_len; /* resumption_key */
3797
3798#if defined(MBEDTLS_SSL_EARLY_DATA)
3799 needed += 4; /* max_early_data_size */
3800#endif
3801#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3802 needed += 2; /* record_size_limit */
3803#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3804
3805#if defined(MBEDTLS_HAVE_TIME)
3806 needed += 8; /* ticket_creation_time or ticket_reception_time */
3807#endif
3808
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003809#if defined(MBEDTLS_SSL_SRV_C)
3810 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3811#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003812 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003813 + alpn_len; /* alpn */
3814#endif
3815 }
3816#endif /* MBEDTLS_SSL_SRV_C */
3817
David Horstmanne59f9702024-02-29 16:08:57 +00003818#if defined(MBEDTLS_SSL_CLI_C)
3819 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3821 needed += 2 /* hostname_len */
3822 + hostname_len; /* hostname */
3823#endif
3824
3825 needed += 4 /* ticket_lifetime */
3826 + 2; /* ticket_len */
3827
3828 /* Check size_t overflow */
3829 if (session->ticket_len > SIZE_MAX - needed) {
3830 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3831 }
3832
3833 needed += session->ticket_len; /* ticket */
3834 }
3835#endif /* MBEDTLS_SSL_CLI_C */
3836
3837 *olen = needed;
3838 if (needed > buf_len) {
3839 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3840 }
3841
3842 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3843 p[4] = session->ticket_flags;
3844
3845 /* save resumption_key */
3846 p[5] = session->resumption_key_len;
3847 p += 6;
3848 memcpy(p, session->resumption_key, session->resumption_key_len);
3849 p += session->resumption_key_len;
3850
3851#if defined(MBEDTLS_SSL_EARLY_DATA)
3852 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3853 p += 4;
3854#endif
3855#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3856 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3857 p += 2;
3858#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3859
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003860#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003861 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003862#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003863 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3864 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003865#endif /* MBEDTLS_HAVE_TIME */
3866
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003867#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003868 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3869 p += 2;
3870
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003871 if (alpn_len > 0) {
3872 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003873 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003874 p += alpn_len;
3875 }
3876#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3877 }
3878#endif /* MBEDTLS_SSL_SRV_C */
3879
David Horstmanne59f9702024-02-29 16:08:57 +00003880#if defined(MBEDTLS_SSL_CLI_C)
3881 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3882#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3883 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3884 p += 2;
3885 if (hostname_len > 0) {
3886 /* save host name */
3887 memcpy(p, session->hostname, hostname_len);
3888 p += hostname_len;
3889 }
3890#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3891
3892#if defined(MBEDTLS_HAVE_TIME)
3893 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3894 p += 8;
3895#endif
3896 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3897 p += 4;
3898
3899 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3900 p += 2;
3901
3902 if (session->ticket != NULL && session->ticket_len > 0) {
3903 memcpy(p, session->ticket, session->ticket_len);
3904 p += session->ticket_len;
3905 }
3906 }
3907#endif /* MBEDTLS_SSL_CLI_C */
3908 return 0;
3909}
3910
3911MBEDTLS_CHECK_RETURN_CRITICAL
3912static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3913 const unsigned char *buf,
3914 size_t len)
3915{
3916 const unsigned char *p = buf;
3917 const unsigned char *end = buf + len;
3918
3919 if (end - p < 6) {
3920 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3921 }
3922 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3923 session->ticket_flags = p[4];
3924
3925 /* load resumption_key */
3926 session->resumption_key_len = p[5];
3927 p += 6;
3928
3929 if (end - p < session->resumption_key_len) {
3930 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3931 }
3932
3933 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3934 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3935 }
3936 memcpy(session->resumption_key, p, session->resumption_key_len);
3937 p += session->resumption_key_len;
3938
3939#if defined(MBEDTLS_SSL_EARLY_DATA)
3940 if (end - p < 4) {
3941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3942 }
3943 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3944 p += 4;
3945#endif
3946#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3947 if (end - p < 2) {
3948 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3949 }
3950 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3951 p += 2;
3952#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3953
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003954#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003955 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003956#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003957 if (end - p < 8) {
3958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3959 }
3960 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3961 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003962#endif /* MBEDTLS_HAVE_TIME */
3963
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003964#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003965 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003966
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003967 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3969 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003970
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003971 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3972 p += 2;
3973
3974 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003975 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3976 }
3977
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003978 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003979 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3980 if (ret != 0) {
3981 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003982 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003983 p += alpn_len;
3984 }
3985#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3986 }
3987#endif /* MBEDTLS_SSL_SRV_C */
3988
David Horstmanne59f9702024-02-29 16:08:57 +00003989#if defined(MBEDTLS_SSL_CLI_C)
3990 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3991#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3992 size_t hostname_len;
3993 /* load host name */
3994 if (end - p < 2) {
3995 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3996 }
3997 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3998 p += 2;
3999
4000 if (end - p < (long int) hostname_len) {
4001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4002 }
4003 if (hostname_len > 0) {
4004 session->hostname = mbedtls_calloc(1, hostname_len);
4005 if (session->hostname == NULL) {
4006 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4007 }
4008 memcpy(session->hostname, p, hostname_len);
4009 p += hostname_len;
4010 }
4011#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4012
4013#if defined(MBEDTLS_HAVE_TIME)
4014 if (end - p < 8) {
4015 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4016 }
4017 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4018 p += 8;
4019#endif
4020 if (end - p < 4) {
4021 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4022 }
4023 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4024 p += 4;
4025
4026 if (end - p < 2) {
4027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4028 }
4029 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4030 p += 2;
4031
4032 if (end - p < (long int) session->ticket_len) {
4033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4034 }
4035 if (session->ticket_len > 0) {
4036 session->ticket = mbedtls_calloc(1, session->ticket_len);
4037 if (session->ticket == NULL) {
4038 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4039 }
4040 memcpy(session->ticket, p, session->ticket_len);
4041 p += session->ticket_len;
4042 }
4043 }
4044#endif /* MBEDTLS_SSL_CLI_C */
4045
4046 return 0;
4047
4048}
4049#else /* MBEDTLS_SSL_SESSION_TICKETS */
4050MBEDTLS_CHECK_RETURN_CRITICAL
4051static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4052 unsigned char *buf,
4053 size_t buf_len,
4054 size_t *olen)
4055{
4056 ((void) session);
4057 ((void) buf);
4058 ((void) buf_len);
4059 *olen = 0;
4060 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4061}
4062
4063static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004064 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004065 size_t buf_len)
4066{
4067 ((void) session);
4068 ((void) buf);
4069 ((void) buf_len);
4070 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4071}
4072#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4073#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4074
Paul Bakker5121ce52009-01-03 21:22:43 +00004075/*
Hanno Beckera835da52019-05-16 12:39:07 +01004076 * Define ticket header determining Mbed TLS version
4077 * and structure of the ticket.
4078 */
4079
Hanno Becker94ef3b32019-05-16 12:50:45 +01004080/*
Hanno Becker50b59662019-05-28 14:30:45 +01004081 * Define bitflag determining compile-time settings influencing
4082 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004083 */
4084
Hanno Becker50b59662019-05-28 14:30:45 +01004085#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004086#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004087#else
Hanno Becker3e088662019-05-29 11:10:18 +01004088#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004089#endif /* MBEDTLS_HAVE_TIME */
4090
4091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004092#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004093#else
Hanno Becker3e088662019-05-29 11:10:18 +01004094#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004095#endif /* MBEDTLS_X509_CRT_PARSE_C */
4096
David Horstmann5c5a32f2024-02-13 17:53:35 +00004097#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004098#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004099#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004100#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004101#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4102
Hanno Becker94ef3b32019-05-16 12:50:45 +01004103#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004104#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004105#else
Hanno Becker3e088662019-05-29 11:10:18 +01004106#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004107#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4108
4109#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004110#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004111#else
Hanno Becker3e088662019-05-29 11:10:18 +01004112#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004113#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4114
Hanno Becker94ef3b32019-05-16 12:50:45 +01004115#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004116#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004117#else
Hanno Becker3e088662019-05-29 11:10:18 +01004118#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004119#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4120
Hanno Becker94ef3b32019-05-16 12:50:45 +01004121#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4122#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4123#else
4124#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4125#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4126
David Horstmann92b258b2024-02-13 17:23:34 +00004127#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4128#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4129#else
4130#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4131#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4132
4133#if defined(MBEDTLS_SSL_EARLY_DATA)
4134#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4135#else
4136#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4137#endif /* MBEDTLS_SSL_EARLY_DATA */
4138
4139#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4140#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4141#else
4142#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4143#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4144
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004145#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4146 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004147#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4148#else
4149#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4150#endif /* MBEDTLS_SSL_ALPN */
4151
Hanno Becker3e088662019-05-29 11:10:18 +01004152#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4153#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4154#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4155#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004156#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4157#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004158#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004159#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4160#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4161#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004162#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004163
Hanno Becker50b59662019-05-28 14:30:45 +01004164#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 ((uint16_t) ( \
4166 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4167 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4168 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4169 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4170 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4171 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004172 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004173 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4174 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004175 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4176 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4177 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4178 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004179 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004180 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004181 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004182
Chien Wong4e9683e2023-12-28 17:07:43 +08004183static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004184 MBEDTLS_VERSION_MAJOR,
4185 MBEDTLS_VERSION_MINOR,
4186 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4188 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004189};
Hanno Beckera835da52019-05-16 12:39:07 +01004190
4191/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004192 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004193 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004194 *
David Horstmanncb01b362024-02-29 17:31:46 +00004195 * TLS 1.2 session:
4196 *
4197 * struct {
4198 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4199 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4200 * uint32 ticket_lifetime;
4201 * #endif
4202 * } ClientOnlyData;
4203 *
4204 * struct {
4205 * #if defined(MBEDTLS_HAVE_TIME)
4206 * uint64 start_time;
4207 * #endif
4208 * uint8 session_id_len; // at most 32
4209 * opaque session_id[32];
4210 * opaque master[48]; // fixed length in the standard
4211 * uint32 verify_result;
4212 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4213 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4214 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004215 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004216 * opaque peer_cert_digest<0..2^8-1>
4217 * #endif
4218 * select (endpoint) {
4219 * case client: ClientOnlyData;
4220 * case server: uint64 ticket_creation_time;
4221 * };
4222 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4223 * uint8 mfl_code; // up to 255 according to standard
4224 * #endif
4225 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4226 * uint8 encrypt_then_mac; // 0 or 1
4227 * #endif
4228 * } serialized_session_tls12;
4229 *
4230 *
4231 * TLS 1.3 Session:
4232 *
4233 * struct {
4234 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4235 * opaque hostname<0..2^16-1>;
4236 * #endif
4237 * #if defined(MBEDTLS_HAVE_TIME)
4238 * uint64 ticket_reception_time;
4239 * #endif
4240 * uint32 ticket_lifetime;
4241 * opaque ticket<1..2^16-1>;
4242 * } ClientOnlyData;
4243 *
4244 * struct {
4245 * uint32 ticket_age_add;
4246 * uint8 ticket_flags;
4247 * opaque resumption_key<0..255>;
4248 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4249 * uint32 max_early_data_size;
4250 * #endif
4251 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4252 * uint16 record_size_limit;
4253 * #endif
4254 * select ( endpoint ) {
4255 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004256 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004257 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004258 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004259 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004260 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004261 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004262 * #endif
4263 * };
4264 * } serialized_session_tls13;
4265 *
4266 *
4267 * SSL session:
4268 *
4269 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004270 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004271 * opaque mbedtls_version[3]; // library version: major, minor, patch
4272 * opaque session_format[2]; // library-version specific 16-bit field
4273 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004274 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004275 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004276 * Note: When updating the format, remember to keep
4277 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004278 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004279 * // In this version, `session_format` determines
4280 * // the setting of those compile-time
4281 * // configuration options which influence
4282 * // the structure of mbedtls_ssl_session.
4283 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004284 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004285 * // - TLS 1.2 (0x0303)
4286 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004287 * uint8_t endpoint;
4288 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004289 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004290 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004291 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004292 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004293 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004294 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004295 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004296 *
4297 * };
4298 *
4299 * } serialized_session;
4300 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004301 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004302
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004303MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004304static int ssl_session_save(const mbedtls_ssl_session *session,
4305 unsigned char omit_header,
4306 unsigned char *buf,
4307 size_t buf_len,
4308 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004309{
4310 unsigned char *p = buf;
4311 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004312 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004313#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4314 size_t out_len;
4315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4316#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (session == NULL) {
4318 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4319 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004322 /*
4323 * Add Mbed TLS version identifier
4324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (used <= buf_len) {
4328 memcpy(p, ssl_serialized_session_header,
4329 sizeof(ssl_serialized_session_header));
4330 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004331 }
4332 }
4333
4334 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004335 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004336 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004337 used += 1 /* TLS version */
4338 + 1 /* endpoint */
4339 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (used <= buf_len) {
4341 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004342 *p++ = session->endpoint;
4343 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4344 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004345 }
4346
4347 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004348 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004350#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 case MBEDTLS_SSL_VERSION_TLS1_2:
4352 used += ssl_tls12_session_save(session, p, remaining_len);
4353 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004354#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4355
Jerry Yu251a12e2022-07-13 15:15:48 +08004356#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 case MBEDTLS_SSL_VERSION_TLS1_3:
4358 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4359 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4360 return ret;
4361 }
4362 used += out_len;
4363 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004364#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 default:
4367 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004368 }
4369
4370 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (used > buf_len) {
4372 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4373 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004376}
4377
4378/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004379 * Public wrapper for ssl_session_save()
4380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004381int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4382 unsigned char *buf,
4383 size_t buf_len,
4384 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004385{
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004387}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004388
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004389/*
4390 * Deserialize session, see mbedtls_ssl_session_save() for format.
4391 *
4392 * This internal version is wrapped by a public function that cleans up in
4393 * case of error, and has an extra option omit_header.
4394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004396static int ssl_session_load(mbedtls_ssl_session *session,
4397 unsigned char omit_header,
4398 const unsigned char *buf,
4399 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004400{
4401 const unsigned char *p = buf;
4402 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004403 size_t remaining_len;
4404
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (session == NULL) {
4407 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4408 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004411 /*
4412 * Check Mbed TLS version identifier
4413 */
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4416 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004417 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004418
4419 if (memcmp(p, ssl_serialized_session_header,
4420 sizeof(ssl_serialized_session_header)) != 0) {
4421 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4422 }
4423 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004424 }
4425
4426 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004427 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004428 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004429 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4431 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004432 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004433 session->endpoint = *p++;
4434 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4435 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004436
4437 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004438 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 case MBEDTLS_SSL_VERSION_TLS1_2:
4442 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4444
Jerry Yu438ddd82022-07-07 06:55:50 +00004445#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 case MBEDTLS_SSL_VERSION_TLS1_3:
4447 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004448#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 default:
4451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004452 }
4453}
4454
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004455/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004456 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004458int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4459 const unsigned char *buf,
4460 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004461{
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (ret != 0) {
4465 mbedtls_ssl_session_free(session);
4466 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004469}
4470
4471/*
Paul Bakker1961b702013-01-25 14:49:24 +01004472 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004476{
4477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4478
Ronald Cron66dbf912022-02-02 15:33:46 +01004479 /*
4480 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004481 * that were written into the output buffer by the previous handshake step,
4482 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004483 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4484 * We proceed to the next handshake step only when all data from the
4485 * previous one have been sent to the peer, thus we make sure that this is
4486 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4487 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4488 * we have to wait before to go ahead.
4489 * In the case of TLS 1.3, handshake step handlers do not send data to the
4490 * peer. Data are only sent here and through
4491 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004492 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4495 return ret;
4496 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004497
4498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4500 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4501 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4502 return ret;
4503 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004504 }
4505#endif /* MBEDTLS_SSL_PROTO_DTLS */
4506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004508}
4509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004511{
Hanno Becker41934dd2021-08-07 19:13:43 +01004512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004515 ssl->conf == NULL ||
4516 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004519 }
4520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 ret = ssl_prepare_handshake_step(ssl);
4522 if (ret != 0) {
4523 return ret;
4524 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 ret = mbedtls_ssl_handle_pending_alert(ssl);
4527 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004528 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 }
Jerry Yue7047812021-09-13 19:26:39 +08004530
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004531 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4532 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4533 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4537 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004538 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004541 case MBEDTLS_SSL_HELLO_REQUEST:
4542 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004543 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004544 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004545
Ronald Cron9f0fba32022-02-10 16:45:15 +01004546 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004548 break;
4549
4550 default:
4551#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4553 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4554 } else {
4555 ret = mbedtls_ssl_handshake_client_step(ssl);
4556 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004557#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004559#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004561#endif
4562 }
Jerry Yub9930e72021-08-06 17:11:51 +08004563 }
Ronald Cron6291b232023-03-08 15:51:25 +01004564#endif /* MBEDTLS_SSL_CLI_C */
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004568#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4569 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004571 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 ret = mbedtls_ssl_handshake_server_step(ssl);
4573 }
Ronald Cron6291b232023-03-08 15:51:25 +01004574#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4575 ret = mbedtls_ssl_handshake_server_step(ssl);
4576#else
4577 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004578#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004579 }
4580#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004583 /* handshake_step return error. And it is same
4584 * with alert_reason.
4585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (ssl->send_alert) {
4587 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004588 goto cleanup;
4589 }
4590 }
4591
4592cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004594}
4595
4596/*
4597 * Perform the SSL handshake
4598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004600{
4601 int ret = 0;
4602
Hanno Beckera817ea42020-10-20 15:20:23 +01004603 /* Sanity checks */
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (ssl == NULL || ssl->conf == NULL) {
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4607 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004608
Hanno Beckera817ea42020-10-20 15:20:23 +01004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4611 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4612 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4613 "mbedtls_ssl_set_timer_cb() for DTLS"));
4614 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004615 }
4616#endif /* MBEDTLS_SSL_PROTO_DTLS */
4617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004619
Hanno Beckera817ea42020-10-20 15:20:23 +01004620 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4622 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004625 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 }
Paul Bakker1961b702013-01-25 14:49:24 +01004627 }
4628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004632}
4633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634#if defined(MBEDTLS_SSL_RENEGOTIATION)
4635#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004636/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004637 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004638 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004639MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004640static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004641{
Janos Follath865b3eb2019-12-16 11:46:15 +00004642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004645
4646 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4648 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4651 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4652 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004653 }
4654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004660
4661/*
4662 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 * - any side: calling mbedtls_ssl_renegotiate(),
4664 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4665 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004666 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004667 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004670int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004671{
Janos Follath865b3eb2019-12-16 11:46:15 +00004672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if ((ret = ssl_handshake_init(ssl)) != 0) {
4677 return ret;
4678 }
Paul Bakker48916f92012-09-16 19:57:18 +00004679
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004680 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4681 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4684 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4685 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004686 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004688 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004690 }
4691#endif
4692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4694 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4697 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4698 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004699 }
4700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004704}
4705
4706/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004707 * Renegotiate current connection on client,
4708 * or request renegotiation on server
4709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004710int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (ssl == NULL || ssl->conf == NULL) {
4715 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4716 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004719 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4721 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4722 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4723 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004726
4727 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if (ssl->out_left != 0) {
4729 return mbedtls_ssl_flush_output(ssl);
4730 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004737 /*
4738 * On client, either start the renegotiation process or,
4739 * if already in progress, continue the handshake
4740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4742 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004745
4746 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4747 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4748 return ret;
4749 }
4750 } else {
4751 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4752 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4753 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004754 }
4755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004763{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004764 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004767 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004769
Valerio Setti6f0441d2023-07-03 12:11:36 +02004770#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004771#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (ssl->handshake->group_list_heap_allocated) {
4773 mbedtls_free((void *) handshake->group_list);
4774 }
Brett Warrene0edc842021-08-17 09:53:13 +01004775 handshake->group_list = NULL;
4776#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004777#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004778
Ronald Crone68ab4f2022-10-05 12:46:29 +02004779#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004780#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (ssl->handshake->sig_algs_heap_allocated) {
4782 mbedtls_free((void *) handshake->sig_algs);
4783 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004784 handshake->sig_algs = NULL;
4785#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004786#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (ssl->handshake->certificate_request_context) {
4788 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004789 }
4790#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004791#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004792
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004793#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4795 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004796 handshake->async_in_progress = 0;
4797 }
4798#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4799
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004800#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004801#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004803#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004804 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004805#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004806#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004807#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004808#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004810#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004811 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004812#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004813#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004817#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004818#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004819 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004821#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004822
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004823#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004824#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004826 /*
4827 * Opaque keys are not stored in the handshake's data and it's the user
4828 * responsibility to destroy them. Clear ones, instead, are created by
4829 * the TLS library and should be destroyed at the same level
4830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4832 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004833 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004834 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4835#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004838#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004840 handshake->ecjpake_cache = NULL;
4841 handshake->ecjpake_cache_len = 0;
4842#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004843#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004844
Valerio Setti7aeec542023-07-05 18:57:21 +02004845#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004846 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004847 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004848 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004850#endif
4851
Ronald Cron73fe8df2022-10-05 14:31:43 +02004852#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004853#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004855 /* The maintenance of the external PSK key slot is the
4856 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (ssl->handshake->psk_opaque_is_internal) {
4858 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004859 ssl->handshake->psk_opaque_is_internal = 0;
4860 }
4861 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4862 }
Neil Armstronge952a302022-05-03 10:22:14 +02004863#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004865 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004866 }
Neil Armstronge952a302022-05-03 10:22:14 +02004867#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004868#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4871 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004872 /*
4873 * Free only the linked list wrapper, not the keys themselves
4874 * since the belong to the SNI callback
4875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004878
Gilles Peskineeccd8882020-03-10 12:19:08 +01004879#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4881 if (handshake->ecrs_peer_cert != NULL) {
4882 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4883 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004884 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004885#endif
4886
Hanno Becker75173122019-02-06 16:18:31 +00004887#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4888 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004890#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4891
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004892#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4894 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004895#endif /* MBEDTLS_SSL_CLI_C &&
4896 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004897
4898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 mbedtls_ssl_flight_free(handshake->flight);
4900 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004902
Valerio Settiea59c432023-07-25 11:14:03 +02004903#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004904 if (handshake->xxdh_psa_privkey_is_external == 0) {
4905 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 }
Valerio Settiea59c432023-07-25 11:14:03 +02004907#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004908
Ronald Cron6f135e12021-12-08 16:57:54 +01004909#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 mbedtls_ssl_transform_free(handshake->transform_handshake);
4911 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004912#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4914 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004915#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004916#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004917
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004918
4919#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4920 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4921 * processes datagrams and the fact that a datagram is allowed to have
4922 * several records in it, it is possible that the I/O buffers are not
4923 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4925 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004926#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004927
Jerry Yuba9c7272021-10-30 11:54:10 +08004928 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_platform_zeroize(handshake,
4930 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004931}
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004934{
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004936 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004941#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004942
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004943#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004944#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4945 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004947#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004949#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004950
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004951#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4952 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004953 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004954#endif
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004957}
4958
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004959#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004960
4961#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4962#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4963#else
4964#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4965#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4966
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004967#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004968
4969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4970#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4971#else
4972#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4973#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4974
4975#if defined(MBEDTLS_SSL_ALPN)
4976#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4977#else
4978#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4979#endif /* MBEDTLS_SSL_ALPN */
4980
4981#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4982#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4983#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4984#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4985
4986#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 ((uint32_t) ( \
4988 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4989 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4990 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4991 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4992 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4993 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4994 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4995 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004996
Chien Wong4e9683e2023-12-28 17:07:43 +08004997static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004998 MBEDTLS_VERSION_MAJOR,
4999 MBEDTLS_VERSION_MINOR,
5000 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5002 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5003 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5004 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5005 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005006};
5007
Paul Bakker5121ce52009-01-03 21:22:43 +00005008/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005009 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005010 *
5011 * The format of the serialized data is:
5012 * (in the presentation language of TLS, RFC 8446 section 3)
5013 *
5014 * // header
5015 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005016 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005017 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005018 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005019 * Note: When updating the format, remember to keep these
5020 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005021 *
5022 * // session sub-structure
5023 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5024 * // transform sub-structure
5025 * uint8 random[64]; // ServerHello.random+ClientHello.random
5026 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5027 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5028 * // fields from ssl_context
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005029 * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005030 * uint64 in_window_top; // DTLS: last validated record seq_num
5031 * uint64 in_window; // DTLS: bitmask for replay protection
5032 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5033 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5034 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5035 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5036 *
5037 * Note that many fields of the ssl_context or sub-structures are not
5038 * serialized, as they fall in one of the following categories:
5039 *
5040 * 1. forced value (eg in_left must be 0)
5041 * 2. pointer to dynamically-allocated memory (eg session, transform)
5042 * 3. value can be re-derived from other data (eg session keys from MS)
5043 * 4. value was temporary (eg content of input buffer)
5044 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5047 unsigned char *buf,
5048 size_t buf_len,
5049 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005050{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005051 unsigned char *p = buf;
5052 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005053 size_t session_len;
5054 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005055
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005056 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005057 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5058 * this function's documentation.
5059 *
5060 * These are due to assumptions/limitations in the implementation. Some of
5061 * them are likely to stay (no handshake in progress) some might go away
5062 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005063 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005064 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5066 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (ssl->handshake != NULL) {
5070 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
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 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (ssl->transform == NULL || ssl->session == NULL) {
5075 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005077 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005078 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (mbedtls_ssl_check_pending(ssl) != 0) {
5080 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005082 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (ssl->out_left != 0) {
5084 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5085 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005086 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005087 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5089 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is 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 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5094 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 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 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5099 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005101 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005102 /* Renegotiation must not be enabled */
5103#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5105 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005107 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005108#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005109
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005110 /*
5111 * Version and format identifier
5112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (used <= buf_len) {
5116 memcpy(p, ssl_serialized_context_header,
5117 sizeof(ssl_serialized_context_header));
5118 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005119 }
5120
5121 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005122 * Session (length + data)
5123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5125 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5126 return ret;
5127 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005128
5129 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (used <= buf_len) {
5131 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005132 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 ret = ssl_session_save(ssl->session, 1,
5135 p, session_len, &session_len);
5136 if (ret != 0) {
5137 return ret;
5138 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005139
5140 p += session_len;
5141 }
5142
5143 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005144 * Transform
5145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 used += sizeof(ssl->transform->randbytes);
5147 if (used <= buf_len) {
5148 memcpy(p, ssl->transform->randbytes,
5149 sizeof(ssl->transform->randbytes));
5150 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005151 }
5152
5153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005154 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005156 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005158 p += ssl->transform->in_cid_len;
5159
5160 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005162 p += ssl->transform->out_cid_len;
5163 }
5164#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5165
5166 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005167 * Saved fields from top-level ssl_context structure
5168 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005169 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (used <= buf_len) {
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005171 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005172 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005173 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005174
5175#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5176 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (used <= buf_len) {
5178 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005179 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005182 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005183 }
5184#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5185
5186#if defined(MBEDTLS_SSL_PROTO_DTLS)
5187 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005189 *p++ = ssl->disable_datagram_packing;
5190 }
5191#endif /* MBEDTLS_SSL_PROTO_DTLS */
5192
Jerry Yuae0b2e22021-10-08 15:21:19 +08005193 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (used <= buf_len) {
5195 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005196 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005197 }
5198
5199#if defined(MBEDTLS_SSL_PROTO_DTLS)
5200 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 if (used <= buf_len) {
5202 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005203 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005204 }
5205#endif /* MBEDTLS_SSL_PROTO_DTLS */
5206
5207#if defined(MBEDTLS_SSL_ALPN)
5208 {
5209 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005211 : 0;
5212
5213 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005215 *p++ = alpn_len;
5216
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (ssl->alpn_chosen != NULL) {
5218 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005219 p += alpn_len;
5220 }
5221 }
5222 }
5223#endif /* MBEDTLS_SSL_ALPN */
5224
5225 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005226 * Done
5227 */
5228 *olen = used;
5229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (used > buf_len) {
5231 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5232 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005237}
5238
5239/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005240 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005241 *
5242 * This internal version is wrapped by a public function that cleans up in
5243 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005244 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005245MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005246static int ssl_context_load(mbedtls_ssl_context *ssl,
5247 const unsigned char *buf,
5248 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005249{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005250 const unsigned char *p = buf;
5251 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005252 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005255 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005256#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005257
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005258 /*
5259 * The context should have been freshly setup or reset.
5260 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005261 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005262 * renegotiating, or if the user mistakenly loaded a session first.)
5263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5265 ssl->session != NULL) {
5266 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005267 }
5268
5269 /*
5270 * We can't check that the config matches the initial one, but we can at
5271 * least check it matches the requirements for serializing.
5272 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005273 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005274#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005275 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005276#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005277 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5278 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5279 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5280 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005285
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005286 /*
5287 * Check version identifier
5288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005292
5293 if (memcmp(p, ssl_serialized_context_header,
5294 sizeof(ssl_serialized_context_header)) != 0) {
5295 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5296 }
5297 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005298
5299 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005300 * Session
5301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 if ((size_t) (end - p) < 4) {
5303 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5304 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005305
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005306 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005307 p += 4;
5308
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005309 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005310 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005311 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005312 ssl->session_in = ssl->session;
5313 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005314 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if ((size_t) (end - p) < session_len) {
5317 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5318 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 ret = ssl_session_load(ssl->session, 1, p, session_len);
5321 if (ret != 0) {
5322 mbedtls_ssl_session_free(ssl->session);
5323 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005324 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005325
5326 p += session_len;
5327
5328 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005329 * Transform
5330 */
5331
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005332 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005333 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005334#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005335 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005336 ssl->transform_in = ssl->transform;
5337 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005338 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005339#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005340
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005341#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5343 if (prf_func == NULL) {
5344 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5345 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005346
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005347 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5349 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5350 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 ret = ssl_tls12_populate_transform(ssl->transform,
5353 ssl->session->ciphersuite,
5354 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005357#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 prf_func,
5359 p, /* currently pointing to randbytes */
5360 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5361 ssl->conf->endpoint,
5362 ssl);
5363 if (ret != 0) {
5364 return ret;
5365 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005366#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005368
5369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5370 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if ((size_t) (end - p) < 1) {
5372 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5373 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005374
5375 ssl->transform->in_cid_len = *p++;
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5378 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5379 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005382 p += ssl->transform->in_cid_len;
5383
5384 ssl->transform->out_cid_len = *p++;
5385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5387 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5388 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005391 p += ssl->transform->out_cid_len;
5392#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5393
5394 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005395 * Saved fields from top-level ssl_context structure
5396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 if ((size_t) (end - p) < 4) {
5398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5399 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005400
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005401 ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005402 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005403
5404#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 if ((size_t) (end - p) < 16) {
5406 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5407 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005408
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005409 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005410 p += 8;
5411
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005412 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005413 p += 8;
5414#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5415
5416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if ((size_t) (end - p) < 1) {
5418 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5419 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005420
5421 ssl->disable_datagram_packing = *p++;
5422#endif /* MBEDTLS_SSL_PROTO_DTLS */
5423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5425 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5426 }
5427 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5428 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005429
5430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if ((size_t) (end - p) < 2) {
5432 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5433 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005434
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005435 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005436 p += 2;
5437#endif /* MBEDTLS_SSL_PROTO_DTLS */
5438
5439#if defined(MBEDTLS_SSL_ALPN)
5440 {
5441 uint8_t alpn_len;
5442 const char **cur;
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if ((size_t) (end - p) < 1) {
5445 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5446 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005447
5448 alpn_len = *p++;
5449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005451 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5453 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005454 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005455 ssl->alpn_chosen = *cur;
5456 break;
5457 }
5458 }
5459 }
5460
5461 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5463 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5464 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005465
5466 p += alpn_len;
5467 }
5468#endif /* MBEDTLS_SSL_ALPN */
5469
5470 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005471 * Forced fields from top-level ssl_context structure
5472 *
5473 * Most of them already set to the correct value by mbedtls_ssl_init() and
5474 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5475 */
5476 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005477 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005478
Hanno Becker361b10d2019-08-30 10:42:49 +01005479 /* Adjust pointers for header fields of outgoing records to
5480 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005482
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005483#if defined(MBEDTLS_SSL_PROTO_DTLS)
5484 ssl->in_epoch = 1;
5485#endif
5486
5487 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5488 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005489 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5490 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 if (ssl->handshake != NULL) {
5492 mbedtls_ssl_handshake_free(ssl);
5493 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005494 ssl->handshake = NULL;
5495 }
5496
5497 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005498 * Done - should have consumed entire buffer
5499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 if (p != end) {
5501 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5502 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005503
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005505}
5506
5507/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005508 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005510int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5511 const unsigned char *buf,
5512 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005513{
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 if (ret != 0) {
5517 mbedtls_ssl_free(context);
5518 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005521}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005522#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005523
5524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 * Free an SSL context
5526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005527void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005528{
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005530 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005536#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5537 size_t out_buf_len = ssl->out_buf_len;
5538#else
5539 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5540#endif
5541
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005542 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005543 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005547#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5548 size_t in_buf_len = ssl->in_buf_len;
5549#else
5550 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5551#endif
5552
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005553 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005554 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 }
5556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (ssl->transform) {
5558 mbedtls_ssl_transform_free(ssl->transform);
5559 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005560 }
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (ssl->handshake) {
5563 mbedtls_ssl_handshake_free(ssl);
5564 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005565
5566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5568 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005569#endif
5570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 mbedtls_ssl_session_free(ssl->session_negotiate);
5572 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005573 }
5574
Ronald Cron6f135e12021-12-08 16:57:54 +01005575#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 mbedtls_ssl_transform_free(ssl->transform_application);
5577 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005578#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (ssl->session) {
5581 mbedtls_ssl_session_free(ssl->session);
5582 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005583 }
5584
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005585#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005587 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005593#endif
5594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005596
Paul Bakker86f04f42013-02-14 11:20:09 +01005597 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005599}
5600
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005601/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005602 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005605{
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005607}
5608
Gilles Peskineae270bf2021-06-02 00:05:29 +02005609/* The selection should be the same as mbedtls_x509_crt_profile_default in
5610 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005611 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005612 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005613 * about this list.
5614 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005615static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005616#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005617 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005618#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005619#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005620 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005621#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005622#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005623 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005624#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005625#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005626 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005627#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005628#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005629 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005630#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005631#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005632 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005633#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005634#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005635 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005636#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005637#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005638 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005639#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005640#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005641 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5642 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5643 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5644 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5645 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5646#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005647 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005648};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005649
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005650static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005651 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5652 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5653 0
5654};
5655
Ronald Crone68ab4f2022-10-05 12:46:29 +02005656#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005657
Jerry Yu909df7b2022-01-22 11:56:27 +08005658/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005659 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005660 * rules SHOULD be upheld.
5661 * - No duplicate entries.
5662 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005663 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005664 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005665 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005666static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005667
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005668#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005669 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005670 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005671 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005672 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5673#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005674
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005675#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005676 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005677 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005678 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005679 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5680#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005681
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005682#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005683 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005684 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005685 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005686 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5687#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005688
Yanray Wang1136fad2023-11-22 16:54:31 +08005689#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005690 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005691#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005692
Yanray Wang1136fad2023-11-22 16:54:31 +08005693#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005694 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005695#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005696
Yanray Wang1136fad2023-11-22 16:54:31 +08005697#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005698 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005699#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005700
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005701#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005702 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005703#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005704
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005705#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005706 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005707#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005708
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005709#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005710 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005711#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005712
Gabor Mezei15b95a62022-05-09 16:37:58 +02005713 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005714};
5715
5716/* NOTICE: see above */
5717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5718static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005719
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005720#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005721#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005723#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005724#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5725 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005726#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005727#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005729#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005730#endif /* MBEDTLS_MD_CAN_SHA512 */
5731
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005732#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005733#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005735#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005736#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5737 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005738#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005739#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005741#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005742#endif /* MBEDTLS_MD_CAN_SHA384 */
5743
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005744#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005745#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005747#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005748#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5749 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005750#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005751#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005753#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005754#endif /* MBEDTLS_MD_CAN_SHA256 */
5755
Gabor Mezei15b95a62022-05-09 16:37:58 +02005756 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005757};
5758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005759
Jerry Yu909df7b2022-01-22 11:56:27 +08005760/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005761static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005762
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005763#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005764 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005765 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005766 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005767 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5768#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005769
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005770#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005771 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005772 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005773 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005774 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5775#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005776
Gabor Mezei15b95a62022-05-09 16:37:58 +02005777 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005778};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005779
Jerry Yu909df7b2022-01-22 11:56:27 +08005780/* NOTICE: see above */
5781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5782static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005783
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005784#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005785#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005787#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005788#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005789
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005790#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005791#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005793#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005794#endif /* MBEDTLS_MD_CAN_SHA384 */
5795
Gabor Mezei15b95a62022-05-09 16:37:58 +02005796 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005797};
Jerry Yu909df7b2022-01-22 11:56:27 +08005798#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5799
Ronald Crone68ab4f2022-10-05 12:46:29 +02005800#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005801
Chien Wong4e9683e2023-12-28 17:07:43 +08005802static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005803#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005804 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005805#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005806#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005807 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005808#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005809 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005810};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005811
Ronald Crone68ab4f2022-10-05 12:46:29 +02005812#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005813/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005814 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005815MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005816static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005817{
5818 size_t i, j;
5819 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5822 for (j = 0; j < i; j++) {
5823 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005824 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 }
5826 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5827 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5828 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005829 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005830 }
5831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005833}
5834
Ronald Crone68ab4f2022-10-05 12:46:29 +02005835#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005836
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005837/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005838 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005840int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5841 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005842{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005843#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005845#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005846
Ronald Crone68ab4f2022-10-05 12:46:29 +02005847#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5849 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5850 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005851 }
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5854 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5855 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005856 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005857
5858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5860 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5861 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005862 }
5863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5865 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5866 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005867 }
5868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005869#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005870
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005871 /* Use the functions here so that they are covered in tests,
5872 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 mbedtls_ssl_conf_endpoint(conf, endpoint);
5874 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005875
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005876 /*
5877 * Things that are common to all presets
5878 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005879#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005881 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5882#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005883 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005884#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005885 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5886 * handling is disabled by default in Mbed TLS 3.6.x for backward
5887 * compatibility with client applications developed using Mbed TLS 3.5
5888 * or earlier with the default configuration.
5889 *
5890 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5891 * disabled, and a Mbed TLS client with the default configuration would
5892 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5893 * server.
5894 *
5895 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5896 * an Mbed TLS client with the default configuration establishes a
5897 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5898 * following the handshake the TLS 1.3 server sends NewSessionTicket
5899 * messages and the Mbed TLS client processes them, this results in
5900 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5901 * mbedtls_ssl_handshake(), ...) to eventually return an
5902 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5903 * (see the documentation of mbedtls_ssl_read() for more information on
5904 * that error code). Applications unaware of that TLS 1.3 specific non
5905 * fatal error code are then failing.
5906 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005907 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5908 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005909#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005910#endif
5911 }
5912#endif
5913
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005914#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5915 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5916#endif
5917
5918#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5919 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5920#endif
5921
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005922#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005923 conf->f_cookie_write = ssl_cookie_write_dummy;
5924 conf->f_cookie_check = ssl_cookie_check_dummy;
5925#endif
5926
5927#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5928 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5929#endif
5930
Janos Follath088ce432017-04-10 12:42:31 +01005931#if defined(MBEDTLS_SSL_SRV_C)
5932 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005933 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005934#endif
5935
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005936#if defined(MBEDTLS_SSL_PROTO_DTLS)
5937 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5938 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5939#endif
5940
5941#if defined(MBEDTLS_SSL_RENEGOTIATION)
5942 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 memset(conf->renego_period, 0x00, 2);
5944 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005945#endif
5946
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005947#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005949 const unsigned char dhm_p[] =
5950 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5951 const unsigned char dhm_g[] =
5952 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5955 dhm_p, sizeof(dhm_p),
5956 dhm_g, sizeof(dhm_g))) != 0) {
5957 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005958 }
5959 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005960#endif
5961
Ronald Cron6f135e12021-12-08 16:57:54 +01005962#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005963
5964#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005965 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005966#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005967 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005968#endif
5969#endif /* MBEDTLS_SSL_EARLY_DATA */
5970
Jerry Yud0766ec2022-09-22 10:46:57 +08005971#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005972 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005974#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005975 /*
5976 * Allow all TLS 1.3 key exchange modes by default.
5977 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005978 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005979#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005982#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005983 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005984 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005985#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005987#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005989#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005990 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5991 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005992#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5993 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5994 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5995#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5996 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5997 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5998#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006000#endif
6001 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006002
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006003 /*
6004 * Preset-specific defaults
6005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006007 /*
6008 * NSA Suite B
6009 */
6010 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006011
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006012 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006013
6014#if defined(MBEDTLS_X509_CRT_PARSE_C)
6015 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006016#endif
6017
Ronald Crone68ab4f2022-10-05 12:46:29 +02006018#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006021 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006023#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006025#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006026
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006027#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006028 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006029#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006030 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006031 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006032
6033 /*
6034 * Default
6035 */
6036 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006037
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006038 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006039
6040#if defined(MBEDTLS_X509_CRT_PARSE_C)
6041 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6042#endif
6043
Ronald Crone68ab4f2022-10-05 12:46:29 +02006044#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006045#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006047 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006049#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006051#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006052
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006053#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006054 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006055#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006056 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006057
6058#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6059 conf->dhm_min_bitlen = 1024;
6060#endif
6061 }
6062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006064}
6065
6066/*
6067 * Free mbedtls_ssl_config
6068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006069void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006070{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006071 if (conf == NULL) {
6072 return;
6073 }
6074
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006075#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 mbedtls_mpi_free(&conf->dhm_P);
6077 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006078#endif
6079
Ronald Cron73fe8df2022-10-05 14:31:43 +02006080#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006083 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6084 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006085#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006087 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006088 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006089 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006090 }
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006093 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006094 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006095 conf->psk_identity_len = 0;
6096 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006097#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006098
6099#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006101#endif
6102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006104}
6105
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006106#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006107 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006108/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006111unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6115 return MBEDTLS_SSL_SIG_RSA;
6116 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006117#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006118#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6120 return MBEDTLS_SSL_SIG_ECDSA;
6121 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006122#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006124}
6125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006127{
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006129 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006131 case MBEDTLS_PK_ECDSA:
6132 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006134 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006136 }
6137}
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006140{
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_RSA_C)
6143 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006145#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006146#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006149#endif
6150 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006152 }
6153}
Valerio Settie9646ec2023-08-02 20:02:28 +02006154#endif /* MBEDTLS_PK_C &&
6155 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006156
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006157/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006158 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006160mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006161{
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006163#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006164 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006166#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006167#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006170#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006171#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006174#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006175#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006176 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006177 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006178#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006179#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006182#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006183#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006186#endif
6187 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006189 }
6190}
6191
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006192/*
6193 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006195unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006196{
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006198#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006199 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006201#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006202#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006203 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006205#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006206#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006207 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006209#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006210#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006211 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006213#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006214#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006215 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006217#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006218#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006219 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006221#endif
6222 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006224 }
6225}
6226
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006227/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006228 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006229 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006231int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006232{
Gilles Peskine449bd832023-01-11 14:50:10 +01006233 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 if (group_list == NULL) {
6236 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006237 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 for (; *group_list != 0; group_list++) {
6240 if (*group_list == tls_id) {
6241 return 0;
6242 }
6243 }
6244
6245 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006246}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006247
Valerio Setti49e69072023-06-27 17:27:51 +02006248#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006249/*
6250 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006252int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006253{
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006257 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006261}
Valerio Setti49e69072023-06-27 17:27:51 +02006262#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006263
Valerio Setti18c9fed2022-12-30 17:44:24 +01006264static const struct {
6265 uint16_t tls_id;
6266 mbedtls_ecp_group_id ecp_group_id;
6267 psa_ecc_family_t psa_family;
6268 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006269} tls_id_match_table[] =
6270{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006271#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006272 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006273#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006274#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006275 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006276#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006277#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006278 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006279#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006280#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006281 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006282#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006283#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006284 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006285#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006286#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006287 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006288#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006289#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006290 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006291#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006292#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006293 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006294#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006295#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006296 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006297#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006298#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006299 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006300#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006301#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006302 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006303#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006304#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006305 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006306#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006307#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006308 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006309#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006310 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006311};
6312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006314 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006316{
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6318 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006319 if (type != NULL) {
6320 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 }
6322 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006323 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006325 return PSA_SUCCESS;
6326 }
6327 }
6328
6329 return PSA_ERROR_NOT_SUPPORTED;
6330}
6331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006333{
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6335 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006336 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006338 }
6339
6340 return MBEDTLS_ECP_DP_NONE;
6341}
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006344{
Gilles Peskine449bd832023-01-11 14:50:10 +01006345 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6346 i++) {
6347 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006348 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006350 }
6351
6352 return 0;
6353}
6354
Valerio Setti67419f02023-01-04 16:12:42 +01006355#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006356static const struct {
6357 uint16_t tls_id;
6358 const char *name;
6359} tls_id_curve_name_table[] =
6360{
Valerio Setti54e23792023-07-07 10:49:27 +02006361 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6362 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6363 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6364 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6365 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6366 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6367 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6368 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6369 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6370 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6371 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6372 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6373 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006374 { 0, NULL },
6375};
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006378{
Valerio Settiacd32c02023-06-29 18:06:29 +02006379 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6380 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6381 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006383 }
6384
6385 return NULL;
6386}
Valerio Setti67419f02023-01-04 16:12:42 +01006387#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006388
Jerry Yu148165c2021-09-24 23:20:59 +08006389#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006390int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6391 const mbedtls_md_type_t md,
6392 unsigned char *dst,
6393 size_t dst_len,
6394 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006395{
Ronald Cronf6893e12022-01-07 22:09:01 +01006396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6397 psa_hash_operation_t *hash_operation_to_clone;
6398 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6399
Jerry Yu148165c2021-09-24 23:20:59 +08006400 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006401
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006403#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 case MBEDTLS_MD_SHA384:
6405 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6406 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006407#endif
6408
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006409#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 case MBEDTLS_MD_SHA256:
6411 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6412 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006413#endif
6414
Gilles Peskine449bd832023-01-11 14:50:10 +01006415 default:
6416 goto exit;
6417 }
6418
6419 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6420 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006421 goto exit;
6422 }
6423
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6425 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006426 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006428
6429exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006430#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6431 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006432 (void) ssl;
6433#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006434 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006435}
6436#else /* MBEDTLS_USE_PSA_CRYPTO */
6437
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006438#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006439MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006440static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6441 unsigned char *dst,
6442 size_t dst_len,
6443 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006444{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006445 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006446 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 if (dst_len < 48) {
6449 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6450 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006451
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006452 mbedtls_md_init(&sha384);
6453 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006454 if (ret != 0) {
6455 goto exit;
6456 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006457 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006458 if (ret != 0) {
6459 goto exit;
6460 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006461
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006462 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006463 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006464 goto exit;
6465 }
6466
6467 *olen = 48;
6468
6469exit:
6470
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006471 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006473}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006474#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006475
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006476#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006478static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6479 unsigned char *dst,
6480 size_t dst_len,
6481 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006482{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006483 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006484 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 if (dst_len < 32) {
6487 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6488 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006489
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006490 mbedtls_md_init(&sha256);
6491 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6492 if (ret != 0) {
6493 goto exit;
6494 }
6495 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6496 if (ret != 0) {
6497 goto exit;
6498 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006499
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006500 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006502 goto exit;
6503 }
6504
6505 *olen = 32;
6506
6507exit:
6508
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006509 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006510 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006511}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006512#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6515 const mbedtls_md_type_t md,
6516 unsigned char *dst,
6517 size_t dst_len,
6518 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006519{
Gilles Peskine449bd832023-01-11 14:50:10 +01006520 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006521
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006522#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 case MBEDTLS_MD_SHA384:
6524 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006525#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006526
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006527#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 case MBEDTLS_MD_SHA256:
6529 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006530#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006533#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6534 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 (void) ssl;
6536 (void) dst;
6537 (void) dst_len;
6538 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006539#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006541 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006543}
XiaokangQian647719a2021-12-07 09:16:29 +00006544
Jerry Yu148165c2021-09-24 23:20:59 +08006545#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006546
Ronald Crone68ab4f2022-10-05 12:46:29 +02006547#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006548/* mbedtls_ssl_parse_sig_alg_ext()
6549 *
6550 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6551 * value (TLS 1.3 RFC8446):
6552 * enum {
6553 * ....
6554 * ecdsa_secp256r1_sha256( 0x0403 ),
6555 * ecdsa_secp384r1_sha384( 0x0503 ),
6556 * ecdsa_secp521r1_sha512( 0x0603 ),
6557 * ....
6558 * } SignatureScheme;
6559 *
6560 * struct {
6561 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6562 * } SignatureSchemeList;
6563 *
6564 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6565 * value (TLS 1.2 RFC5246):
6566 * enum {
6567 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6568 * sha512(6), (255)
6569 * } HashAlgorithm;
6570 *
6571 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6572 * SignatureAlgorithm;
6573 *
6574 * struct {
6575 * HashAlgorithm hash;
6576 * SignatureAlgorithm signature;
6577 * } SignatureAndHashAlgorithm;
6578 *
6579 * SignatureAndHashAlgorithm
6580 * supported_signature_algorithms<2..2^16-2>;
6581 *
6582 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6583 * generalization of the TLS 1.2 signature algorithm extension.
6584 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6585 * `SignatureScheme` field of TLS 1.3
6586 *
6587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006588int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6589 const unsigned char *buf,
6590 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006591{
6592 const unsigned char *p = buf;
6593 size_t supported_sig_algs_len = 0;
6594 const unsigned char *supported_sig_algs_end;
6595 uint16_t sig_alg;
6596 uint32_t common_idx = 0;
6597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6599 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006600 p += 2;
6601
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 memset(ssl->handshake->received_sig_algs, 0,
6603 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006606 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 while (p < supported_sig_algs_end) {
6608 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6609 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006610 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6612 sig_alg,
6613 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6616 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6617 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006618 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006619 }
6620#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6623 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006624
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006626 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6627 common_idx += 1;
6628 }
6629 }
6630 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 if (p != end) {
6632 MBEDTLS_SSL_DEBUG_MSG(1,
6633 ("Signature algorithms extension length misaligned"));
6634 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6635 MBEDTLS_ERR_SSL_DECODE_ERROR);
6636 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006637 }
6638
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 if (common_idx == 0) {
6640 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6641 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6642 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6643 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006644 }
6645
Gabor Mezei15b95a62022-05-09 16:37:58 +02006646 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006648}
6649
Ronald Crone68ab4f2022-10-05 12:46:29 +02006650#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006651
Jerry Yudc7bd172022-02-17 13:44:15 +08006652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6653
6654#if defined(MBEDTLS_USE_PSA_CRYPTO)
6655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6657 mbedtls_svc_key_id_t key,
6658 psa_algorithm_t alg,
6659 const unsigned char *raw_psk, size_t raw_psk_length,
6660 const unsigned char *seed, size_t seed_length,
6661 const unsigned char *label, size_t label_length,
6662 const unsigned char *other_secret,
6663 size_t other_secret_length,
6664 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006665{
6666 psa_status_t status;
6667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 status = psa_key_derivation_setup(derivation, alg);
6669 if (status != PSA_SUCCESS) {
6670 return status;
6671 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6674 status = psa_key_derivation_input_bytes(derivation,
6675 PSA_KEY_DERIVATION_INPUT_SEED,
6676 seed, seed_length);
6677 if (status != PSA_SUCCESS) {
6678 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006679 }
6680
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 if (other_secret != NULL) {
6682 status = psa_key_derivation_input_bytes(derivation,
6683 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6684 other_secret, other_secret_length);
6685 if (status != PSA_SUCCESS) {
6686 return status;
6687 }
6688 }
6689
6690 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006691 status = psa_key_derivation_input_bytes(
6692 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 raw_psk, raw_psk_length);
6694 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006695 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006697 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if (status != PSA_SUCCESS) {
6699 return status;
6700 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 status = psa_key_derivation_input_bytes(derivation,
6703 PSA_KEY_DERIVATION_INPUT_LABEL,
6704 label, label_length);
6705 if (status != PSA_SUCCESS) {
6706 return status;
6707 }
6708 } else {
6709 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006710 }
6711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 status = psa_key_derivation_set_capacity(derivation, capacity);
6713 if (status != PSA_SUCCESS) {
6714 return status;
6715 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006718}
6719
Andrzej Kurek57d10632022-10-24 10:32:01 -04006720#if defined(PSA_WANT_ALG_SHA_384) || \
6721 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006722MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006723static int tls_prf_generic(mbedtls_md_type_t md_type,
6724 const unsigned char *secret, size_t slen,
6725 const char *label,
6726 const unsigned char *random, size_t rlen,
6727 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006728{
6729 psa_status_t status;
6730 psa_algorithm_t alg;
6731 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6732 psa_key_derivation_operation_t derivation =
6733 PSA_KEY_DERIVATION_OPERATION_INIT;
6734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006736 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006738 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006740
6741 /* Normally a "secret" should be long enough to be impossible to
6742 * find by brute force, and in particular should not be empty. But
6743 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6744 * and for this use case it makes sense to have a 0-length "secret".
6745 * Since the key API doesn't allow importing a key of length 0,
6746 * keep master_key=0, which setup_psa_key_derivation() understands
6747 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006749 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6751 psa_set_key_algorithm(&key_attributes, alg);
6752 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006753
Gilles Peskine449bd832023-01-11 14:50:10 +01006754 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6755 if (status != PSA_SUCCESS) {
6756 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6757 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006758 }
6759
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 status = setup_psa_key_derivation(&derivation,
6761 master_key, alg,
6762 NULL, 0,
6763 random, rlen,
6764 (unsigned char const *) label,
6765 (size_t) strlen(label),
6766 NULL, 0,
6767 dlen);
6768 if (status != PSA_SUCCESS) {
6769 psa_key_derivation_abort(&derivation);
6770 psa_destroy_key(master_key);
6771 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006772 }
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6775 if (status != PSA_SUCCESS) {
6776 psa_key_derivation_abort(&derivation);
6777 psa_destroy_key(master_key);
6778 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006779 }
6780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 status = psa_key_derivation_abort(&derivation);
6782 if (status != PSA_SUCCESS) {
6783 psa_destroy_key(master_key);
6784 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006785 }
6786
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 if (!mbedtls_svc_key_id_is_null(master_key)) {
6788 status = psa_destroy_key(master_key);
6789 }
6790 if (status != PSA_SUCCESS) {
6791 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6792 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006795}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006796#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006797#else /* MBEDTLS_USE_PSA_CRYPTO */
6798
Andrzej Kurek57d10632022-10-24 10:32:01 -04006799#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006800 (defined(MBEDTLS_MD_CAN_SHA256) || \
6801 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006802MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006803static int tls_prf_generic(mbedtls_md_type_t md_type,
6804 const unsigned char *secret, size_t slen,
6805 const char *label,
6806 const unsigned char *random, size_t rlen,
6807 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006808{
6809 size_t nb;
6810 size_t i, j, k, md_len;
6811 unsigned char *tmp;
6812 size_t tmp_len = 0;
6813 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6814 const mbedtls_md_info_t *md_info;
6815 mbedtls_md_context_t md_ctx;
6816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6817
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6821 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6822 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006825
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 tmp_len = md_len + strlen(label) + rlen;
6827 tmp = mbedtls_calloc(1, tmp_len);
6828 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006829 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6830 goto exit;
6831 }
6832
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 nb = strlen(label);
6834 memcpy(tmp + md_len, label, nb);
6835 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006836 nb += rlen;
6837
6838 /*
6839 * Compute P_<hash>(secret, label + random)[0..dlen]
6840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006843 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6846 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006847 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 }
6849 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6850 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006851 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 }
6853 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6854 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006855 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 for (i = 0; i < dlen; i += md_len) {
6859 ret = mbedtls_md_hmac_reset(&md_ctx);
6860 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006861 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 }
6863 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6864 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006865 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 }
6867 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6868 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006869 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 ret = mbedtls_md_hmac_reset(&md_ctx);
6873 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006874 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 }
6876 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6877 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006878 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 }
6880 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6881 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006882 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006888 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006890 }
6891
6892exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006894
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 if (tmp != NULL) {
6896 mbedtls_platform_zeroize(tmp, tmp_len);
6897 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006900
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006904}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006905#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006906#endif /* MBEDTLS_USE_PSA_CRYPTO */
6907
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006908#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006910static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6911 const char *label,
6912 const unsigned char *random, size_t rlen,
6913 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006914{
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6916 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006917}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006918#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006919
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006920#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006921MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006922static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6923 const char *label,
6924 const unsigned char *random, size_t rlen,
6925 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006926{
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6928 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006929}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006930#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006931
Jerry Yuf009d862022-02-17 14:01:37 +08006932/*
6933 * Set appropriate PRF function and other SSL / TLS1.2 functions
6934 *
6935 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006936 * - hash associated with the ciphersuite (only used by TLS 1.2)
6937 *
6938 * Outputs:
6939 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6940 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006941MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006942static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6943 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006944{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006945#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006947 handshake->tls_prf = tls_prf_sha384;
6948 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6949 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006951#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006952#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006953 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006954 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006955 handshake->tls_prf = tls_prf_sha256;
6956 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6957 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6958 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006959#else
Jerry Yuf009d862022-02-17 14:01:37 +08006960 {
Jerry Yuf009d862022-02-17 14:01:37 +08006961 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006962 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006964 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006965#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006966
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006968}
Jerry Yud6ab2352022-02-17 14:03:43 +08006969
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006970/*
6971 * Compute master secret if needed
6972 *
6973 * Parameters:
6974 * [in/out] handshake
6975 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6976 * (PSA-PSK) ciphersuite_info, psk_opaque
6977 * [out] premaster (cleared)
6978 * [out] master
6979 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6980 * debug: conf->f_dbg, conf->p_dbg
6981 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006982 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006983 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006984MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006985static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6986 unsigned char *master,
6987 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006988{
6989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6990
6991 /* cf. RFC 5246, Section 8.1:
6992 * "The master secret is always exactly 48 bytes in length." */
6993 size_t const master_secret_len = 48;
6994
6995#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6996 unsigned char session_hash[48];
6997#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6998
6999 /* The label for the KDF used for key expansion.
7000 * This is either "master secret" or "extended master secret"
7001 * depending on whether the Extended Master Secret extension
7002 * is used. */
7003 char const *lbl = "master secret";
7004
Przemek Stekielae4ed302022-04-05 17:15:55 +02007005 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007006 * - If the Extended Master Secret extension is not used,
7007 * this is ClientHello.Random + ServerHello.Random
7008 * (see Sect. 8.1 in RFC 5246).
7009 * - If the Extended Master Secret extension is used,
7010 * this is the transcript of the handshake so far.
7011 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007012 unsigned char const *seed = handshake->randbytes;
7013 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007014
7015#if !defined(MBEDTLS_DEBUG_C) && \
7016 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7017 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007019 ssl = NULL; /* make sure we don't use it except for those cases */
7020 (void) ssl;
7021#endif
7022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (handshake->resume != 0) {
7024 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7025 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007026 }
7027
7028#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007030 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007031 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007032 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7033 if (ret != 0) {
7034 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7035 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7038 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007039 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007040#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007041
Przemek Stekiel99114f32022-04-22 11:20:09 +02007042#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007043 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007045 /* Perform PSK-to-MS expansion in a single step. */
7046 psa_status_t status;
7047 psa_algorithm_t alg;
7048 mbedtls_svc_key_id_t psk;
7049 psa_key_derivation_operation_t derivation =
7050 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007051 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007054
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007058 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007060 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007062
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007063 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007067 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007068 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007069 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007070 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007071 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007072 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007073 other_secret_len = 48;
7074 other_secret = handshake->premaster + 2;
7075 break;
7076 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007077 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007078 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7079 other_secret = handshake->premaster + 2;
7080 break;
7081 default:
7082 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007083 }
7084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 status = setup_psa_key_derivation(&derivation, psk, alg,
7086 ssl->conf->psk, ssl->conf->psk_len,
7087 seed, seed_len,
7088 (unsigned char const *) lbl,
7089 (size_t) strlen(lbl),
7090 other_secret, other_secret_len,
7091 master_secret_len);
7092 if (status != PSA_SUCCESS) {
7093 psa_key_derivation_abort(&derivation);
7094 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007095 }
7096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 status = psa_key_derivation_output_bytes(&derivation,
7098 master,
7099 master_secret_len);
7100 if (status != PSA_SUCCESS) {
7101 psa_key_derivation_abort(&derivation);
7102 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007103 }
7104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 status = psa_key_derivation_abort(&derivation);
7106 if (status != PSA_SUCCESS) {
7107 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7108 }
7109 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007110#endif
7111 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007112#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7114 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007115 psa_status_t status;
7116 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7117 psa_key_derivation_operation_t derivation =
7118 PSA_KEY_DERIVATION_OPERATION_INIT;
7119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007121
7122 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7123
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 status = psa_key_derivation_setup(&derivation, alg);
7125 if (status != PSA_SUCCESS) {
7126 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007127 }
7128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 status = psa_key_derivation_set_capacity(&derivation,
7130 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7131 if (status != PSA_SUCCESS) {
7132 psa_key_derivation_abort(&derivation);
7133 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007134 }
7135
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7137 &derivation);
7138 if (status != PSA_SUCCESS) {
7139 psa_key_derivation_abort(&derivation);
7140 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007141 }
7142
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 status = psa_key_derivation_output_bytes(&derivation,
7144 handshake->premaster,
7145 handshake->pmslen);
7146 if (status != PSA_SUCCESS) {
7147 psa_key_derivation_abort(&derivation);
7148 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7149 }
7150
7151 status = psa_key_derivation_abort(&derivation);
7152 if (status != PSA_SUCCESS) {
7153 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007154 }
7155 }
7156#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7158 lbl, seed, seed_len,
7159 master,
7160 master_secret_len);
7161 if (ret != 0) {
7162 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7163 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007164 }
7165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7167 handshake->premaster,
7168 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 mbedtls_platform_zeroize(handshake->premaster,
7171 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007172 }
7173
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007175}
7176
Gilles Peskine449bd832023-01-11 14:50:10 +01007177int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007178{
7179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7180 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7181 ssl->handshake->ciphersuite_info;
7182
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007184
7185 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007187 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 if (ret != 0) {
7189 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7190 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007191 }
7192
7193 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 ret = ssl_compute_master(ssl->handshake,
7195 ssl->session_negotiate->master,
7196 ssl);
7197 if (ret != 0) {
7198 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7199 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007200 }
7201
7202 /* Swap the client and server random values:
7203 * - MS derivation wanted client+server (RFC 5246 8.1)
7204 * - key derivation wants server+client (RFC 5246 6.3) */
7205 {
7206 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 memcpy(tmp, ssl->handshake->randbytes, 64);
7208 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7209 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7210 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007211 }
7212
7213 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7215 ssl->session_negotiate->ciphersuite,
7216 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007217#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007219#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 ssl->handshake->tls_prf,
7221 ssl->handshake->randbytes,
7222 ssl->tls_version,
7223 ssl->conf->endpoint,
7224 ssl);
7225 if (ret != 0) {
7226 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7227 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007228 }
7229
7230 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7232 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007233
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007237}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007240{
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007242#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007243 case MBEDTLS_SSL_HASH_SHA384:
7244 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7245 break;
7246#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007247#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007248 case MBEDTLS_SSL_HASH_SHA256:
7249 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7250 break;
7251#endif
7252 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007254 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007255#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7256 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007257 (void) ssl;
7258#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007260}
7261
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007262#if defined(MBEDTLS_USE_PSA_CRYPTO)
7263static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7264 const psa_hash_operation_t *hs_op,
7265 size_t buffer_size,
7266 unsigned char *hash,
7267 size_t *hlen)
7268{
7269 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007270 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007271
7272#if !defined(MBEDTLS_DEBUG_C)
7273 (void) ssl;
7274#endif
7275 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007276 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007277 if (status != PSA_SUCCESS) {
7278 goto exit;
7279 }
7280
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007281 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007282 if (status != PSA_SUCCESS) {
7283 goto exit;
7284 }
7285
7286 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7287 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7288
7289exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007290 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007291 return mbedtls_md_error_from_psa(status);
7292}
7293#else
7294static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7295 const mbedtls_md_context_t *hs_ctx,
7296 unsigned char *hash,
7297 size_t *hlen)
7298{
7299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007300 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007301
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007302 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007303
7304#if !defined(MBEDTLS_DEBUG_C)
7305 (void) ssl;
7306#endif
7307 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7308
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007309 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007310 if (ret != 0) {
7311 goto exit;
7312 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007313 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007314 if (ret != 0) {
7315 goto exit;
7316 }
7317
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007318 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007319 if (ret != 0) {
7320 goto exit;
7321 }
7322
7323 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7324
7325 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7326 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7327
7328exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007329 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007330 return ret;
7331}
7332#endif /* MBEDTLS_USE_PSA_CRYPTO */
7333
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007334#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007335int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7336 unsigned char *hash,
7337 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007338{
7339#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007340 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7341 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007342#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007343 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7344 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007345#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007346}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007347#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007348
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007349#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007350int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7351 unsigned char *hash,
7352 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007353{
7354#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007355 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7356 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007357#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007358 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7359 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007360#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007361}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007362#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007363
Neil Armstrong80f6f322022-05-03 17:56:38 +02007364#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7365 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007366int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007367{
7368 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007370 const unsigned char *psk = NULL;
7371 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007373
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007375 /*
7376 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007377 * checked before calling this function.
7378 *
7379 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007380 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7383 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7384 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007385 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007386 }
7387
7388 /*
7389 * PMS = struct {
7390 * opaque other_secret<0..2^16-1>;
7391 * opaque psk<0..2^16-1>;
7392 * };
7393 * with "other_secret" depending on the particular key exchange
7394 */
7395#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7397 if (end - p < 2) {
7398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7399 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007402 p += 2;
7403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if (end < p || (size_t) (end - p) < psk_len) {
7405 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7406 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007409 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007411#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7412#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007414 /*
7415 * other_secret already set by the ClientKeyExchange message,
7416 * and is 48 bytes long
7417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 if (end - p < 2) {
7419 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7420 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007421
7422 *p++ = 0;
7423 *p++ = 48;
7424 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007426#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7427#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7430 size_t len;
7431
7432 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007434 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7437 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007438 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007440 p += 2 + len;
7441
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7443 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007444#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007445#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7448 size_t zlen;
7449
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007451 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7453 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7454 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007455 }
7456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007458 p += 2 + zlen;
7459
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7461 MBEDTLS_DEBUG_ECDH_Z);
7462 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007463#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007464 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7466 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007467 }
7468
7469 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 if (end - p < 2) {
7471 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7472 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007473
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007475 p += 2;
7476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (end < p || (size_t) (end - p) < psk_len) {
7478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7479 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007480
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007482 p += psk_len;
7483
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007484 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007485
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007487}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007488#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007489
7490#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007491MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007492static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007493
7494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007495int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007496{
7497 /* If renegotiation is not enforced, retransmit until we would reach max
7498 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007500 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7501 unsigned char doublings = 1;
7502
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007504 ++doublings;
7505 ratio >>= 1;
7506 }
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 if (++ssl->renego_records_seen > doublings) {
7509 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7510 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007511 }
7512 }
7513
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007515}
7516#endif
7517#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007518
Jerry Yud9526692022-02-17 14:23:47 +08007519/*
7520 * Handshake functions
7521 */
7522#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7523/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007524int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007525{
7526 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7527 ssl->handshake->ciphersuite_info;
7528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7532 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007533 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007535 }
7536
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7538 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007539}
7540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007542{
7543 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7544 ssl->handshake->ciphersuite_info;
7545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007547
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7549 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007550 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007552 }
7553
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7555 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007556}
7557
7558#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7559/* Some certificate support -> implement write and parse */
7560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007562{
7563 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7564 size_t i, n;
7565 const mbedtls_x509_crt *crt;
7566 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7567 ssl->handshake->ciphersuite_info;
7568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007570
Gilles Peskine449bd832023-01-11 14:50:10 +01007571 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7572 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007573 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007575 }
7576
7577#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7579 if (ssl->handshake->client_auth == 0) {
7580 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007581 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007583 }
7584 }
7585#endif /* MBEDTLS_SSL_CLI_C */
7586#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7588 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007589 /* Should never happen because we shouldn't have picked the
7590 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007592 }
7593 }
7594#endif
7595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007597
7598 /*
7599 * 0 . 0 handshake type
7600 * 1 . 3 handshake length
7601 * 4 . 6 length of all certs
7602 * 7 . 9 length of cert. 1
7603 * 10 . n-1 peer certificate
7604 * n . n+2 length of cert. 2
7605 * n+3 . ... upper level cert, etc.
7606 */
7607 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007611 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7613 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7614 " > %" MBEDTLS_PRINTF_SIZET,
7615 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7616 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007617 }
7618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7620 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7621 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007622
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007624 i += n; crt = crt->next;
7625 }
7626
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7628 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7629 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007630
7631 ssl->out_msglen = i;
7632 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7633 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7634
7635 ssl->state++;
7636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7638 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7639 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007640 }
7641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007643
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007645}
7646
7647#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7648
7649#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007651static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7652 unsigned char *crt_buf,
7653 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007654{
7655 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 if (peer_crt == NULL) {
7658 return -1;
7659 }
Jerry Yud9526692022-02-17 14:23:47 +08007660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 if (peer_crt->raw.len != crt_buf_len) {
7662 return -1;
7663 }
Jerry Yud9526692022-02-17 14:23:47 +08007664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007666}
7667#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007669static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7670 unsigned char *crt_buf,
7671 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007672{
7673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7674 unsigned char const * const peer_cert_digest =
7675 ssl->session->peer_cert_digest;
7676 mbedtls_md_type_t const peer_cert_digest_type =
7677 ssl->session->peer_cert_digest_type;
7678 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007680 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7681 size_t digest_len;
7682
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 if (peer_cert_digest == NULL || digest_info == NULL) {
7684 return -1;
7685 }
Jerry Yud9526692022-02-17 14:23:47 +08007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 digest_len = mbedtls_md_get_size(digest_info);
7688 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7689 return -1;
7690 }
Jerry Yud9526692022-02-17 14:23:47 +08007691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7693 if (ret != 0) {
7694 return -1;
7695 }
Jerry Yud9526692022-02-17 14:23:47 +08007696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007698}
7699#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7700#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7701
7702/*
7703 * Once the certificate message is read, parse it into a cert chain and
7704 * perform basic checks, but leave actual verification to the caller
7705 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007707static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7708 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007709{
7710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7711#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007713#endif
7714 size_t i, n;
7715 uint8_t alert;
7716
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7718 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7719 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7720 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7721 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007722 }
7723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7725 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7726 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7727 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007728 }
7729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7731 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7732 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7733 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7734 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007735 }
7736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007738
7739 /*
7740 * Same message structure as in mbedtls_ssl_write_certificate()
7741 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007742 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 if (ssl->in_msg[i] != 0 ||
7745 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7746 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7747 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7748 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7749 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007750 }
7751
7752 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7753 i += 3;
7754
7755 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007756 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007757 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 if (i + 3 > ssl->in_hslen) {
7759 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7760 mbedtls_ssl_send_alert_message(ssl,
7761 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7762 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7763 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007764 }
7765 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7766 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 if (ssl->in_msg[i] != 0) {
7768 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7769 mbedtls_ssl_send_alert_message(ssl,
7770 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7771 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7772 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007773 }
7774
7775 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007776 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007777 i += 3;
7778
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 if (n < 128 || i + n > ssl->in_hslen) {
7780 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7781 mbedtls_ssl_send_alert_message(ssl,
7782 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7783 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7784 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007785 }
7786
7787 /* Check if we're handling the first CRT in the chain. */
7788#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007790 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007792 /* During client-side renegotiation, check that the server's
7793 * end-CRTs hasn't changed compared to the initial handshake,
7794 * mitigating the triple handshake attack. On success, reuse
7795 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7797 if (ssl_check_peer_crt_unchanged(ssl,
7798 &ssl->in_msg[i],
7799 n) != 0) {
7800 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7801 mbedtls_ssl_send_alert_message(ssl,
7802 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7803 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7804 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007805 }
7806
7807 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007809 }
7810#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7811
7812 /* Parse the next certificate in the chain. */
7813#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007815#else
7816 /* If we don't need to store the CRT chain permanently, parse
7817 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007818 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007819#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007820 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007821 case 0: /*ok*/
7822 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7823 /* Ignore certificate with an unknown algorithm: maybe a
7824 prior certificate was already trusted. */
7825 break;
7826
7827 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7828 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7829 goto crt_parse_der_failed;
7830
7831 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7832 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7833 goto crt_parse_der_failed;
7834
7835 default:
7836 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007837crt_parse_der_failed:
7838 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7839 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7840 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007841 }
7842
7843 i += n;
7844 }
7845
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7847 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007848}
7849
7850#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007851MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007852static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007853{
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7855 return -1;
7856 }
Jerry Yud9526692022-02-17 14:23:47 +08007857
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007859 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7860 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7862 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7863 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007864 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007866}
7867#endif /* MBEDTLS_SSL_SRV_C */
7868
7869/* Check if a certificate message is expected.
7870 * Return either
7871 * - SSL_CERTIFICATE_EXPECTED, or
7872 * - SSL_CERTIFICATE_SKIP
7873 * indicating whether a Certificate message is expected or not.
7874 */
7875#define SSL_CERTIFICATE_EXPECTED 0
7876#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007877MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007878static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7879 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007880{
7881 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7882 ssl->handshake->ciphersuite_info;
7883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7885 return SSL_CERTIFICATE_SKIP;
7886 }
Jerry Yud9526692022-02-17 14:23:47 +08007887
7888#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7890 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7891 return SSL_CERTIFICATE_SKIP;
7892 }
Jerry Yud9526692022-02-17 14:23:47 +08007893
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007895 ssl->session_negotiate->verify_result =
7896 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007898 }
7899 }
7900#else
7901 ((void) authmode);
7902#endif /* MBEDTLS_SSL_SRV_C */
7903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007905}
7906
Jerry Yud9526692022-02-17 14:23:47 +08007907#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007909static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7910 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007911{
7912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7913 /* Remember digest of the peer's end-CRT. */
7914 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7916 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7917 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7918 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7919 mbedtls_ssl_send_alert_message(ssl,
7920 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7921 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007924 }
7925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 ret = mbedtls_md(mbedtls_md_info_from_type(
7927 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7928 start, len,
7929 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007930
7931 ssl->session_negotiate->peer_cert_digest_type =
7932 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7933 ssl->session_negotiate->peer_cert_digest_len =
7934 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007937}
7938
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007939MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007940static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7941 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007942{
7943 unsigned char *end = start + len;
7944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7945
7946 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7948 ret = mbedtls_pk_parse_subpubkey(&start, end,
7949 &ssl->handshake->peer_pubkey);
7950 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007951 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007953 }
7954
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007956}
7957#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7958
Gilles Peskine449bd832023-01-11 14:50:10 +01007959int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007960{
7961 int ret = 0;
7962 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02007963 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7965 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7966 ? ssl->handshake->sni_authmode
7967 : ssl->conf->authmode;
7968#else
7969 const int authmode = ssl->conf->authmode;
7970#endif
7971 void *rs_ctx = NULL;
7972 mbedtls_x509_crt *chain = NULL;
7973
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007975
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7977 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7978 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007979 goto exit;
7980 }
7981
7982#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 if (ssl->handshake->ecrs_enabled &&
7984 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007985 chain = ssl->handshake->ecrs_peer_cert;
7986 ssl->handshake->ecrs_peer_cert = NULL;
7987 goto crt_verify;
7988 }
7989#endif
7990
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007992 /* mbedtls_ssl_read_record may have sent an alert already. We
7993 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007995 goto exit;
7996 }
7997
7998#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008000 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08008003 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 }
Jerry Yud9526692022-02-17 14:23:47 +08008005
8006 goto exit;
8007 }
8008#endif /* MBEDTLS_SSL_SRV_C */
8009
8010 /* Clear existing peer CRT structure in case we tried to
8011 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008013
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8015 if (chain == NULL) {
8016 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8017 sizeof(mbedtls_x509_crt)));
8018 mbedtls_ssl_send_alert_message(ssl,
8019 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8020 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008021
8022 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8023 goto exit;
8024 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008026
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 ret = ssl_parse_certificate_chain(ssl, chain);
8028 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 }
Jerry Yud9526692022-02-17 14:23:47 +08008031
8032#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008033 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008034 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 }
Jerry Yud9526692022-02-17 14:23:47 +08008036
8037crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008039 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 }
Jerry Yud9526692022-02-17 14:23:47 +08008041#endif
8042
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008043 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8044 ssl->handshake->ciphersuite_info,
8045 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008046 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008047 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 }
Jerry Yud9526692022-02-17 14:23:47 +08008049
8050#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8051 {
8052 unsigned char *crt_start, *pk_start;
8053 size_t crt_len, pk_len;
8054
8055 /* We parse the CRT chain without copying, so
8056 * these pointers point into the input buffer,
8057 * and are hence still valid after freeing the
8058 * CRT chain. */
8059
8060 crt_start = chain->raw.p;
8061 crt_len = chain->raw.len;
8062
8063 pk_start = chain->pk_raw.p;
8064 pk_len = chain->pk_raw.len;
8065
8066 /* Free the CRT structures before computing
8067 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 mbedtls_x509_crt_free(chain);
8069 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008070 chain = NULL;
8071
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_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
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8078 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008080 }
Jerry Yud9526692022-02-17 14:23:47 +08008081 }
8082#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8083 /* Pass ownership to session structure. */
8084 ssl->session_negotiate->peer_cert = chain;
8085 chain = NULL;
8086#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8087
Gilles Peskine449bd832023-01-11 14:50:10 +01008088 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008089
8090exit:
8091
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008093 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 }
Jerry Yud9526692022-02-17 14:23:47 +08008095
8096#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008097 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008098 ssl->handshake->ecrs_peer_cert = chain;
8099 chain = NULL;
8100 }
8101#endif
8102
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 if (chain != NULL) {
8104 mbedtls_x509_crt_free(chain);
8105 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008106 }
8107
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008109}
8110#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8111
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008112static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8113 unsigned char *padbuf, size_t hlen,
8114 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008115{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008116 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008117 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008119 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008120 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008121 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008122 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008123#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008125 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008126 mbedtls_md_context_t cloned_ctx;
8127 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008128#endif
8129
8130 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008132 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008133 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008134
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008136 ? "client finished"
8137 : "server finished";
8138
8139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008140 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008141
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008142 status = psa_hash_clone(hs_op, &cloned_op);
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 }
8146
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008147 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008149 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008150 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008151 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008152#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008153 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008154
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008155 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008156 if (ret != 0) {
8157 goto exit;
8158 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008159 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008160 if (ret != 0) {
8161 goto exit;
8162 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008163
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008164 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008165 if (ret != 0) {
8166 goto exit;
8167 }
8168#endif /* MBEDTLS_USE_PSA_CRYPTO */
8169
8170 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8171
Jerry Yu615bd6f2022-02-17 14:25:15 +08008172 /*
8173 * TLSv1.2:
8174 * hash = PRF( master, finished_label,
8175 * Hash( handshake ) )[0.11]
8176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008178 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008179
Gilles Peskine449bd832023-01-11 14:50:10 +01008180 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008181
Paul Elliottecb95be2023-08-11 16:41:04 +01008182 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008183
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008184 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008185
8186exit:
8187#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008188 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008189 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008190#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008191 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008192 return ret;
8193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008194}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008195
8196#if defined(MBEDTLS_MD_CAN_SHA256)
8197static int ssl_calc_finished_tls_sha256(
8198 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8199{
8200 unsigned char padbuf[32];
8201 return ssl_calc_finished_tls_generic(ssl,
8202#if defined(MBEDTLS_USE_PSA_CRYPTO)
8203 &ssl->handshake->fin_sha256_psa,
8204#else
8205 &ssl->handshake->fin_sha256,
8206#endif
8207 padbuf, sizeof(padbuf),
8208 buf, from);
8209}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008210#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008211
Jerry Yub7ba49e2022-02-17 14:25:53 +08008212
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008213#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008214static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008216{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008217 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008218 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008220 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008221#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008222 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008223#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008224 padbuf, sizeof(padbuf),
8225 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008226}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008227#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008228
Gilles Peskine449bd832023-01-11 14:50:10 +01008229void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008230{
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008232
8233 /*
8234 * Free our handshake params
8235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 mbedtls_ssl_handshake_free(ssl);
8237 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008238 ssl->handshake = NULL;
8239
8240 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008241 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 if (ssl->transform) {
8244 mbedtls_ssl_transform_free(ssl->transform);
8245 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008246 }
8247 ssl->transform = ssl->transform_negotiate;
8248 ssl->transform_negotiate = NULL;
8249
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008251}
8252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008254{
8255 int resume = ssl->handshake->resume;
8256
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008258
8259#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008261 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8262 ssl->renego_records_seen = 0;
8263 }
8264#endif
8265
8266 /*
8267 * Free the previous session and switch in the current one
8268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008270#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8271 /* RFC 7366 3.1: keep the EtM state */
8272 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008274#endif
8275
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 mbedtls_ssl_session_free(ssl->session);
8277 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008278 }
8279 ssl->session = ssl->session_negotiate;
8280 ssl->session_negotiate = NULL;
8281
8282 /*
8283 * Add cache entry
8284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008286 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 resume == 0) {
8288 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8289 ssl->session->id,
8290 ssl->session->id_len,
8291 ssl->session) != 0) {
8292 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8293 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008294 }
8295
8296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8298 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008299 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008301
8302 /* Keep last flight around in case we need to resend it:
8303 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8305 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008306#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008308
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008309 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008312}
8313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008315{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008316 int ret;
8317 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008318
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008320
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008322
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008323 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8324 if (ret != 0) {
8325 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8326 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008327
8328 /*
8329 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8330 * may define some other value. Currently (early 2016), no defined
8331 * ciphersuite does this (and this is unlikely to change as activity has
8332 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8333 */
8334 hash_len = 12;
8335
8336#if defined(MBEDTLS_SSL_RENEGOTIATION)
8337 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008339#endif
8340
8341 ssl->out_msglen = 4 + hash_len;
8342 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8343 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8344
8345 /*
8346 * In case of session resuming, invert the client and server
8347 * ChangeCipherSpec messages order.
8348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008350#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008352 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008354#endif
8355#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008357 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008359#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008361 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008363
8364 /*
8365 * Switch to our negotiated transform and session parameters for outbound
8366 * data.
8367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008369
8370#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008372 unsigned char i;
8373
8374 /* Remember current epoch settings for resending */
8375 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8377 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008378
8379 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008381
8382
8383 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 for (i = 2; i > 0; i--) {
8385 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008386 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 }
8388 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008389
8390 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 if (i == 0) {
8392 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8393 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008394 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008398
8399 ssl->transform_out = ssl->transform_negotiate;
8400 ssl->session_out = ssl->session_negotiate;
8401
8402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8404 mbedtls_ssl_send_flight_completed(ssl);
8405 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008406#endif
8407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8409 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8410 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008411 }
8412
8413#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8415 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8416 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8417 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008418 }
8419#endif
8420
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008424}
8425
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008426#define SSL_MAX_HASH_LEN 12
8427
Gilles Peskine449bd832023-01-11 14:50:10 +01008428int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008429{
8430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8431 unsigned int hash_len = 12;
8432 unsigned char buf[SSL_MAX_HASH_LEN];
8433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008435
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008436 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8437 if (ret != 0) {
8438 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8439 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8442 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008443 goto exit;
8444 }
8445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8447 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8448 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8449 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008450 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8451 goto exit;
8452 }
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8455 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8456 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008457 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8458 goto exit;
8459 }
8460
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8462 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8463 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8464 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008465 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8466 goto exit;
8467 }
8468
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8470 buf, hash_len) != 0) {
8471 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8472 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8473 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008474 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8475 goto exit;
8476 }
8477
8478#if defined(MBEDTLS_SSL_RENEGOTIATION)
8479 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008481#endif
8482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008484#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008486 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008488#endif
8489#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008491 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008493#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008495 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008497
8498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8500 mbedtls_ssl_recv_flight_completed(ssl);
8501 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008502#endif
8503
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008505
8506exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 mbedtls_platform_zeroize(buf, hash_len);
8508 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008509}
8510
Jerry Yu392112c2022-02-17 14:34:10 +08008511#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8512/*
8513 * Helper to get TLS 1.2 PRF from ciphersuite
8514 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008516static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008517{
Jerry Yu392112c2022-02-17 14:34:10 +08008518 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008520#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8522 return tls_prf_sha384;
8523 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008524#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008525#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008526 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8528 return tls_prf_sha256;
8529 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008530 }
8531#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008532#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8533 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008534 (void) ciphersuite_info;
8535#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008538}
8539#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008542{
8543 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008544#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 if (tls_prf == tls_prf_sha384) {
8546 return MBEDTLS_SSL_TLS_PRF_SHA384;
8547 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008548#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008549#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 if (tls_prf == tls_prf_sha256) {
8551 return MBEDTLS_SSL_TLS_PRF_SHA256;
8552 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008553#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008555}
8556
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008557/*
8558 * Populate a transform structure with session keys and all the other
8559 * necessary information.
8560 *
8561 * Parameters:
8562 * - [in/out]: transform: structure to populate
8563 * [in] must be just initialised with mbedtls_ssl_transform_init()
8564 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8565 * - [in] ciphersuite
8566 * - [in] master
8567 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008568 * - [in] tls_prf: pointer to PRF to use for key derivation
8569 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008570 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008571 * - [in] endpoint: client or server
8572 * - [in] ssl: used for:
8573 * - ssl->conf->{f,p}_export_keys
8574 * [in] optionally used for:
8575 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8576 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008577MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008578static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8579 int ciphersuite,
8580 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008581#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008583#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 ssl_tls_prf_t tls_prf,
8585 const unsigned char randbytes[64],
8586 mbedtls_ssl_protocol_version tls_version,
8587 unsigned endpoint,
8588 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008589{
8590 int ret = 0;
8591 unsigned char keyblk[256];
8592 unsigned char *key1;
8593 unsigned char *key2;
8594 unsigned char *mac_enc;
8595 unsigned char *mac_dec;
8596 size_t mac_key_len = 0;
8597 size_t iv_copy_len;
8598 size_t keylen;
8599 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008600 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008601#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008602 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008603 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008604#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008605
8606#if defined(MBEDTLS_USE_PSA_CRYPTO)
8607 psa_key_type_t key_type;
8608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8609 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008610 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008611 size_t key_bits;
8612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8613#endif
8614
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008615 /*
8616 * Some data just needs copying into the structure
8617 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008618#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008619 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008620#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008621 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008622
8623#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008625#endif
8626
8627#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008629 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8630 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008632 }
8633#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8634
8635 /*
8636 * Get various info structures
8637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8639 if (ciphersuite_info == NULL) {
8640 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8641 ciphersuite));
8642 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008643 }
8644
Neil Armstrongab555e02022-04-04 11:07:59 +02008645 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008646#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008648#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008650
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008652 transform->taglen =
8653 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008655
8656#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008657 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 transform->taglen,
8659 &alg,
8660 &key_type,
8661 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008662 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008664 goto end;
8665 }
8666#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008667 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 if (cipher_info == NULL) {
8669 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8670 ciphersuite_info->cipher));
8671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008672 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008673#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008674
Neil Armstronge4512952022-03-08 09:08:22 +01008675#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008676 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008678 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 (unsigned) ciphersuite_info->mac));
8680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008681 }
8682#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008683 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 if (md_info == NULL) {
8685 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8686 (unsigned) ciphersuite_info->mac));
8687 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008688 }
Neil Armstronge4512952022-03-08 09:08:22 +01008689#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008690
8691#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8692 /* Copy own and peer's CID if the use of the CID
8693 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008694 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8695 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008696
8697 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8699 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8700 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008701
8702 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8704 ssl->handshake->peer_cid_len);
8705 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8706 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008707 }
8708#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8709
8710 /*
8711 * Compute key block using the PRF
8712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8714 if (ret != 0) {
8715 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8716 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008717 }
8718
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8720 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8721 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8722 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8723 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008724
8725 /*
8726 * Determine the appropriate key, IV and MAC length.
8727 */
8728
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008729#if defined(MBEDTLS_USE_PSA_CRYPTO)
8730 keylen = PSA_BITS_TO_BYTES(key_bits);
8731#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008733#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008734
Valerio Settie5707042023-10-11 11:54:42 +02008735#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008737 size_t explicit_ivlen;
8738
8739 transform->maclen = 0;
8740 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008741
8742 /* All modes haves 96-bit IVs, but the length of the static parts vary
8743 * with mode and version:
8744 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8745 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8746 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8747 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8748 * sequence number).
8749 */
8750 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008751
8752 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008753#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008754 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008755#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8757 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008758#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008761 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008763 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008765
8766 /* Minimum length of encrypted record */
8767 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8768 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 } else
Valerio Settie5707042023-10-11 11:54:42 +02008770#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008771#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008773 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008775#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008777#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008778 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008779#endif /* MBEDTLS_USE_PSA_CRYPTO */
8780
8781#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008782 /* Get MAC length */
8783 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8784#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008785 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8787 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8788 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008789 goto end;
8790 }
8791
8792 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008794#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008795 transform->maclen = mac_key_len;
8796
8797 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008798#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008800#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008801 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008802#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008803
8804 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008806 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008808 /*
8809 * GenericBlockCipher:
8810 * 1. if EtM is in use: one block plus MAC
8811 * otherwise: * first multiple of blocklen greater than maclen
8812 * 2. IV
8813 */
8814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008816 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 + block_size;
8818 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008819#endif
8820 {
8821 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 + block_size
8823 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008824 }
8825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008827 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 } else {
8829 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008830 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8831 goto end;
8832 }
8833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008835#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8836 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8838 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008839 }
8840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8842 (unsigned) keylen,
8843 (unsigned) transform->minlen,
8844 (unsigned) transform->ivlen,
8845 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008846
8847 /*
8848 * Finally setup the cipher contexts, IVs and MAC secrets.
8849 */
8850#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008852 key1 = keyblk + mac_key_len * 2;
8853 key2 = keyblk + mac_key_len * 2 + keylen;
8854
8855 mac_enc = keyblk;
8856 mac_dec = keyblk + mac_key_len;
8857
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 iv_copy_len = (transform->fixed_ivlen) ?
8859 transform->fixed_ivlen : transform->ivlen;
8860 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8861 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8862 iv_copy_len);
8863 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008864#endif /* MBEDTLS_SSL_CLI_C */
8865#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008867 key1 = keyblk + mac_key_len * 2 + keylen;
8868 key2 = keyblk + mac_key_len * 2;
8869
8870 mac_enc = keyblk + mac_key_len;
8871 mac_dec = keyblk;
8872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 iv_copy_len = (transform->fixed_ivlen) ?
8874 transform->fixed_ivlen : transform->ivlen;
8875 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8876 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8877 iv_copy_len);
8878 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008879#endif /* MBEDTLS_SSL_SRV_C */
8880 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008882 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8883 goto end;
8884 }
8885
Paul Elliott4fb19552023-10-18 12:15:30 +01008886 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 ssl->f_export_keys(ssl->p_export_keys,
8888 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8889 master, 48,
8890 randbytes + 32,
8891 randbytes,
8892 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008893 }
8894
8895#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008896 transform->psa_alg = alg;
8897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8899 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8900 psa_set_key_algorithm(&attributes, alg);
8901 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008902
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 if ((status = psa_import_key(&attributes,
8904 key1,
8905 PSA_BITS_TO_BYTES(key_bits),
8906 &transform->psa_key_enc)) != PSA_SUCCESS) {
8907 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008908 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008910 goto end;
8911 }
8912
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 if ((status = psa_import_key(&attributes,
8916 key2,
8917 PSA_BITS_TO_BYTES(key_bits),
8918 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008919 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008920 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008921 goto end;
8922 }
8923 }
8924#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8926 cipher_info)) != 0) {
8927 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008928 goto end;
8929 }
8930
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8932 cipher_info)) != 0) {
8933 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008934 goto end;
8935 }
8936
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8938 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8939 MBEDTLS_ENCRYPT)) != 0) {
8940 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008941 goto end;
8942 }
8943
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8945 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8946 MBEDTLS_DECRYPT)) != 0) {
8947 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008948 goto end;
8949 }
8950
8951#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8953 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8954 MBEDTLS_PADDING_NONE)) != 0) {
8955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008956 goto end;
8957 }
8958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8960 MBEDTLS_PADDING_NONE)) != 0) {
8961 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008962 goto end;
8963 }
8964 }
8965#endif /* MBEDTLS_CIPHER_MODE_CBC */
8966#endif /* MBEDTLS_USE_PSA_CRYPTO */
8967
Neil Armstrong29c0c042022-03-17 17:47:28 +01008968#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8969 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8970 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008971 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008972#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008974
Gilles Peskine449bd832023-01-11 14:50:10 +01008975 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8976 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8977 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 if ((status = psa_import_key(&attributes,
8980 mac_enc, mac_key_len,
8981 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008982 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008984 goto end;
8985 }
8986
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8988 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008989#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008991#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008993 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8995 PSA_KEY_USAGE_VERIFY_HASH);
8996 } else {
8997 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8998 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008999
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if ((status = psa_import_key(&attributes,
9001 mac_dec, mac_key_len,
9002 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009003 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009005 goto end;
9006 }
9007#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9009 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009010 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 }
9012 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9013 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009014 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009016#endif /* MBEDTLS_USE_PSA_CRYPTO */
9017 }
9018#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9019
9020 ((void) mac_dec);
9021 ((void) mac_enc);
9022
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009023end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9025 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009026}
9027
Valerio Settia08b1a42022-11-17 15:10:02 +01009028#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9029 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009030int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 psa_pake_operation_t *pake_ctx,
9032 const unsigned char *buf,
9033 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009034{
9035 psa_status_t status;
9036 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009037 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009038 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9039 * At round two perform a single cycle
9040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009042
Gilles Peskine449bd832023-01-11 14:50:10 +01009043 for (; remaining_steps > 0; remaining_steps--) {
9044 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009045 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009047 /* Length is stored at the first byte */
9048 size_t length = buf[input_offset];
9049 input_offset += 1;
9050
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009052 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9053 }
9054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 status = psa_pake_input(pake_ctx, step,
9056 buf + input_offset, length);
9057 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009058 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009059 }
9060
9061 input_offset += length;
9062 }
9063 }
9064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009066 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009070}
9071
Valerio Setti6b3dab02022-11-17 17:14:54 +01009072int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009073 psa_pake_operation_t *pake_ctx,
9074 unsigned char *buf,
9075 size_t len, size_t *olen,
9076 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009077{
9078 psa_status_t status;
9079 size_t output_offset = 0;
9080 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009081 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009082 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9083 * At round two perform a single cycle
9084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 for (; remaining_steps > 0; remaining_steps--) {
9088 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9089 step <= PSA_PAKE_STEP_ZK_PROOF;
9090 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009091 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009092 * For each step, prepend 1 byte with the length of the data as
9093 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 status = psa_pake_output(pake_ctx, step,
9096 buf + output_offset + 1,
9097 len - output_offset - 1,
9098 &output_len);
9099 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009100 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009101 }
9102
Valerio Setti99d88c12022-11-22 16:03:43 +01009103 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009104
9105 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009106 }
9107 }
9108
9109 *olen = output_offset;
9110
Gilles Peskine449bd832023-01-11 14:50:10 +01009111 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009112}
Valerio Settia08b1a42022-11-17 15:10:02 +01009113#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9114
Jerry Yuee40f9d2022-02-17 14:55:16 +08009115#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009116int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9117 unsigned char *hash, size_t *hashlen,
9118 unsigned char *data, size_t data_len,
9119 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009120{
9121 psa_status_t status;
9122 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009123 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009124
Gilles Peskine449bd832023-01-11 14:50:10 +01009125 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if ((status = psa_hash_setup(&hash_operation,
9128 hash_alg)) != PSA_SUCCESS) {
9129 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009130 goto exit;
9131 }
9132
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9134 64)) != PSA_SUCCESS) {
9135 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009136 goto exit;
9137 }
9138
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 if ((status = psa_hash_update(&hash_operation,
9140 data, data_len)) != PSA_SUCCESS) {
9141 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009142 goto exit;
9143 }
9144
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9146 hashlen)) != PSA_SUCCESS) {
9147 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9148 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009149 }
9150
9151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 if (status != PSA_SUCCESS) {
9153 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9154 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9155 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009156 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009158 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9159 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009161 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009163 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009165 }
9166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009168}
9169
9170#else
9171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9173 unsigned char *hash, size_t *hashlen,
9174 unsigned char *data, size_t data_len,
9175 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009176{
9177 int ret = 0;
9178 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9180 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009183
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009185
9186 /*
9187 * digitally-signed struct {
9188 * opaque client_random[32];
9189 * opaque server_random[32];
9190 * ServerDHParams params;
9191 * };
9192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9194 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009195 goto exit;
9196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9198 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009199 goto exit;
9200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9202 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009203 goto exit;
9204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9206 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009207 goto exit;
9208 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9210 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009211 goto exit;
9212 }
9213
9214exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009216
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 if (ret != 0) {
9218 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9219 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9220 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009221
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009223}
9224#endif /* MBEDTLS_USE_PSA_CRYPTO */
9225
Jerry Yud9d91da2022-02-17 14:57:06 +08009226#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9227
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009228/* Find the preferred hash for a given signature algorithm. */
9229unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 mbedtls_ssl_context *ssl,
9231 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009232{
Gabor Mezei078e8032022-04-27 21:17:56 +02009233 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009234 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009235
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9237 return MBEDTLS_SSL_HASH_NONE;
9238 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009241 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9243 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009244 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9246 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009247
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009248 mbedtls_md_type_t md_alg =
9249 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9250 if (md_alg == MBEDTLS_MD_NONE) {
9251 continue;
9252 }
9253
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009257 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009258 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009259
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9261 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9262 PSA_ALG_ECDSA(psa_hash_alg),
9263 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009264 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009266
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9268 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9269 PSA_ALG_RSA_PKCS1V15_SIGN(
9270 psa_hash_alg),
9271 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009272 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009274 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009275#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009276
Gilles Peskine449bd832023-01-11 14:50:10 +01009277 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009278 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009279 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009280
Gilles Peskine449bd832023-01-11 14:50:10 +01009281 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009282}
9283
9284#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9285
Jerry Yudc7bd172022-02-17 13:44:15 +08009286#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9287
XiaokangQian75d40ef2022-04-20 11:05:24 +00009288int mbedtls_ssl_validate_ciphersuite(
9289 const mbedtls_ssl_context *ssl,
9290 const mbedtls_ssl_ciphersuite_t *suite_info,
9291 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009293{
9294 (void) ssl;
9295
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 if (suite_info == NULL) {
9297 return -1;
9298 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009299
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 if ((suite_info->min_tls_version > max_tls_version) ||
9301 (suite_info->max_tls_version < min_tls_version)) {
9302 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009303 }
9304
XiaokangQian060d8672022-04-21 09:24:56 +00009305#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009306#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009307#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9309 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009310#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9312 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009313#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009314 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009316 }
9317#endif
9318
9319 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9320#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9322 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9323 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009324 }
9325#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9326#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9327
Gilles Peskine449bd832023-01-11 14:50:10 +01009328 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009329}
9330
Ronald Crone68ab4f2022-10-05 12:46:29 +02009331#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009332/*
9333 * Function for writing a signature algorithm extension.
9334 *
9335 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9336 * value (TLS 1.3 RFC8446):
9337 * enum {
9338 * ....
9339 * ecdsa_secp256r1_sha256( 0x0403 ),
9340 * ecdsa_secp384r1_sha384( 0x0503 ),
9341 * ecdsa_secp521r1_sha512( 0x0603 ),
9342 * ....
9343 * } SignatureScheme;
9344 *
9345 * struct {
9346 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9347 * } SignatureSchemeList;
9348 *
9349 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9350 * value (TLS 1.2 RFC5246):
9351 * enum {
9352 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9353 * sha512(6), (255)
9354 * } HashAlgorithm;
9355 *
9356 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9357 * SignatureAlgorithm;
9358 *
9359 * struct {
9360 * HashAlgorithm hash;
9361 * SignatureAlgorithm signature;
9362 * } SignatureAndHashAlgorithm;
9363 *
9364 * SignatureAndHashAlgorithm
9365 * supported_signature_algorithms<2..2^16-2>;
9366 *
9367 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9368 * generalization of the TLS 1.2 signature algorithm extension.
9369 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9370 * `SignatureScheme` field of TLS 1.3
9371 *
9372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009373int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9374 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009375{
9376 unsigned char *p = buf;
9377 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9378 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9379
9380 *out_len = 0;
9381
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009383
9384 /* Check if we have space for header and length field:
9385 * - extension_type (2 bytes)
9386 * - extension_data_length (2 bytes)
9387 * - supported_signature_algorithms_length (2 bytes)
9388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009390 p += 6;
9391
9392 /*
9393 * Write supported_signature_algorithms
9394 */
9395 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9397 if (sig_alg == NULL) {
9398 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9399 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009400
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9402 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9403 *sig_alg,
9404 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9405 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009406 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009407 }
9408 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9409 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009410 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9412 *sig_alg,
9413 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009414 }
9415
9416 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009417 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 if (supported_sig_alg_len == 0) {
9419 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9420 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009421 }
9422
Gilles Peskine449bd832023-01-11 14:50:10 +01009423 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9424 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9425 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009426
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009427 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009428
9429#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009431#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009432
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009434}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009435#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009436
XiaokangQian40a35232022-05-07 09:02:40 +00009437#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009438/*
9439 * mbedtls_ssl_parse_server_name_ext
9440 *
9441 * Structure of server_name extension:
9442 *
9443 * enum {
9444 * host_name(0), (255)
9445 * } NameType;
9446 * opaque HostName<1..2^16-1>;
9447 *
9448 * struct {
9449 * NameType name_type;
9450 * select (name_type) {
9451 * case host_name: HostName;
9452 * } name;
9453 * } ServerName;
9454 * struct {
9455 * ServerName server_name_list<1..2^16-1>
9456 * } ServerNameList;
9457 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009458MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009459int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9460 const unsigned char *buf,
9461 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009462{
9463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9464 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009465 size_t server_name_list_len, hostname_len;
9466 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009467
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009469
Gilles Peskine449bd832023-01-11 14:50:10 +01009470 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9471 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009472 p += 2;
9473
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009475 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 while (p < server_name_list_end) {
9477 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9478 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9479 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9480 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009481
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009483 /* sni_name is intended to be used only during the parsing of the
9484 * ClientHello message (it is reset to NULL before the end of
9485 * the message parsing). Thus it is ok to just point to the
9486 * reception buffer and not make a copy of it.
9487 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009488 ssl->handshake->sni_name = p + 3;
9489 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 if (ssl->conf->f_sni == NULL) {
9491 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009492 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9494 ssl, p + 3, hostname_len);
9495 if (ret != 0) {
9496 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9497 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9498 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9499 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9500 }
9501 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009502 }
9503
9504 p += hostname_len + 3;
9505 }
9506
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009508}
9509#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9510
XiaokangQianacb39922022-06-17 10:18:48 +00009511#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009512MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009513int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9514 const unsigned char *buf,
9515 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009516{
9517 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009518 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009519 const unsigned char *protocol_name_list;
9520 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009521 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009522
9523 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 if (ssl->conf->alpn_list == NULL) {
9525 return 0;
9526 }
XiaokangQianacb39922022-06-17 10:18:48 +00009527
9528 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009529 * RFC7301, section 3.1
9530 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009531 *
XiaokangQianc7403452022-06-23 03:24:12 +00009532 * struct {
9533 * ProtocolName protocol_name_list<2..2^16-1>
9534 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009535 */
9536
XiaokangQianc7403452022-06-23 03:24:12 +00009537 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009538 * protocol_name_list_len 2 bytes
9539 * protocol_name_len 1 bytes
9540 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009545 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009547 protocol_name_list = p;
9548 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009549
9550 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009552 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9554 protocol_name_len);
9555 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009556 MBEDTLS_SSL_PEND_FATAL_ALERT(
9557 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9559 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009560 }
9561
9562 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009563 }
9564
9565 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9567 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009568 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009570 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 if (protocol_name_len == alpn_len &&
9572 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009573 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009575 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009576
9577 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009578 }
9579 }
9580
XiaokangQian95d5f542022-06-24 02:29:26 +00009581 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009582 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009583 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9584 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9585 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009586}
9587
Gilles Peskine449bd832023-01-11 14:50:10 +01009588int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9589 unsigned char *buf,
9590 unsigned char *end,
9591 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009592{
9593 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009594 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009595 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009596
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 if (ssl->alpn_chosen == NULL) {
9598 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009599 }
9600
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 protocol_name_len = strlen(ssl->alpn_chosen);
9602 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009605 /*
9606 * 0 . 1 ext identifier
9607 * 2 . 3 ext length
9608 * 4 . 5 protocol list length
9609 * 6 . 6 protocol name length
9610 * 7 . 7+n protocol name
9611 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009613
XiaokangQian95d5f542022-06-24 02:29:26 +00009614 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009615
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9617 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009618 /* Note: the length of the chosen protocol has been checked to be less
9619 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9620 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009622
Gilles Peskine449bd832023-01-11 14:50:10 +01009623 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009624
9625#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009626 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009627#endif
9628
Gilles Peskine449bd832023-01-11 14:50:10 +01009629 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009630}
9631#endif /* MBEDTLS_SSL_ALPN */
9632
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009633#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009634 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009635 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009636 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009637int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9638 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009639{
9640 /* Initialize to suppress unnecessary compiler warning */
9641 size_t hostname_len = 0;
9642
9643 /* Check if new hostname is valid before
9644 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 if (hostname != NULL) {
9646 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009647
Gilles Peskine449bd832023-01-11 14:50:10 +01009648 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9649 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9650 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009651 }
9652
9653 /* Now it's clear that we will overwrite the old hostname,
9654 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009655 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009656 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009658 }
9659
9660 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009661 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009662 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 } else {
9664 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9665 if (session->hostname == NULL) {
9666 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9667 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009668
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009670 }
9671
Gilles Peskine449bd832023-01-11 14:50:10 +01009672 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009673}
Xiaokang Qian03409292022-10-12 02:49:52 +00009674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9675 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009676 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009677 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009678
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009679#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9680 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009681int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9682 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009683{
9684 size_t alpn_len = 0;
9685
9686 if (alpn != NULL) {
9687 alpn_len = strlen(alpn);
9688
9689 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9691 }
9692 }
9693
9694 if (session->ticket_alpn != NULL) {
9695 mbedtls_zeroize_and_free(session->ticket_alpn,
9696 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009697 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009698 }
9699
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009700 if (alpn != NULL) {
9701 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009702 if (session->ticket_alpn == NULL) {
9703 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9704 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009705 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009706 }
9707
9708 return 0;
9709}
9710#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009711
9712/*
9713 * The following functions are used by 1.2 and 1.3, client and server.
9714 */
9715#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9716int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9717 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9718 int recv_endpoint,
9719 mbedtls_ssl_protocol_version tls_version,
9720 uint32_t *flags)
9721{
9722 int ret = 0;
9723 unsigned int usage = 0;
9724 const char *ext_oid;
9725 size_t ext_len;
9726
9727 /*
9728 * keyUsage
9729 */
9730
9731 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9732 * to check what a compliant client will think while choosing which cert
9733 * to send to the client. */
9734#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9735 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9736 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9737 /* TLS 1.2 server part of the key exchange */
9738 switch (ciphersuite->key_exchange) {
9739 case MBEDTLS_KEY_EXCHANGE_RSA:
9740 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9741 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9742 break;
9743
9744 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9745 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9746 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9747 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9748 break;
9749
9750 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9751 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9752 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9753 break;
9754
9755 /* Don't use default: we want warnings when adding new values */
9756 case MBEDTLS_KEY_EXCHANGE_NONE:
9757 case MBEDTLS_KEY_EXCHANGE_PSK:
9758 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9759 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9760 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9761 usage = 0;
9762 }
9763 } else
9764#endif
9765 {
9766 /* This is either TLS 1.3 authentication, which always uses signatures,
9767 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9768 * options we implement, both using signatures. */
9769 (void) tls_version;
9770 (void) ciphersuite;
9771 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9772 }
9773
9774 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9775 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9776 ret = -1;
9777 }
9778
9779 /*
9780 * extKeyUsage
9781 */
9782
9783 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9784 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9785 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9786 } else {
9787 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9788 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9789 }
9790
9791 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9792 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9793 ret = -1;
9794 }
9795
9796 return ret;
9797}
9798
9799int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9800 int authmode,
9801 mbedtls_x509_crt *chain,
9802 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9803 void *rs_ctx)
9804{
9805 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9806 return 0;
9807 }
9808
9809 /*
9810 * Primary check: use the appropriate X.509 verification function
9811 */
9812 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9813 void *p_vrfy;
9814 if (ssl->f_vrfy != NULL) {
9815 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9816 f_vrfy = ssl->f_vrfy;
9817 p_vrfy = ssl->p_vrfy;
9818 } else {
9819 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9820 f_vrfy = ssl->conf->f_vrfy;
9821 p_vrfy = ssl->conf->p_vrfy;
9822 }
9823
9824 int ret = 0;
9825 int have_ca_chain_or_callback = 0;
9826#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9827 if (ssl->conf->f_ca_cb != NULL) {
9828 ((void) rs_ctx);
9829 have_ca_chain_or_callback = 1;
9830
9831 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9832 ret = mbedtls_x509_crt_verify_with_ca_cb(
9833 chain,
9834 ssl->conf->f_ca_cb,
9835 ssl->conf->p_ca_cb,
9836 ssl->conf->cert_profile,
9837 ssl->hostname,
9838 &ssl->session_negotiate->verify_result,
9839 f_vrfy, p_vrfy);
9840 } else
9841#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9842 {
9843 mbedtls_x509_crt *ca_chain;
9844 mbedtls_x509_crl *ca_crl;
9845#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9846 if (ssl->handshake->sni_ca_chain != NULL) {
9847 ca_chain = ssl->handshake->sni_ca_chain;
9848 ca_crl = ssl->handshake->sni_ca_crl;
9849 } else
9850#endif
9851 {
9852 ca_chain = ssl->conf->ca_chain;
9853 ca_crl = ssl->conf->ca_crl;
9854 }
9855
9856 if (ca_chain != NULL) {
9857 have_ca_chain_or_callback = 1;
9858 }
9859
9860 ret = mbedtls_x509_crt_verify_restartable(
9861 chain,
9862 ca_chain, ca_crl,
9863 ssl->conf->cert_profile,
9864 ssl->hostname,
9865 &ssl->session_negotiate->verify_result,
9866 f_vrfy, p_vrfy, rs_ctx);
9867 }
9868
9869 if (ret != 0) {
9870 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9871 }
9872
9873#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9874 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9875 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9876 }
9877#endif
9878
9879 /*
9880 * Secondary checks: always done, but change 'ret' only if it was 0
9881 */
9882
9883 /* With TLS 1.2 and ECC certs, check that the curve used by the
9884 * certificate is on our list of acceptable curves.
9885 *
9886 * With TLS 1.3 this is not needed because the curve is part of the
9887 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9888 * we validate the signature made with the key associated to this cert.
9889 */
9890#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9891 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9892 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9893 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9894 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9895 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9896 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9897 if (ret == 0) {
9898 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9899 }
9900 }
9901 }
9902#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9903
9904 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9905 if (mbedtls_ssl_check_cert_usage(chain,
9906 ciphersuite_info,
9907 ssl->conf->endpoint,
9908 ssl->tls_version,
9909 &ssl->session_negotiate->verify_result) != 0) {
9910 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9911 if (ret == 0) {
9912 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9913 }
9914 }
9915
9916 /* With authmode optional, we want to keep going if the certificate was
9917 * unacceptable, but still fail on other errors (out of memory etc),
9918 * including fatal errors from the f_vrfy callback.
9919 *
9920 * The only acceptable errors are:
9921 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9922 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9923 * Anything else is a fatal error. */
9924 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9925 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9926 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9927 ret = 0;
9928 }
9929
9930 /* Return a specific error as this is a user error: inconsistent
9931 * configuration - can't verify without trust anchors. */
9932 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9933 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9934 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9935 }
9936
9937 if (ret != 0) {
9938 uint8_t alert;
9939
9940 /* The certificate may have been rejected for several reasons.
9941 Pick one and send the corresponding alert. Which alert to send
9942 may be a subject of debate in some cases. */
9943 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9944 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9945 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9946 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9947 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9948 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9949 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9950 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9951 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9952 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9953 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9954 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9955 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9956 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9957 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9958 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9959 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9960 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9961 } else {
9962 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9963 }
9964 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9965 alert);
9966 }
9967
9968#if defined(MBEDTLS_DEBUG_C)
9969 if (ssl->session_negotiate->verify_result != 0) {
9970 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9971 (unsigned int) ssl->session_negotiate->verify_result));
9972 } else {
9973 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9974 }
9975#endif /* MBEDTLS_DEBUG_C */
9976
9977 return ret;
9978}
9979#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#endif /* MBEDTLS_SSL_TLS_C */