blob: 3633cd40f9e4dbd56b74777d328b56259d2ede14 [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 Peskinee61852e2025-02-12 23:28:48 +01002772
Gilles Peskine6b885942025-02-12 23:53:25 +01002773/* A magic value for `ssl->hostname` indicating that
2774 * mbedtls_ssl_set_hostname() has been called with `NULL`.
2775 * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then
2776 * `ssl->hostname == NULL`. */
2777static const char *const ssl_hostname_skip_cn_verification = "";
2778
Gilles Peskinee61852e2025-02-12 23:28:48 +01002779#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2780/** Whether mbedtls_ssl_set_hostname() has been called.
2781 *
2782 * \param[in] ssl SSL context
2783 *
2784 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
2785 * (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
2786 * otherwise \c 0.
2787 */
2788static int mbedtls_ssl_has_set_hostname_been_called(
2789 const mbedtls_ssl_context *ssl)
2790{
Gilles Peskinee61852e2025-02-12 23:28:48 +01002791 return ssl->hostname != NULL;
2792}
2793#endif
2794
2795/* Micro-optimization: don't export this function if it isn't needed outside
2796 * of this source file. */
2797#if !defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2798static
2799#endif
2800const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
2801{
Gilles Peskine6b885942025-02-12 23:53:25 +01002802 if (ssl->hostname == ssl_hostname_skip_cn_verification) {
2803 return NULL;
2804 }
Gilles Peskinee61852e2025-02-12 23:28:48 +01002805 return ssl->hostname;
2806}
2807
2808static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
2809{
Gilles Peskine6b885942025-02-12 23:53:25 +01002810 if (ssl->hostname != NULL &&
2811 ssl->hostname != ssl_hostname_skip_cn_verification) {
Gilles Peskinee61852e2025-02-12 23:28:48 +01002812 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
2813 }
2814 ssl->hostname = NULL;
2815}
2816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002818{
Hanno Becker947194e2017-04-07 13:25:49 +01002819 /* Initialize to suppress unnecessary compiler warning */
2820 size_t hostname_len = 0;
2821
2822 /* Check if new hostname is valid before
2823 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (hostname != NULL) {
2825 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2828 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2829 }
Hanno Becker947194e2017-04-07 13:25:49 +01002830 }
2831
2832 /* Now it's clear that we will overwrite the old hostname,
2833 * so we can free it safely */
Gilles Peskinee61852e2025-02-12 23:28:48 +01002834 mbedtls_ssl_free_hostname(ssl);
Hanno Becker947194e2017-04-07 13:25:49 +01002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (hostname == NULL) {
Gilles Peskine6b885942025-02-12 23:53:25 +01002837 /* Passing NULL as hostname clears the old one, but leaves a
2838 * special marker to indicate that mbedtls_ssl_set_hostname()
2839 * has been called. */
2840 /* ssl->hostname should be const, but isn't. We won't actually
2841 * write to the buffer, so it's ok to cast away the const. */
2842 ssl->hostname = (char *) ssl_hostname_skip_cn_verification;
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 } else {
2844 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2845 if (ssl->hostname == NULL) {
Gilles Peskine6b885942025-02-12 23:53:25 +01002846 /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully.
2847 * Leave ssl->hostname in the same state as if the function had
2848 * not been called, i.e. a null pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2850 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002853
Hanno Becker947194e2017-04-07 13:25:49 +01002854 ssl->hostname[hostname_len] = '\0';
2855 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002858}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002859#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002861#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002862void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2863 int (*f_sni)(void *, mbedtls_ssl_context *,
2864 const unsigned char *, size_t),
2865 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002866{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002867 conf->f_sni = f_sni;
2868 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002872#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002873int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002874{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002875 size_t cur_len, tot_len;
2876 const char **p;
2877
2878 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002879 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2880 * MUST NOT be truncated."
2881 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002882 */
2883 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 for (p = protos; *p != NULL; p++) {
2885 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002886 tot_len += cur_len;
2887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 if ((cur_len == 0) ||
2889 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2890 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2891 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2892 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002893 }
2894
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002895 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002898}
2899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002901{
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002905
Johan Pascalb62bb512015-12-03 21:56:45 +01002906#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002907void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2908 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002909{
2910 conf->dtls_srtp_mki_support = support_mki_value;
2911}
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2914 unsigned char *mki_value,
2915 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002916{
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2918 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002919 }
Ron Eldor591f1622018-01-22 12:30:04 +02002920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2922 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002923 }
Ron Eldor591f1622018-01-22 12:30:04 +02002924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002926 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002928}
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2931 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002932{
Johan Pascal253d0262020-09-22 13:04:45 +02002933 const mbedtls_ssl_srtp_profile *p;
2934 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002935
Johan Pascal253d0262020-09-22 13:04:45 +02002936 /* check the profiles list: all entry must be valid,
2937 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2939 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2940 p++) {
2941 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002942 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002944 /* unsupported value, stop parsing and set the size to an error value */
2945 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002946 }
2947 }
2948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2950 conf->dtls_srtp_profile_list = NULL;
2951 conf->dtls_srtp_profile_list_len = 0;
2952 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002953 }
2954
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002955 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002956 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002959}
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2962 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002963{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002964 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2965 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002967 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002969 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2971 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002972 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002973}
Johan Pascalb62bb512015-12-03 21:56:45 +01002974#endif /* MBEDTLS_SSL_DTLS_SRTP */
2975
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302976#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002977void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002978{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002979 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002980}
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002983{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002984 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002985}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302986#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002987
Janos Follath088ce432017-04-10 12:42:31 +01002988#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002989void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2990 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002991{
2992 conf->cert_req_ca_list = cert_req_ca_list;
2993}
2994#endif
2995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002997void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002998{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002999 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003000}
3001#endif
3002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003004void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003006 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003007}
3008#endif
3009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003011int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003012{
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3014 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3015 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003016 }
3017
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003018 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003022#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003025{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003026 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003027}
3028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003030void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003032 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003033}
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003037 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3041 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003042{
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003047#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003048#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02003049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003051{
Ronald Crond67f8012024-08-28 07:45:57 +02003052 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3053 conf->session_tickets |= (use_tickets != 0) <<
3054 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003055}
Ronald Crond67f8012024-08-28 07:45:57 +02003056
Ronald Cronbedddd72024-08-27 14:18:50 +02003057#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003058void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3059 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003060{
Ronald Crond67f8012024-08-28 07:45:57 +02003061 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003062 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003063 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3064}
Ronald Cronbedddd72024-08-27 14:18:50 +02003065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3066#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003067
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003068#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003069
Jerry Yud0766ec2022-09-22 10:46:57 +08003070#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003071void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3072 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003073{
Jerry Yud0766ec2022-09-22 10:46:57 +08003074 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003075}
3076#endif
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3079 mbedtls_ssl_ticket_write_t *f_ticket_write,
3080 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3081 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003082{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003083 conf->f_ticket_write = f_ticket_write;
3084 conf->f_ticket_parse = f_ticket_parse;
3085 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003086}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3091 mbedtls_ssl_export_keys_t *f_export_keys,
3092 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003093{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003094 ssl->f_export_keys = f_export_keys;
3095 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003096}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003097
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003098#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003099void mbedtls_ssl_conf_async_private_cb(
3100 mbedtls_ssl_config *conf,
3101 mbedtls_ssl_async_sign_t *f_async_sign,
3102 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3103 mbedtls_ssl_async_resume_t *f_async_resume,
3104 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003106{
3107 conf->f_async_sign_start = f_async_sign;
3108 conf->f_async_decrypt_start = f_async_decrypt;
3109 conf->f_async_resume = f_async_resume;
3110 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003111 conf->p_async_config_data = async_config_data;
3112}
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003115{
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003117}
3118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003120{
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (ssl->handshake == NULL) {
3122 return NULL;
3123 } else {
3124 return ssl->handshake->user_async_ctx;
3125 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003126}
3127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3129 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003130{
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003132 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003134}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003135#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003136
Paul Bakker5121ce52009-01-03 21:22:43 +00003137/*
3138 * SSL get accessors
3139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003140uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003141{
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if (ssl->session != NULL) {
3143 return ssl->session->verify_result;
3144 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if (ssl->session_negotiate != NULL) {
3147 return ssl->session_negotiate->verify_result;
3148 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003151}
3152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003154{
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if (ssl == NULL || ssl->session == NULL) {
3156 return 0;
3157 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003160}
3161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003163{
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (ssl == NULL || ssl->session == NULL) {
3165 return NULL;
3166 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003169}
3170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3175 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003176 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003178 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003180 }
3181 }
3182#endif
3183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003185 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003187 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003189 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003191 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003192}
3193
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003194#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3195
3196size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3197{
3198 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3199 size_t record_size_limit = max_len;
3200
3201 if (ssl->session != NULL &&
3202 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3203 ssl->session->record_size_limit < max_len) {
3204 record_size_limit = ssl->session->record_size_limit;
3205 }
3206
3207 // TODO: this is currently untested
3208 /* During a handshake, use the value being negotiated */
3209 if (ssl->session_negotiate != NULL &&
3210 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3211 ssl->session_negotiate->record_size_limit < max_len) {
3212 record_size_limit = ssl->session_negotiate->record_size_limit;
3213 }
3214
3215 return record_size_limit;
3216}
3217#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3218
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003219#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003220size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003221{
David Horstmann95d516f2021-05-04 18:36:56 +01003222 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003223 size_t read_mfl;
3224
Jerry Yuddda0502022-12-01 19:43:12 +08003225#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003226 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3228 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3229 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003230 }
Jerry Yuddda0502022-12-01 19:43:12 +08003231#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003232
3233 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (ssl->session_out != NULL) {
3235 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3236 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003237 max_len = read_mfl;
3238 }
3239 }
3240
Jerry Yuddda0502022-12-01 19:43:12 +08003241 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ssl->session_negotiate != NULL) {
3243 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3244 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003245 max_len = read_mfl;
3246 }
3247 }
3248
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003250}
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003253{
3254 size_t max_len;
3255
3256 /*
3257 * Assume mfl_code is correct since it was checked when set
3258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003260
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003261 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (ssl->session_out != NULL &&
3263 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3264 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003265 }
3266
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003267 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (ssl->session_negotiate != NULL &&
3269 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3270 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003271 }
3272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003274}
3275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3276
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003278size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003279{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003280 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3282 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3283 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3284 return 0;
3285 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3288 return ssl->mtu;
3289 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (ssl->mtu == 0) {
3292 return ssl->handshake->mtu;
3293 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 return ssl->mtu < ssl->handshake->mtu ?
3296 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003297}
3298#endif /* MBEDTLS_SSL_PROTO_DTLS */
3299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003301{
3302 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3303
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003304#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003305 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003306 !defined(MBEDTLS_SSL_PROTO_DTLS)
3307 (void) ssl;
3308#endif
3309
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003310#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003316#endif
3317
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003318#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3319 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3320
3321 if (max_len > record_size_limit) {
3322 max_len = record_size_limit;
3323 }
3324#endif
3325
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003326 if (ssl->transform_out != NULL &&
3327 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003328 /*
3329 * In TLS 1.3 case, when records are protected, `max_len` as computed
3330 * above is the maximum length of the TLSInnerPlaintext structure that
3331 * along the plaintext payload contains the inner content type (one byte)
3332 * and some zero padding. Given the algorithm used for padding
3333 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3334 * the plaintext payload. Round down to a multiple of
3335 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3336 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003337 */
3338 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3339 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3340 }
3341
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3344 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3345 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003346 const size_t overhead = (size_t) ret;
3347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 if (ret < 0) {
3349 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003350 }
3351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (mtu <= overhead) {
3353 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3354 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3355 }
3356
3357 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003358 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003360 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003361#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003362
Hanno Becker0defedb2018-08-10 12:35:02 +01003363#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003364 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3365 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003366 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003367#endif
3368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003370}
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003373{
3374 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3375
3376#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3377 (void) ssl;
3378#endif
3379
3380#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003382
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003384 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003386#endif
3387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003389}
3390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003392const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003393{
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 if (ssl == NULL || ssl->session == NULL) {
3395 return NULL;
3396 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003397
Hanno Beckere6824572019-02-07 13:18:46 +00003398#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003400#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003402#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003407int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3408 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003409{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003410 int ret;
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003413 dst == NULL ||
3414 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3416 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003417 }
3418
Hanno Beckere810bbc2021-05-14 16:01:05 +01003419 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3420 * idempotent: Each session can only be exported once.
3421 *
3422 * (This is in preparation for TLS 1.3 support where we will
3423 * need the ability to export multiple sessions (aka tickets),
3424 * which will be achieved by calling mbedtls_ssl_get_session()
3425 * multiple times until it fails.)
3426 *
3427 * Check whether we have already exported the current session,
3428 * and fail if so.
3429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 if (ssl->session->exported == 1) {
3431 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3432 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3435 if (ret != 0) {
3436 return ret;
3437 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003438
3439 /* Remember that we've exported the session. */
3440 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003444
David Horstmanncb01b362024-02-29 17:31:46 +00003445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3446
3447/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003448 *
David Horstmanncb01b362024-02-29 17:31:46 +00003449 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003450 */
3451static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3452 unsigned char *buf,
3453 size_t buf_len)
3454{
3455 unsigned char *p = buf;
3456 size_t used = 0;
3457
3458#if defined(MBEDTLS_HAVE_TIME)
3459 uint64_t start;
3460#endif
3461#if defined(MBEDTLS_X509_CRT_PARSE_C)
3462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3463 size_t cert_len;
3464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3465#endif /* MBEDTLS_X509_CRT_PARSE_C */
3466
3467 /*
3468 * Time
3469 */
3470#if defined(MBEDTLS_HAVE_TIME)
3471 used += 8;
3472
3473 if (used <= buf_len) {
3474 start = (uint64_t) session->start;
3475
3476 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3477 p += 8;
3478 }
3479#endif /* MBEDTLS_HAVE_TIME */
3480
3481 /*
3482 * Basic mandatory fields
3483 */
3484 used += 1 /* id_len */
3485 + sizeof(session->id)
3486 + sizeof(session->master)
3487 + 4; /* verify_result */
3488
3489 if (used <= buf_len) {
3490 *p++ = MBEDTLS_BYTE_0(session->id_len);
3491 memcpy(p, session->id, 32);
3492 p += 32;
3493
3494 memcpy(p, session->master, 48);
3495 p += 48;
3496
3497 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3498 p += 4;
3499 }
3500
3501 /*
3502 * Peer's end-entity certificate
3503 */
3504#if defined(MBEDTLS_X509_CRT_PARSE_C)
3505#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3506 if (session->peer_cert == NULL) {
3507 cert_len = 0;
3508 } else {
3509 cert_len = session->peer_cert->raw.len;
3510 }
3511
3512 used += 3 + cert_len;
3513
3514 if (used <= buf_len) {
3515 *p++ = MBEDTLS_BYTE_2(cert_len);
3516 *p++ = MBEDTLS_BYTE_1(cert_len);
3517 *p++ = MBEDTLS_BYTE_0(cert_len);
3518
3519 if (session->peer_cert != NULL) {
3520 memcpy(p, session->peer_cert->raw.p, cert_len);
3521 p += cert_len;
3522 }
3523 }
3524#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3525 if (session->peer_cert_digest != NULL) {
3526 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3527 if (used <= buf_len) {
3528 *p++ = (unsigned char) session->peer_cert_digest_type;
3529 *p++ = (unsigned char) session->peer_cert_digest_len;
3530 memcpy(p, session->peer_cert_digest,
3531 session->peer_cert_digest_len);
3532 p += session->peer_cert_digest_len;
3533 }
3534 } else {
3535 used += 2;
3536 if (used <= buf_len) {
3537 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3538 *p++ = 0;
3539 }
3540 }
3541#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3542#endif /* MBEDTLS_X509_CRT_PARSE_C */
3543
3544 /*
3545 * Session ticket if any, plus associated data
3546 */
3547#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3548#if defined(MBEDTLS_SSL_CLI_C)
3549 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3550 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3551
3552 if (used <= buf_len) {
3553 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3554 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3555 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3556
3557 if (session->ticket != NULL) {
3558 memcpy(p, session->ticket, session->ticket_len);
3559 p += session->ticket_len;
3560 }
3561
3562 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3563 p += 4;
3564 }
3565 }
3566#endif /* MBEDTLS_SSL_CLI_C */
3567#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3568 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3569 used += 8;
3570
3571 if (used <= buf_len) {
3572 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3573 p += 8;
3574 }
3575 }
3576#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3577#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3578
3579 /*
3580 * Misc extension-related info
3581 */
3582#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3583 used += 1;
3584
3585 if (used <= buf_len) {
3586 *p++ = session->mfl_code;
3587 }
3588#endif
3589
3590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3591 used += 1;
3592
3593 if (used <= buf_len) {
3594 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3595 }
3596#endif
3597
3598 return used;
3599}
3600
3601MBEDTLS_CHECK_RETURN_CRITICAL
3602static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3603 const unsigned char *buf,
3604 size_t len)
3605{
3606#if defined(MBEDTLS_HAVE_TIME)
3607 uint64_t start;
3608#endif
3609#if defined(MBEDTLS_X509_CRT_PARSE_C)
3610#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3611 size_t cert_len;
3612#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3613#endif /* MBEDTLS_X509_CRT_PARSE_C */
3614
3615 const unsigned char *p = buf;
3616 const unsigned char * const end = buf + len;
3617
3618 /*
3619 * Time
3620 */
3621#if defined(MBEDTLS_HAVE_TIME)
3622 if (8 > (size_t) (end - p)) {
3623 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3624 }
3625
3626 start = MBEDTLS_GET_UINT64_BE(p, 0);
3627 p += 8;
3628
3629 session->start = (time_t) start;
3630#endif /* MBEDTLS_HAVE_TIME */
3631
3632 /*
3633 * Basic mandatory fields
3634 */
3635 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3637 }
3638
3639 session->id_len = *p++;
3640 memcpy(session->id, p, 32);
3641 p += 32;
3642
3643 memcpy(session->master, p, 48);
3644 p += 48;
3645
3646 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3647 p += 4;
3648
3649 /* Immediately clear invalid pointer values that have been read, in case
3650 * we exit early before we replaced them with valid ones. */
3651#if defined(MBEDTLS_X509_CRT_PARSE_C)
3652#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3653 session->peer_cert = NULL;
3654#else
3655 session->peer_cert_digest = NULL;
3656#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3657#endif /* MBEDTLS_X509_CRT_PARSE_C */
3658#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3659 session->ticket = NULL;
3660#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3661
3662 /*
3663 * Peer certificate
3664 */
3665#if defined(MBEDTLS_X509_CRT_PARSE_C)
3666#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3667 /* Deserialize CRT from the end of the ticket. */
3668 if (3 > (size_t) (end - p)) {
3669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3670 }
3671
3672 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3673 p += 3;
3674
3675 if (cert_len != 0) {
3676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3677
3678 if (cert_len > (size_t) (end - p)) {
3679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3680 }
3681
3682 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3683
3684 if (session->peer_cert == NULL) {
3685 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3686 }
3687
3688 mbedtls_x509_crt_init(session->peer_cert);
3689
3690 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3691 p, cert_len)) != 0) {
3692 mbedtls_x509_crt_free(session->peer_cert);
3693 mbedtls_free(session->peer_cert);
3694 session->peer_cert = NULL;
3695 return ret;
3696 }
3697
3698 p += cert_len;
3699 }
3700#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3701 /* Deserialize CRT digest from the end of the ticket. */
3702 if (2 > (size_t) (end - p)) {
3703 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3704 }
3705
3706 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3707 session->peer_cert_digest_len = (size_t) *p++;
3708
3709 if (session->peer_cert_digest_len != 0) {
3710 const mbedtls_md_info_t *md_info =
3711 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3712 if (md_info == NULL) {
3713 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3714 }
3715 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3717 }
3718
3719 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3721 }
3722
3723 session->peer_cert_digest =
3724 mbedtls_calloc(1, session->peer_cert_digest_len);
3725 if (session->peer_cert_digest == NULL) {
3726 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3727 }
3728
3729 memcpy(session->peer_cert_digest, p,
3730 session->peer_cert_digest_len);
3731 p += session->peer_cert_digest_len;
3732 }
3733#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3734#endif /* MBEDTLS_X509_CRT_PARSE_C */
3735
3736 /*
3737 * Session ticket and associated data
3738 */
3739#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3740#if defined(MBEDTLS_SSL_CLI_C)
3741 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3742 if (3 > (size_t) (end - p)) {
3743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3744 }
3745
3746 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3747 p += 3;
3748
3749 if (session->ticket_len != 0) {
3750 if (session->ticket_len > (size_t) (end - p)) {
3751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3752 }
3753
3754 session->ticket = mbedtls_calloc(1, session->ticket_len);
3755 if (session->ticket == NULL) {
3756 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3757 }
3758
3759 memcpy(session->ticket, p, session->ticket_len);
3760 p += session->ticket_len;
3761 }
3762
3763 if (4 > (size_t) (end - p)) {
3764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3765 }
3766
3767 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3768 p += 4;
3769 }
3770#endif /* MBEDTLS_SSL_CLI_C */
3771#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3772 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3773 if (8 > (size_t) (end - p)) {
3774 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3775 }
3776 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3777 p += 8;
3778 }
3779#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3780#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3781
3782 /*
3783 * Misc extension-related info
3784 */
3785#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3786 if (1 > (size_t) (end - p)) {
3787 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3788 }
3789
3790 session->mfl_code = *p++;
3791#endif
3792
3793#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3794 if (1 > (size_t) (end - p)) {
3795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3796 }
3797
3798 session->encrypt_then_mac = *p++;
3799#endif
3800
3801 /* Done, should have consumed entire buffer */
3802 if (p != end) {
3803 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3804 }
3805
3806 return 0;
3807}
3808
3809#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3810
3811#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3812/* Serialization of TLS 1.3 sessions:
3813 *
David Horstmanncb01b362024-02-29 17:31:46 +00003814 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003815 */
3816#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3817MBEDTLS_CHECK_RETURN_CRITICAL
3818static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3819 unsigned char *buf,
3820 size_t buf_len,
3821 size_t *olen)
3822{
3823 unsigned char *p = buf;
3824#if defined(MBEDTLS_SSL_CLI_C) && \
3825 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3826 size_t hostname_len = (session->hostname == NULL) ?
3827 0 : strlen(session->hostname) + 1;
3828#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003829
3830#if defined(MBEDTLS_SSL_SRV_C) && \
3831 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003832 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003833 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003834#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003835 size_t needed = 4 /* ticket_age_add */
3836 + 1 /* ticket_flags */
3837 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003838
David Horstmanne59f9702024-02-29 16:08:57 +00003839 *olen = 0;
3840
3841 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3842 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3843 }
3844 needed += session->resumption_key_len; /* resumption_key */
3845
3846#if defined(MBEDTLS_SSL_EARLY_DATA)
3847 needed += 4; /* max_early_data_size */
3848#endif
3849#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3850 needed += 2; /* record_size_limit */
3851#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3852
3853#if defined(MBEDTLS_HAVE_TIME)
3854 needed += 8; /* ticket_creation_time or ticket_reception_time */
3855#endif
3856
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003857#if defined(MBEDTLS_SSL_SRV_C)
3858 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3859#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003860 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003861 + alpn_len; /* alpn */
3862#endif
3863 }
3864#endif /* MBEDTLS_SSL_SRV_C */
3865
David Horstmanne59f9702024-02-29 16:08:57 +00003866#if defined(MBEDTLS_SSL_CLI_C)
3867 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3868#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3869 needed += 2 /* hostname_len */
3870 + hostname_len; /* hostname */
3871#endif
3872
3873 needed += 4 /* ticket_lifetime */
3874 + 2; /* ticket_len */
3875
3876 /* Check size_t overflow */
3877 if (session->ticket_len > SIZE_MAX - needed) {
3878 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3879 }
3880
3881 needed += session->ticket_len; /* ticket */
3882 }
3883#endif /* MBEDTLS_SSL_CLI_C */
3884
3885 *olen = needed;
3886 if (needed > buf_len) {
3887 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3888 }
3889
3890 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3891 p[4] = session->ticket_flags;
3892
3893 /* save resumption_key */
3894 p[5] = session->resumption_key_len;
3895 p += 6;
3896 memcpy(p, session->resumption_key, session->resumption_key_len);
3897 p += session->resumption_key_len;
3898
3899#if defined(MBEDTLS_SSL_EARLY_DATA)
3900 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3901 p += 4;
3902#endif
3903#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3904 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3905 p += 2;
3906#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3907
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003908#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003909 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003910#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003911 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3912 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003913#endif /* MBEDTLS_HAVE_TIME */
3914
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003915#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003916 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3917 p += 2;
3918
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003919 if (alpn_len > 0) {
3920 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003921 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003922 p += alpn_len;
3923 }
3924#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3925 }
3926#endif /* MBEDTLS_SSL_SRV_C */
3927
David Horstmanne59f9702024-02-29 16:08:57 +00003928#if defined(MBEDTLS_SSL_CLI_C)
3929 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3930#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3931 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3932 p += 2;
3933 if (hostname_len > 0) {
3934 /* save host name */
3935 memcpy(p, session->hostname, hostname_len);
3936 p += hostname_len;
3937 }
3938#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3939
3940#if defined(MBEDTLS_HAVE_TIME)
3941 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3942 p += 8;
3943#endif
3944 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3945 p += 4;
3946
3947 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3948 p += 2;
3949
3950 if (session->ticket != NULL && session->ticket_len > 0) {
3951 memcpy(p, session->ticket, session->ticket_len);
3952 p += session->ticket_len;
3953 }
3954 }
3955#endif /* MBEDTLS_SSL_CLI_C */
3956 return 0;
3957}
3958
3959MBEDTLS_CHECK_RETURN_CRITICAL
3960static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3961 const unsigned char *buf,
3962 size_t len)
3963{
3964 const unsigned char *p = buf;
3965 const unsigned char *end = buf + len;
3966
3967 if (end - p < 6) {
3968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3969 }
3970 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3971 session->ticket_flags = p[4];
3972
3973 /* load resumption_key */
3974 session->resumption_key_len = p[5];
3975 p += 6;
3976
3977 if (end - p < session->resumption_key_len) {
3978 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3979 }
3980
3981 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3982 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3983 }
3984 memcpy(session->resumption_key, p, session->resumption_key_len);
3985 p += session->resumption_key_len;
3986
3987#if defined(MBEDTLS_SSL_EARLY_DATA)
3988 if (end - p < 4) {
3989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3990 }
3991 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3992 p += 4;
3993#endif
3994#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3995 if (end - p < 2) {
3996 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3997 }
3998 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3999 p += 2;
4000#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4001
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004002#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00004003 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004004#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00004005 if (end - p < 8) {
4006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4007 }
4008 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
4009 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00004010#endif /* MBEDTLS_HAVE_TIME */
4011
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004012#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004013 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004014
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004015 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4017 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004018
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004019 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
4020 p += 2;
4021
4022 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4024 }
4025
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004026 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004027 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
4028 if (ret != 0) {
4029 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004030 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004031 p += alpn_len;
4032 }
4033#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
4034 }
4035#endif /* MBEDTLS_SSL_SRV_C */
4036
David Horstmanne59f9702024-02-29 16:08:57 +00004037#if defined(MBEDTLS_SSL_CLI_C)
4038 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4039#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4040 size_t hostname_len;
4041 /* load host name */
4042 if (end - p < 2) {
4043 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4044 }
4045 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
4046 p += 2;
4047
4048 if (end - p < (long int) hostname_len) {
4049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4050 }
4051 if (hostname_len > 0) {
4052 session->hostname = mbedtls_calloc(1, hostname_len);
4053 if (session->hostname == NULL) {
4054 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4055 }
4056 memcpy(session->hostname, p, hostname_len);
4057 p += hostname_len;
4058 }
4059#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4060
4061#if defined(MBEDTLS_HAVE_TIME)
4062 if (end - p < 8) {
4063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4064 }
4065 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4066 p += 8;
4067#endif
4068 if (end - p < 4) {
4069 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4070 }
4071 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4072 p += 4;
4073
4074 if (end - p < 2) {
4075 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4076 }
4077 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4078 p += 2;
4079
4080 if (end - p < (long int) session->ticket_len) {
4081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4082 }
4083 if (session->ticket_len > 0) {
4084 session->ticket = mbedtls_calloc(1, session->ticket_len);
4085 if (session->ticket == NULL) {
4086 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4087 }
4088 memcpy(session->ticket, p, session->ticket_len);
4089 p += session->ticket_len;
4090 }
4091 }
4092#endif /* MBEDTLS_SSL_CLI_C */
4093
4094 return 0;
4095
4096}
4097#else /* MBEDTLS_SSL_SESSION_TICKETS */
4098MBEDTLS_CHECK_RETURN_CRITICAL
4099static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4100 unsigned char *buf,
4101 size_t buf_len,
4102 size_t *olen)
4103{
4104 ((void) session);
4105 ((void) buf);
4106 ((void) buf_len);
4107 *olen = 0;
4108 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4109}
4110
4111static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004112 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004113 size_t buf_len)
4114{
4115 ((void) session);
4116 ((void) buf);
4117 ((void) buf_len);
4118 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4119}
4120#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4121#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4122
Paul Bakker5121ce52009-01-03 21:22:43 +00004123/*
Hanno Beckera835da52019-05-16 12:39:07 +01004124 * Define ticket header determining Mbed TLS version
4125 * and structure of the ticket.
4126 */
4127
Hanno Becker94ef3b32019-05-16 12:50:45 +01004128/*
Hanno Becker50b59662019-05-28 14:30:45 +01004129 * Define bitflag determining compile-time settings influencing
4130 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004131 */
4132
Hanno Becker50b59662019-05-28 14:30:45 +01004133#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004134#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004135#else
Hanno Becker3e088662019-05-29 11:10:18 +01004136#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004137#endif /* MBEDTLS_HAVE_TIME */
4138
4139#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004140#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004141#else
Hanno Becker3e088662019-05-29 11:10:18 +01004142#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004143#endif /* MBEDTLS_X509_CRT_PARSE_C */
4144
David Horstmann5c5a32f2024-02-13 17:53:35 +00004145#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004146#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004147#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004148#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004149#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4150
Hanno Becker94ef3b32019-05-16 12:50:45 +01004151#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004152#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004153#else
Hanno Becker3e088662019-05-29 11:10:18 +01004154#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004155#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4156
4157#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004158#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004159#else
Hanno Becker3e088662019-05-29 11:10:18 +01004160#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004161#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4162
Hanno Becker94ef3b32019-05-16 12:50:45 +01004163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004164#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004165#else
Hanno Becker3e088662019-05-29 11:10:18 +01004166#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004167#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4168
Hanno Becker94ef3b32019-05-16 12:50:45 +01004169#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4170#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4171#else
4172#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4173#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4174
David Horstmann92b258b2024-02-13 17:23:34 +00004175#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4176#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4177#else
4178#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4179#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4180
4181#if defined(MBEDTLS_SSL_EARLY_DATA)
4182#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4183#else
4184#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4185#endif /* MBEDTLS_SSL_EARLY_DATA */
4186
4187#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4188#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4189#else
4190#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4191#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4192
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004193#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4194 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004195#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4196#else
4197#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4198#endif /* MBEDTLS_SSL_ALPN */
4199
Hanno Becker3e088662019-05-29 11:10:18 +01004200#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4201#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4202#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4203#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004204#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4205#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004206#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004207#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4208#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4209#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004210#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004211
Hanno Becker50b59662019-05-28 14:30:45 +01004212#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 ((uint16_t) ( \
4214 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4215 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4216 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4217 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4218 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4219 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004220 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004221 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4222 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004223 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4224 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4225 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4226 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004227 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004228 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004229 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004230
Chien Wong4e9683e2023-12-28 17:07:43 +08004231static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004232 MBEDTLS_VERSION_MAJOR,
4233 MBEDTLS_VERSION_MINOR,
4234 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4236 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004237};
Hanno Beckera835da52019-05-16 12:39:07 +01004238
4239/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004240 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004241 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004242 *
David Horstmanncb01b362024-02-29 17:31:46 +00004243 * TLS 1.2 session:
4244 *
4245 * struct {
4246 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4247 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4248 * uint32 ticket_lifetime;
4249 * #endif
4250 * } ClientOnlyData;
4251 *
4252 * struct {
4253 * #if defined(MBEDTLS_HAVE_TIME)
4254 * uint64 start_time;
4255 * #endif
4256 * uint8 session_id_len; // at most 32
4257 * opaque session_id[32];
4258 * opaque master[48]; // fixed length in the standard
4259 * uint32 verify_result;
4260 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4261 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4262 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004263 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004264 * opaque peer_cert_digest<0..2^8-1>
4265 * #endif
4266 * select (endpoint) {
4267 * case client: ClientOnlyData;
4268 * case server: uint64 ticket_creation_time;
4269 * };
4270 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4271 * uint8 mfl_code; // up to 255 according to standard
4272 * #endif
4273 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4274 * uint8 encrypt_then_mac; // 0 or 1
4275 * #endif
4276 * } serialized_session_tls12;
4277 *
4278 *
4279 * TLS 1.3 Session:
4280 *
4281 * struct {
4282 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4283 * opaque hostname<0..2^16-1>;
4284 * #endif
4285 * #if defined(MBEDTLS_HAVE_TIME)
4286 * uint64 ticket_reception_time;
4287 * #endif
4288 * uint32 ticket_lifetime;
4289 * opaque ticket<1..2^16-1>;
4290 * } ClientOnlyData;
4291 *
4292 * struct {
4293 * uint32 ticket_age_add;
4294 * uint8 ticket_flags;
4295 * opaque resumption_key<0..255>;
4296 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4297 * uint32 max_early_data_size;
4298 * #endif
4299 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4300 * uint16 record_size_limit;
4301 * #endif
4302 * select ( endpoint ) {
4303 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004304 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004305 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004306 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004307 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004308 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004309 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004310 * #endif
4311 * };
4312 * } serialized_session_tls13;
4313 *
4314 *
4315 * SSL session:
4316 *
4317 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004318 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004319 * opaque mbedtls_version[3]; // library version: major, minor, patch
4320 * opaque session_format[2]; // library-version specific 16-bit field
4321 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004322 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004323 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004324 * Note: When updating the format, remember to keep
4325 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004326 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004327 * // In this version, `session_format` determines
4328 * // the setting of those compile-time
4329 * // configuration options which influence
4330 * // the structure of mbedtls_ssl_session.
4331 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004332 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004333 * // - TLS 1.2 (0x0303)
4334 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004335 * uint8_t endpoint;
4336 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004337 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004338 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004339 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004340 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004341 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004342 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004343 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004344 *
4345 * };
4346 *
4347 * } serialized_session;
4348 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004349 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004350
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004351MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004352static int ssl_session_save(const mbedtls_ssl_session *session,
4353 unsigned char omit_header,
4354 unsigned char *buf,
4355 size_t buf_len,
4356 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004357{
4358 unsigned char *p = buf;
4359 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004360 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004361#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4362 size_t out_len;
4363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4364#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 if (session == NULL) {
4366 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4367 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004370 /*
4371 * Add Mbed TLS version identifier
4372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (used <= buf_len) {
4376 memcpy(p, ssl_serialized_session_header,
4377 sizeof(ssl_serialized_session_header));
4378 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004379 }
4380 }
4381
4382 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004383 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004384 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004385 used += 1 /* TLS version */
4386 + 1 /* endpoint */
4387 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (used <= buf_len) {
4389 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004390 *p++ = session->endpoint;
4391 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4392 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004393 }
4394
4395 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004396 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004398#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 case MBEDTLS_SSL_VERSION_TLS1_2:
4400 used += ssl_tls12_session_save(session, p, remaining_len);
4401 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004402#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4403
Jerry Yu251a12e2022-07-13 15:15:48 +08004404#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 case MBEDTLS_SSL_VERSION_TLS1_3:
4406 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4407 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4408 return ret;
4409 }
4410 used += out_len;
4411 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004412#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 default:
4415 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004416 }
4417
4418 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 if (used > buf_len) {
4420 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4421 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004424}
4425
4426/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004427 * Public wrapper for ssl_session_save()
4428 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004429int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4430 unsigned char *buf,
4431 size_t buf_len,
4432 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004433{
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004435}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004436
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004437/*
4438 * Deserialize session, see mbedtls_ssl_session_save() for format.
4439 *
4440 * This internal version is wrapped by a public function that cleans up in
4441 * case of error, and has an extra option omit_header.
4442 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004443MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004444static int ssl_session_load(mbedtls_ssl_session *session,
4445 unsigned char omit_header,
4446 const unsigned char *buf,
4447 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004448{
4449 const unsigned char *p = buf;
4450 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004451 size_t remaining_len;
4452
4453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (session == NULL) {
4455 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4456 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004459 /*
4460 * Check Mbed TLS version identifier
4461 */
4462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4464 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004465 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004466
4467 if (memcmp(p, ssl_serialized_session_header,
4468 sizeof(ssl_serialized_session_header)) != 0) {
4469 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4470 }
4471 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004472 }
4473
4474 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004475 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004476 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004477 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4479 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004480 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004481 session->endpoint = *p++;
4482 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4483 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004484
4485 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004486 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 case MBEDTLS_SSL_VERSION_TLS1_2:
4490 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004491#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4492
Jerry Yu438ddd82022-07-07 06:55:50 +00004493#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 case MBEDTLS_SSL_VERSION_TLS1_3:
4495 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004496#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 default:
4499 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004500 }
4501}
4502
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004503/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004504 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004506int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4507 const unsigned char *buf,
4508 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004509{
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 if (ret != 0) {
4513 mbedtls_ssl_session_free(session);
4514 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004517}
4518
4519/*
Paul Bakker1961b702013-01-25 14:49:24 +01004520 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004522MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004523static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004524{
4525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4526
Ronald Cron66dbf912022-02-02 15:33:46 +01004527 /*
4528 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004529 * that were written into the output buffer by the previous handshake step,
4530 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004531 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4532 * We proceed to the next handshake step only when all data from the
4533 * previous one have been sent to the peer, thus we make sure that this is
4534 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4535 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4536 * we have to wait before to go ahead.
4537 * In the case of TLS 1.3, handshake step handlers do not send data to the
4538 * peer. Data are only sent here and through
4539 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004540 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004541 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4543 return ret;
4544 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004545
4546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4548 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4549 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4550 return ret;
4551 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004552 }
4553#endif /* MBEDTLS_SSL_PROTO_DTLS */
4554
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004556}
4557
Gilles Peskine449bd832023-01-11 14:50:10 +01004558int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004559{
Hanno Becker41934dd2021-08-07 19:13:43 +01004560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004561
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004563 ssl->conf == NULL ||
4564 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4566 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004567 }
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 ret = ssl_prepare_handshake_step(ssl);
4570 if (ret != 0) {
4571 return ret;
4572 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 ret = mbedtls_ssl_handle_pending_alert(ssl);
4575 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004576 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 }
Jerry Yue7047812021-09-13 19:26:39 +08004578
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004579 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4580 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4581 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4585 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004586 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004589 case MBEDTLS_SSL_HELLO_REQUEST:
4590 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004591 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004592 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004593
Ronald Cron9f0fba32022-02-10 16:45:15 +01004594 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004596 break;
4597
4598 default:
4599#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4601 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4602 } else {
4603 ret = mbedtls_ssl_handshake_client_step(ssl);
4604 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004605#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004607#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004609#endif
4610 }
Jerry Yub9930e72021-08-06 17:11:51 +08004611 }
Ronald Cron6291b232023-03-08 15:51:25 +01004612#endif /* MBEDTLS_SSL_CLI_C */
4613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004616#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4617 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004619 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 ret = mbedtls_ssl_handshake_server_step(ssl);
4621 }
Ronald Cron6291b232023-03-08 15:51:25 +01004622#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4623 ret = mbedtls_ssl_handshake_server_step(ssl);
4624#else
4625 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004626#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004627 }
4628#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004631 /* handshake_step return error. And it is same
4632 * with alert_reason.
4633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (ssl->send_alert) {
4635 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004636 goto cleanup;
4637 }
4638 }
4639
4640cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004642}
4643
4644/*
4645 * Perform the SSL handshake
4646 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004647int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004648{
4649 int ret = 0;
4650
Hanno Beckera817ea42020-10-20 15:20:23 +01004651 /* Sanity checks */
4652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 if (ssl == NULL || ssl->conf == NULL) {
4654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4655 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004656
Hanno Beckera817ea42020-10-20 15:20:23 +01004657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4659 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4660 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4661 "mbedtls_ssl_set_timer_cb() for DTLS"));
4662 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004663 }
4664#endif /* MBEDTLS_SSL_PROTO_DTLS */
4665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004667
Hanno Beckera817ea42020-10-20 15:20:23 +01004668 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4670 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004673 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 }
Paul Bakker1961b702013-01-25 14:49:24 +01004675 }
4676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004680}
4681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682#if defined(MBEDTLS_SSL_RENEGOTIATION)
4683#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004684/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004685 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004686 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004687MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004688static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004689{
Janos Follath865b3eb2019-12-16 11:46:15 +00004690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004693
4694 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004695 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4696 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4699 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4700 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004701 }
4702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004708
4709/*
4710 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 * - any side: calling mbedtls_ssl_renegotiate(),
4712 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4713 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004714 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004715 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004716 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004718int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004719{
Janos Follath865b3eb2019-12-16 11:46:15 +00004720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if ((ret = ssl_handshake_init(ssl)) != 0) {
4725 return ret;
4726 }
Paul Bakker48916f92012-09-16 19:57:18 +00004727
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004728 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4729 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4732 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4733 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004734 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004736 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004738 }
4739#endif
4740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4742 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4745 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4746 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004747 }
4748
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004752}
4753
4754/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004755 * Renegotiate current connection on client,
4756 * or request renegotiation on server
4757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (ssl == NULL || ssl->conf == NULL) {
4763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4764 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004766#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004767 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4769 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4771 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004774
4775 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 if (ssl->out_left != 0) {
4777 return mbedtls_ssl_flush_output(ssl);
4778 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004781 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004785 /*
4786 * On client, either start the renegotiation process or,
4787 * if already in progress, continue the handshake
4788 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4790 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004793
4794 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4795 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4796 return ret;
4797 }
4798 } else {
4799 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4800 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4801 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004802 }
4803 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004805
Gilles Peskine449bd832023-01-11 14:50:10 +01004806 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004811{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004812 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4813
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004815 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004817
Valerio Setti6f0441d2023-07-03 12:11:36 +02004818#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004819#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 if (ssl->handshake->group_list_heap_allocated) {
4821 mbedtls_free((void *) handshake->group_list);
4822 }
Brett Warrene0edc842021-08-17 09:53:13 +01004823 handshake->group_list = NULL;
4824#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004825#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004826
Ronald Crone68ab4f2022-10-05 12:46:29 +02004827#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004828#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (ssl->handshake->sig_algs_heap_allocated) {
4830 mbedtls_free((void *) handshake->sig_algs);
4831 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004832 handshake->sig_algs = NULL;
4833#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004834#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (ssl->handshake->certificate_request_context) {
4836 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004837 }
4838#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004839#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004840
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004841#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4843 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004844 handshake->async_in_progress = 0;
4845 }
4846#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4847
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004848#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004849#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004851#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004852 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004853#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004854#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004855#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004856#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004858#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004859 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004860#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004861#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004865#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004866#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004867 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004869#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004870
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004871#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004872#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004874 /*
4875 * Opaque keys are not stored in the handshake's data and it's the user
4876 * responsibility to destroy them. Clear ones, instead, are created by
4877 * the TLS library and should be destroyed at the same level
4878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4880 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004881 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004882 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4883#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004885#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004886#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004888 handshake->ecjpake_cache = NULL;
4889 handshake->ecjpake_cache_len = 0;
4890#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004891#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004892
Valerio Setti7aeec542023-07-05 18:57:21 +02004893#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004894 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004895 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004896 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004898#endif
4899
Ronald Cron73fe8df2022-10-05 14:31:43 +02004900#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004901#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004903 /* The maintenance of the external PSK key slot is the
4904 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 if (ssl->handshake->psk_opaque_is_internal) {
4906 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004907 ssl->handshake->psk_opaque_is_internal = 0;
4908 }
4909 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4910 }
Neil Armstronge952a302022-05-03 10:22:14 +02004911#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004913 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004914 }
Neil Armstronge952a302022-05-03 10:22:14 +02004915#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004916#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4919 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004920 /*
4921 * Free only the linked list wrapper, not the keys themselves
4922 * since the belong to the SNI callback
4923 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004925#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004926
Gilles Peskineeccd8882020-03-10 12:19:08 +01004927#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4929 if (handshake->ecrs_peer_cert != NULL) {
4930 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4931 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004932 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004933#endif
4934
Hanno Becker75173122019-02-06 16:18:31 +00004935#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4936 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004938#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4939
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004940#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4942 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004943#endif /* MBEDTLS_SSL_CLI_C &&
4944 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004945
4946#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 mbedtls_ssl_flight_free(handshake->flight);
4948 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004950
Valerio Settiea59c432023-07-25 11:14:03 +02004951#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004952 if (handshake->xxdh_psa_privkey_is_external == 0) {
4953 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 }
Valerio Settiea59c432023-07-25 11:14:03 +02004955#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004956
Ronald Cron6f135e12021-12-08 16:57:54 +01004957#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 mbedtls_ssl_transform_free(handshake->transform_handshake);
4959 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004960#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4962 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004963#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004964#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004965
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004966
4967#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4968 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4969 * processes datagrams and the fact that a datagram is allowed to have
4970 * several records in it, it is possible that the I/O buffers are not
4971 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4973 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004974#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004975
Jerry Yuba9c7272021-10-30 11:54:10 +08004976 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 mbedtls_platform_zeroize(handshake,
4978 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004979}
4980
Gilles Peskine449bd832023-01-11 14:50:10 +01004981void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004982{
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004984 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004989#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004990
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004991#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004992#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4993 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004995#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004997#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004998
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004999#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
5000 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00005001 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00005002#endif
5003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00005005}
5006
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005007#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005008
5009#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5010#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
5011#else
5012#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
5013#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5014
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005015#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005016
5017#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5018#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
5019#else
5020#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
5021#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5022
5023#if defined(MBEDTLS_SSL_ALPN)
5024#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
5025#else
5026#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
5027#endif /* MBEDTLS_SSL_ALPN */
5028
5029#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
5030#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
5031#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
5032#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
5033
5034#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 ((uint32_t) ( \
5036 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
5037 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
5038 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
5039 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
5040 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
5041 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
5042 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
5043 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005044
Chien Wong4e9683e2023-12-28 17:07:43 +08005045static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005046 MBEDTLS_VERSION_MAJOR,
5047 MBEDTLS_VERSION_MINOR,
5048 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5050 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5051 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5052 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5053 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005054};
5055
Paul Bakker5121ce52009-01-03 21:22:43 +00005056/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005057 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005058 *
5059 * The format of the serialized data is:
5060 * (in the presentation language of TLS, RFC 8446 section 3)
5061 *
5062 * // header
5063 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005064 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005065 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005066 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005067 * Note: When updating the format, remember to keep these
5068 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005069 *
5070 * // session sub-structure
5071 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5072 * // transform sub-structure
5073 * uint8 random[64]; // ServerHello.random+ClientHello.random
5074 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5075 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5076 * // fields from ssl_context
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005077 * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005078 * uint64 in_window_top; // DTLS: last validated record seq_num
5079 * uint64 in_window; // DTLS: bitmask for replay protection
5080 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5081 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5082 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5083 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5084 *
5085 * Note that many fields of the ssl_context or sub-structures are not
5086 * serialized, as they fall in one of the following categories:
5087 *
5088 * 1. forced value (eg in_left must be 0)
5089 * 2. pointer to dynamically-allocated memory (eg session, transform)
5090 * 3. value can be re-derived from other data (eg session keys from MS)
5091 * 4. value was temporary (eg content of input buffer)
5092 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005094int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5095 unsigned char *buf,
5096 size_t buf_len,
5097 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005098{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005099 unsigned char *p = buf;
5100 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005101 size_t session_len;
5102 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005103
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005104 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005105 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5106 * this function's documentation.
5107 *
5108 * These are due to assumptions/limitations in the implementation. Some of
5109 * them are likely to stay (no handshake in progress) some might go away
5110 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005111 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005112 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5114 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (ssl->handshake != NULL) {
5118 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005120 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005121 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (ssl->transform == NULL || ssl->session == NULL) {
5123 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5124 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005125 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005126 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005127 if (mbedtls_ssl_check_pending(ssl) != 0) {
5128 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5129 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005130 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 if (ssl->out_left != 0) {
5132 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005134 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005135 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5137 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5138 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005139 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005140 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005141 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5142 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005144 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005145 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5147 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005149 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005150 /* Renegotiation must not be enabled */
5151#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005152 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5153 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5154 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005155 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005156#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005157
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005158 /*
5159 * Version and format identifier
5160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (used <= buf_len) {
5164 memcpy(p, ssl_serialized_context_header,
5165 sizeof(ssl_serialized_context_header));
5166 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005167 }
5168
5169 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005170 * Session (length + data)
5171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5173 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5174 return ret;
5175 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005176
5177 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (used <= buf_len) {
5179 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005180 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 ret = ssl_session_save(ssl->session, 1,
5183 p, session_len, &session_len);
5184 if (ret != 0) {
5185 return ret;
5186 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005187
5188 p += session_len;
5189 }
5190
5191 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005192 * Transform
5193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 used += sizeof(ssl->transform->randbytes);
5195 if (used <= buf_len) {
5196 memcpy(p, ssl->transform->randbytes,
5197 sizeof(ssl->transform->randbytes));
5198 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005199 }
5200
5201#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005202 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005204 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005206 p += ssl->transform->in_cid_len;
5207
5208 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005210 p += ssl->transform->out_cid_len;
5211 }
5212#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5213
5214 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005215 * Saved fields from top-level ssl_context structure
5216 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005217 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (used <= buf_len) {
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005219 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005220 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005221 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005222
5223#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5224 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 if (used <= buf_len) {
5226 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005227 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005228
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005230 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005231 }
5232#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5233
5234#if defined(MBEDTLS_SSL_PROTO_DTLS)
5235 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005237 *p++ = ssl->disable_datagram_packing;
5238 }
5239#endif /* MBEDTLS_SSL_PROTO_DTLS */
5240
Jerry Yuae0b2e22021-10-08 15:21:19 +08005241 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (used <= buf_len) {
5243 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005244 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005245 }
5246
5247#if defined(MBEDTLS_SSL_PROTO_DTLS)
5248 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (used <= buf_len) {
5250 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005251 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005252 }
5253#endif /* MBEDTLS_SSL_PROTO_DTLS */
5254
5255#if defined(MBEDTLS_SSL_ALPN)
5256 {
5257 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005259 : 0;
5260
5261 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005263 *p++ = alpn_len;
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (ssl->alpn_chosen != NULL) {
5266 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005267 p += alpn_len;
5268 }
5269 }
5270 }
5271#endif /* MBEDTLS_SSL_ALPN */
5272
5273 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005274 * Done
5275 */
5276 *olen = used;
5277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if (used > buf_len) {
5279 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5280 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005285}
5286
5287/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005288 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005289 *
5290 * This internal version is wrapped by a public function that cleans up in
5291 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005292 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005294static int ssl_context_load(mbedtls_ssl_context *ssl,
5295 const unsigned char *buf,
5296 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005297{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005298 const unsigned char *p = buf;
5299 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005300 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005303 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005304#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005305
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005306 /*
5307 * The context should have been freshly setup or reset.
5308 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005309 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005310 * renegotiating, or if the user mistakenly loaded a session first.)
5311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5313 ssl->session != NULL) {
5314 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005315 }
5316
5317 /*
5318 * We can't check that the config matches the initial one, but we can at
5319 * least check it matches the requirements for serializing.
5320 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005321 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005322#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005323 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005324#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005325 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5326 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5327 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5328 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005330 }
5331
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005333
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005334 /*
5335 * Check version identifier
5336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005339 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005340
5341 if (memcmp(p, ssl_serialized_context_header,
5342 sizeof(ssl_serialized_context_header)) != 0) {
5343 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5344 }
5345 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005346
5347 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005348 * Session
5349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if ((size_t) (end - p) < 4) {
5351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5352 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005353
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005354 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005355 p += 4;
5356
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005357 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005358 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005359 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005360 ssl->session_in = ssl->session;
5361 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005362 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if ((size_t) (end - p) < session_len) {
5365 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5366 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005367
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 ret = ssl_session_load(ssl->session, 1, p, session_len);
5369 if (ret != 0) {
5370 mbedtls_ssl_session_free(ssl->session);
5371 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005372 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005373
5374 p += session_len;
5375
5376 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005377 * Transform
5378 */
5379
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005380 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005381 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005382#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005383 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005384 ssl->transform_in = ssl->transform;
5385 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005386 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005387#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005388
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5391 if (prf_func == NULL) {
5392 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5393 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005394
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005395 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5397 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5398 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005399
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 ret = ssl_tls12_populate_transform(ssl->transform,
5401 ssl->session->ciphersuite,
5402 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005403#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005405#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 prf_func,
5407 p, /* currently pointing to randbytes */
5408 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5409 ssl->conf->endpoint,
5410 ssl);
5411 if (ret != 0) {
5412 return ret;
5413 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005414#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005416
5417#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5418 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if ((size_t) (end - p) < 1) {
5420 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5421 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005422
5423 ssl->transform->in_cid_len = *p++;
5424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5426 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5427 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005428
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005430 p += ssl->transform->in_cid_len;
5431
5432 ssl->transform->out_cid_len = *p++;
5433
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5435 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5436 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005437
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005439 p += ssl->transform->out_cid_len;
5440#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5441
5442 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005443 * Saved fields from top-level ssl_context structure
5444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 if ((size_t) (end - p) < 4) {
5446 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5447 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005448
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005449 ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005450 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005451
5452#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if ((size_t) (end - p) < 16) {
5454 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5455 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005456
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005457 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005458 p += 8;
5459
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005460 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005461 p += 8;
5462#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5463
5464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if ((size_t) (end - p) < 1) {
5466 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5467 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005468
5469 ssl->disable_datagram_packing = *p++;
5470#endif /* MBEDTLS_SSL_PROTO_DTLS */
5471
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5473 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5474 }
5475 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5476 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005477
5478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if ((size_t) (end - p) < 2) {
5480 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5481 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005482
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005483 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005484 p += 2;
5485#endif /* MBEDTLS_SSL_PROTO_DTLS */
5486
5487#if defined(MBEDTLS_SSL_ALPN)
5488 {
5489 uint8_t alpn_len;
5490 const char **cur;
5491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 if ((size_t) (end - p) < 1) {
5493 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5494 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005495
5496 alpn_len = *p++;
5497
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005499 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5501 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005502 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005503 ssl->alpn_chosen = *cur;
5504 break;
5505 }
5506 }
5507 }
5508
5509 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5511 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5512 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005513
5514 p += alpn_len;
5515 }
5516#endif /* MBEDTLS_SSL_ALPN */
5517
5518 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005519 * Forced fields from top-level ssl_context structure
5520 *
5521 * Most of them already set to the correct value by mbedtls_ssl_init() and
5522 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5523 */
5524 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005525 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005526
Hanno Becker361b10d2019-08-30 10:42:49 +01005527 /* Adjust pointers for header fields of outgoing records to
5528 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005530
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005531#if defined(MBEDTLS_SSL_PROTO_DTLS)
5532 ssl->in_epoch = 1;
5533#endif
5534
5535 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5536 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005537 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5538 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (ssl->handshake != NULL) {
5540 mbedtls_ssl_handshake_free(ssl);
5541 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005542 ssl->handshake = NULL;
5543 }
5544
5545 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005546 * Done - should have consumed entire buffer
5547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 if (p != end) {
5549 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5550 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005551
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005553}
5554
5555/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005556 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005558int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5559 const unsigned char *buf,
5560 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005561{
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (ret != 0) {
5565 mbedtls_ssl_free(context);
5566 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005569}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005570#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005571
5572/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005573 * Free an SSL context
5574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005575void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005576{
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005578 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005584#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5585 size_t out_buf_len = ssl->out_buf_len;
5586#else
5587 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5588#endif
5589
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005590 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005591 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005592 }
5593
Gilles Peskine449bd832023-01-11 14:50:10 +01005594 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005595#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5596 size_t in_buf_len = ssl->in_buf_len;
5597#else
5598 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5599#endif
5600
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005601 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005602 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 }
5604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 if (ssl->transform) {
5606 mbedtls_ssl_transform_free(ssl->transform);
5607 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005608 }
5609
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 if (ssl->handshake) {
5611 mbedtls_ssl_handshake_free(ssl);
5612 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005613
5614#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5616 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005617#endif
5618
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 mbedtls_ssl_session_free(ssl->session_negotiate);
5620 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005621 }
5622
Ronald Cron6f135e12021-12-08 16:57:54 +01005623#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 mbedtls_ssl_transform_free(ssl->transform_application);
5625 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005626#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 if (ssl->session) {
5629 mbedtls_ssl_session_free(ssl->session);
5630 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005631 }
5632
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005633#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskinee61852e2025-02-12 23:28:48 +01005634 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02005635#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005636
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005637#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005639#endif
5640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005642
Paul Bakker86f04f42013-02-14 11:20:09 +01005643 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005645}
5646
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005647/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005648 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005649 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005650void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005651{
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005653}
5654
Gilles Peskineae270bf2021-06-02 00:05:29 +02005655/* The selection should be the same as mbedtls_x509_crt_profile_default in
5656 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005657 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005658 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005659 * about this list.
5660 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005661static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005662#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005663 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005664#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005665#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005666 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005667#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005668#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005669 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005670#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005671#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005672 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005673#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005674#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005675 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005676#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005677#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005678 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005679#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005680#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005681 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005682#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005683#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005684 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005685#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005686#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005687 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5688 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5689 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5690 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5691 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5692#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005693 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005694};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005695
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005696static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005697 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5698 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5699 0
5700};
5701
Ronald Crone68ab4f2022-10-05 12:46:29 +02005702#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005703
Jerry Yu909df7b2022-01-22 11:56:27 +08005704/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005705 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005706 * rules SHOULD be upheld.
5707 * - No duplicate entries.
5708 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005709 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005710 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005711 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005712static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005713
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005714#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005715 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005716 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005717 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005718 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5719#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005720
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005721#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005722 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005723 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005724 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005725 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5726#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005727
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005728#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005729 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005730 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005731 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005732 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5733#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005734
Yanray Wang1136fad2023-11-22 16:54:31 +08005735#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005736 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005737#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005738
Yanray Wang1136fad2023-11-22 16:54:31 +08005739#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005740 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005741#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005742
Yanray Wang1136fad2023-11-22 16:54:31 +08005743#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005744 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005745#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005746
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005747#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005748 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005749#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005750
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005751#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005752 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005753#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005754
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005755#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005756 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005757#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005758
Gabor Mezei15b95a62022-05-09 16:37:58 +02005759 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005760};
5761
5762/* NOTICE: see above */
5763#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5764static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005765
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005766#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005767#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005769#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005770#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5771 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005772#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005773#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005775#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005776#endif /* MBEDTLS_MD_CAN_SHA512 */
5777
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005778#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005779#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005781#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005782#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5783 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005784#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005785#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005787#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005788#endif /* MBEDTLS_MD_CAN_SHA384 */
5789
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005790#if defined(MBEDTLS_MD_CAN_SHA256)
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_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005793#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005794#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5795 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005796#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005797#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005799#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005800#endif /* MBEDTLS_MD_CAN_SHA256 */
5801
Gabor Mezei15b95a62022-05-09 16:37:58 +02005802 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005803};
5804#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005805
Jerry Yu909df7b2022-01-22 11:56:27 +08005806/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005807static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005808
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005809#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005810 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005811 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005812 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005813 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5814#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005815
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005816#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005817 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005818 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005819 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005820 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5821#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005822
Gabor Mezei15b95a62022-05-09 16:37:58 +02005823 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005824};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005825
Jerry Yu909df7b2022-01-22 11:56:27 +08005826/* NOTICE: see above */
5827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5828static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005829
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005830#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005831#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005833#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005834#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005835
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005836#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005837#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005839#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005840#endif /* MBEDTLS_MD_CAN_SHA384 */
5841
Gabor Mezei15b95a62022-05-09 16:37:58 +02005842 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005843};
Jerry Yu909df7b2022-01-22 11:56:27 +08005844#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5845
Ronald Crone68ab4f2022-10-05 12:46:29 +02005846#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005847
Chien Wong4e9683e2023-12-28 17:07:43 +08005848static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005849#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005850 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005851#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005852#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005853 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005854#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005855 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005856};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005857
Ronald Crone68ab4f2022-10-05 12:46:29 +02005858#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005859/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005860 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005861MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005862static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005863{
5864 size_t i, j;
5865 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005866
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5868 for (j = 0; j < i; j++) {
5869 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005870 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 }
5872 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5873 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5874 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005875 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005876 }
5877 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005879}
5880
Ronald Crone68ab4f2022-10-05 12:46:29 +02005881#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005882
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005883/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005884 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005885 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005886int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5887 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005888{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005889#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005891#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005892
Ronald Crone68ab4f2022-10-05 12:46:29 +02005893#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5895 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5896 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005897 }
5898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5900 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5901 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005902 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005903
5904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5906 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5907 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005908 }
5909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5911 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5912 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005913 }
5914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005915#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005916
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005917 /* Use the functions here so that they are covered in tests,
5918 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 mbedtls_ssl_conf_endpoint(conf, endpoint);
5920 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005921
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005922 /*
5923 * Things that are common to all presets
5924 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005925#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005927 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5928#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005929 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005930#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005931 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5932 * handling is disabled by default in Mbed TLS 3.6.x for backward
5933 * compatibility with client applications developed using Mbed TLS 3.5
5934 * or earlier with the default configuration.
5935 *
5936 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5937 * disabled, and a Mbed TLS client with the default configuration would
5938 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5939 * server.
5940 *
5941 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5942 * an Mbed TLS client with the default configuration establishes a
5943 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5944 * following the handshake the TLS 1.3 server sends NewSessionTicket
5945 * messages and the Mbed TLS client processes them, this results in
5946 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5947 * mbedtls_ssl_handshake(), ...) to eventually return an
5948 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5949 * (see the documentation of mbedtls_ssl_read() for more information on
5950 * that error code). Applications unaware of that TLS 1.3 specific non
5951 * fatal error code are then failing.
5952 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005953 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5954 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005955#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005956#endif
5957 }
5958#endif
5959
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005960#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5961 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5962#endif
5963
5964#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5965 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5966#endif
5967
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005968#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005969 conf->f_cookie_write = ssl_cookie_write_dummy;
5970 conf->f_cookie_check = ssl_cookie_check_dummy;
5971#endif
5972
5973#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5974 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5975#endif
5976
Janos Follath088ce432017-04-10 12:42:31 +01005977#if defined(MBEDTLS_SSL_SRV_C)
5978 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005979 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005980#endif
5981
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005982#if defined(MBEDTLS_SSL_PROTO_DTLS)
5983 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5984 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5985#endif
5986
5987#if defined(MBEDTLS_SSL_RENEGOTIATION)
5988 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 memset(conf->renego_period, 0x00, 2);
5990 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005991#endif
5992
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005993#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005995 const unsigned char dhm_p[] =
5996 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5997 const unsigned char dhm_g[] =
5998 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
6001 dhm_p, sizeof(dhm_p),
6002 dhm_g, sizeof(dhm_g))) != 0) {
6003 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01006004 }
6005 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02006006#endif
6007
Ronald Cron6f135e12021-12-08 16:57:54 +01006008#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006009
6010#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08006011 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006012#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08006013 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08006014#endif
6015#endif /* MBEDTLS_SSL_EARLY_DATA */
6016
Jerry Yud0766ec2022-09-22 10:46:57 +08006017#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08006018 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08006020#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01006021 /*
6022 * Allow all TLS 1.3 key exchange modes by default.
6023 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00006024 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01006025#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01006026
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006028#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00006029 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00006030 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006031#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00006033#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006034 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00006035#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01006036 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6037 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006038#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
6039 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6040 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
6041#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
6042 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6043 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
6044#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006046#endif
6047 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006048
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006049 /*
6050 * Preset-specific defaults
6051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006053 /*
6054 * NSA Suite B
6055 */
6056 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006057
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006058 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006059
6060#if defined(MBEDTLS_X509_CRT_PARSE_C)
6061 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006062#endif
6063
Ronald Crone68ab4f2022-10-05 12:46:29 +02006064#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006067 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006071#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006072
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006073#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006074 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006075#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006076 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006077 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006078
6079 /*
6080 * Default
6081 */
6082 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006083
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006084 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006085
6086#if defined(MBEDTLS_X509_CRT_PARSE_C)
6087 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6088#endif
6089
Ronald Crone68ab4f2022-10-05 12:46:29 +02006090#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006093 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006095#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006097#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006098
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006099#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006100 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006101#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006102 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006103
6104#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6105 conf->dhm_min_bitlen = 1024;
6106#endif
6107 }
6108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006110}
6111
6112/*
6113 * Free mbedtls_ssl_config
6114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006115void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006116{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006117 if (conf == NULL) {
6118 return;
6119 }
6120
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006121#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 mbedtls_mpi_free(&conf->dhm_P);
6123 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006124#endif
6125
Ronald Cron73fe8df2022-10-05 14:31:43 +02006126#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006129 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6130 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006131#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006133 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006134 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006135 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006136 }
6137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006139 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006140 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006141 conf->psk_identity_len = 0;
6142 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006143#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006144
6145#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006147#endif
6148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006150}
6151
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006152#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006153 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006154/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006157unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6161 return MBEDTLS_SSL_SIG_RSA;
6162 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006163#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006164#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6166 return MBEDTLS_SSL_SIG_ECDSA;
6167 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006170}
6171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006173{
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006175 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006177 case MBEDTLS_PK_ECDSA:
6178 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006180 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006182 }
6183}
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006186{
Gilles Peskine449bd832023-01-11 14:50:10 +01006187 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188#if defined(MBEDTLS_RSA_C)
6189 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006191#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006192#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006195#endif
6196 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006198 }
6199}
Valerio Settie9646ec2023-08-02 20:02:28 +02006200#endif /* MBEDTLS_PK_C &&
6201 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006202
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006203/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006204 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006206mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006207{
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006209#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006212#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006213#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006216#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006217#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006220#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006221#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006224#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006225#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006226 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006228#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006229#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006232#endif
6233 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006235 }
6236}
6237
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006238/*
6239 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006241unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006242{
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006244#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006245 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006246 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006247#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006248#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006249 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006251#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006252#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006253 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006255#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006256#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006257 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006259#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006260#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006261 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006263#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006264#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006265 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006267#endif
6268 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006270 }
6271}
6272
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006273/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006274 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006275 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006277int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006278{
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006280
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 if (group_list == NULL) {
6282 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006283 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006284
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 for (; *group_list != 0; group_list++) {
6286 if (*group_list == tls_id) {
6287 return 0;
6288 }
6289 }
6290
6291 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006292}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006293
Valerio Setti49e69072023-06-27 17:27:51 +02006294#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006295/*
6296 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006298int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006299{
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006301
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006303 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006304 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006307}
Valerio Setti49e69072023-06-27 17:27:51 +02006308#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006309
Valerio Setti18c9fed2022-12-30 17:44:24 +01006310static const struct {
6311 uint16_t tls_id;
6312 mbedtls_ecp_group_id ecp_group_id;
6313 psa_ecc_family_t psa_family;
6314 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006315} tls_id_match_table[] =
6316{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006317#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006318 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006319#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006320#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006321 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006322#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006323#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006324 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006325#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006326#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006327 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006328#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006329#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006330 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006331#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006332#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006333 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006334#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006335#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006336 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006337#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006338#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006339 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006340#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006341#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006342 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006343#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006344#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006345 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006346#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006347#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006348 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006349#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006350#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006351 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006352#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006353#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006354 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006355#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006356 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006357};
6358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006360 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006362{
Gilles Peskine449bd832023-01-11 14:50:10 +01006363 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6364 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006365 if (type != NULL) {
6366 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 }
6368 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006369 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006371 return PSA_SUCCESS;
6372 }
6373 }
6374
6375 return PSA_ERROR_NOT_SUPPORTED;
6376}
6377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006379{
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6381 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006382 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006384 }
6385
6386 return MBEDTLS_ECP_DP_NONE;
6387}
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006390{
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6392 i++) {
6393 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006394 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006396 }
6397
6398 return 0;
6399}
6400
Valerio Setti67419f02023-01-04 16:12:42 +01006401#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006402static const struct {
6403 uint16_t tls_id;
6404 const char *name;
6405} tls_id_curve_name_table[] =
6406{
Valerio Setti54e23792023-07-07 10:49:27 +02006407 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6408 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6409 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6410 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6411 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6412 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6413 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6414 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6415 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6416 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6417 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6418 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6419 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006420 { 0, NULL },
6421};
6422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006424{
Valerio Settiacd32c02023-06-29 18:06:29 +02006425 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6426 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6427 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006429 }
6430
6431 return NULL;
6432}
Valerio Setti67419f02023-01-04 16:12:42 +01006433#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006434
Jerry Yu148165c2021-09-24 23:20:59 +08006435#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006436int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6437 const mbedtls_md_type_t md,
6438 unsigned char *dst,
6439 size_t dst_len,
6440 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006441{
Ronald Cronf6893e12022-01-07 22:09:01 +01006442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6443 psa_hash_operation_t *hash_operation_to_clone;
6444 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6445
Jerry Yu148165c2021-09-24 23:20:59 +08006446 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006447
Gilles Peskine449bd832023-01-11 14:50:10 +01006448 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006449#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 case MBEDTLS_MD_SHA384:
6451 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6452 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006453#endif
6454
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006455#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 case MBEDTLS_MD_SHA256:
6457 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6458 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006459#endif
6460
Gilles Peskine449bd832023-01-11 14:50:10 +01006461 default:
6462 goto exit;
6463 }
6464
6465 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6466 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006467 goto exit;
6468 }
6469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6471 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006474
6475exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006476#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6477 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006478 (void) ssl;
6479#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006480 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006481}
6482#else /* MBEDTLS_USE_PSA_CRYPTO */
6483
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006484#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006485MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006486static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6487 unsigned char *dst,
6488 size_t dst_len,
6489 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006490{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006491 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006492 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 if (dst_len < 48) {
6495 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6496 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006497
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006498 mbedtls_md_init(&sha384);
6499 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006500 if (ret != 0) {
6501 goto exit;
6502 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006503 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006504 if (ret != 0) {
6505 goto exit;
6506 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006507
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006508 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006509 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006510 goto exit;
6511 }
6512
6513 *olen = 48;
6514
6515exit:
6516
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006517 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006519}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006520#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006521
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006522#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006523MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006524static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6525 unsigned char *dst,
6526 size_t dst_len,
6527 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006528{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006529 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006530 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 if (dst_len < 32) {
6533 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6534 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006535
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006536 mbedtls_md_init(&sha256);
6537 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6538 if (ret != 0) {
6539 goto exit;
6540 }
6541 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6542 if (ret != 0) {
6543 goto exit;
6544 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006545
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006546 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6547 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006548 goto exit;
6549 }
6550
6551 *olen = 32;
6552
6553exit:
6554
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006555 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006557}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006558#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006559
Gilles Peskine449bd832023-01-11 14:50:10 +01006560int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6561 const mbedtls_md_type_t md,
6562 unsigned char *dst,
6563 size_t dst_len,
6564 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006565{
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006567
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006568#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 case MBEDTLS_MD_SHA384:
6570 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006571#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006572
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006573#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 case MBEDTLS_MD_SHA256:
6575 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006576#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006579#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6580 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 (void) ssl;
6582 (void) dst;
6583 (void) dst_len;
6584 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006585#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006587 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006589}
XiaokangQian647719a2021-12-07 09:16:29 +00006590
Jerry Yu148165c2021-09-24 23:20:59 +08006591#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006592
Ronald Crone68ab4f2022-10-05 12:46:29 +02006593#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006594/* mbedtls_ssl_parse_sig_alg_ext()
6595 *
6596 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6597 * value (TLS 1.3 RFC8446):
6598 * enum {
6599 * ....
6600 * ecdsa_secp256r1_sha256( 0x0403 ),
6601 * ecdsa_secp384r1_sha384( 0x0503 ),
6602 * ecdsa_secp521r1_sha512( 0x0603 ),
6603 * ....
6604 * } SignatureScheme;
6605 *
6606 * struct {
6607 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6608 * } SignatureSchemeList;
6609 *
6610 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6611 * value (TLS 1.2 RFC5246):
6612 * enum {
6613 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6614 * sha512(6), (255)
6615 * } HashAlgorithm;
6616 *
6617 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6618 * SignatureAlgorithm;
6619 *
6620 * struct {
6621 * HashAlgorithm hash;
6622 * SignatureAlgorithm signature;
6623 * } SignatureAndHashAlgorithm;
6624 *
6625 * SignatureAndHashAlgorithm
6626 * supported_signature_algorithms<2..2^16-2>;
6627 *
6628 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6629 * generalization of the TLS 1.2 signature algorithm extension.
6630 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6631 * `SignatureScheme` field of TLS 1.3
6632 *
6633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006634int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6635 const unsigned char *buf,
6636 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006637{
6638 const unsigned char *p = buf;
6639 size_t supported_sig_algs_len = 0;
6640 const unsigned char *supported_sig_algs_end;
6641 uint16_t sig_alg;
6642 uint32_t common_idx = 0;
6643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6645 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006646 p += 2;
6647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 memset(ssl->handshake->received_sig_algs, 0,
6649 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006652 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 while (p < supported_sig_algs_end) {
6654 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6655 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006656 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6658 sig_alg,
6659 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6662 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6663 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006664 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006665 }
6666#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6669 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006672 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6673 common_idx += 1;
6674 }
6675 }
6676 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 if (p != end) {
6678 MBEDTLS_SSL_DEBUG_MSG(1,
6679 ("Signature algorithms extension length misaligned"));
6680 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6681 MBEDTLS_ERR_SSL_DECODE_ERROR);
6682 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006683 }
6684
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 if (common_idx == 0) {
6686 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6687 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6688 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6689 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006690 }
6691
Gabor Mezei15b95a62022-05-09 16:37:58 +02006692 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006694}
6695
Ronald Crone68ab4f2022-10-05 12:46:29 +02006696#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006697
Jerry Yudc7bd172022-02-17 13:44:15 +08006698#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6699
6700#if defined(MBEDTLS_USE_PSA_CRYPTO)
6701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6703 mbedtls_svc_key_id_t key,
6704 psa_algorithm_t alg,
6705 const unsigned char *raw_psk, size_t raw_psk_length,
6706 const unsigned char *seed, size_t seed_length,
6707 const unsigned char *label, size_t label_length,
6708 const unsigned char *other_secret,
6709 size_t other_secret_length,
6710 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006711{
6712 psa_status_t status;
6713
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 status = psa_key_derivation_setup(derivation, alg);
6715 if (status != PSA_SUCCESS) {
6716 return status;
6717 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6720 status = psa_key_derivation_input_bytes(derivation,
6721 PSA_KEY_DERIVATION_INPUT_SEED,
6722 seed, seed_length);
6723 if (status != PSA_SUCCESS) {
6724 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006725 }
6726
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 if (other_secret != NULL) {
6728 status = psa_key_derivation_input_bytes(derivation,
6729 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6730 other_secret, other_secret_length);
6731 if (status != PSA_SUCCESS) {
6732 return status;
6733 }
6734 }
6735
6736 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006737 status = psa_key_derivation_input_bytes(
6738 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 raw_psk, raw_psk_length);
6740 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006741 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006743 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (status != PSA_SUCCESS) {
6745 return status;
6746 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 status = psa_key_derivation_input_bytes(derivation,
6749 PSA_KEY_DERIVATION_INPUT_LABEL,
6750 label, label_length);
6751 if (status != PSA_SUCCESS) {
6752 return status;
6753 }
6754 } else {
6755 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006756 }
6757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 status = psa_key_derivation_set_capacity(derivation, capacity);
6759 if (status != PSA_SUCCESS) {
6760 return status;
6761 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006764}
6765
Andrzej Kurek57d10632022-10-24 10:32:01 -04006766#if defined(PSA_WANT_ALG_SHA_384) || \
6767 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006768MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006769static int tls_prf_generic(mbedtls_md_type_t md_type,
6770 const unsigned char *secret, size_t slen,
6771 const char *label,
6772 const unsigned char *random, size_t rlen,
6773 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006774{
6775 psa_status_t status;
6776 psa_algorithm_t alg;
6777 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6778 psa_key_derivation_operation_t derivation =
6779 PSA_KEY_DERIVATION_OPERATION_INIT;
6780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006782 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006783 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006784 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006786
6787 /* Normally a "secret" should be long enough to be impossible to
6788 * find by brute force, and in particular should not be empty. But
6789 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6790 * and for this use case it makes sense to have a 0-length "secret".
6791 * Since the key API doesn't allow importing a key of length 0,
6792 * keep master_key=0, which setup_psa_key_derivation() understands
6793 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006795 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6797 psa_set_key_algorithm(&key_attributes, alg);
6798 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006799
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6801 if (status != PSA_SUCCESS) {
6802 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6803 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006804 }
6805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 status = setup_psa_key_derivation(&derivation,
6807 master_key, alg,
6808 NULL, 0,
6809 random, rlen,
6810 (unsigned char const *) label,
6811 (size_t) strlen(label),
6812 NULL, 0,
6813 dlen);
6814 if (status != PSA_SUCCESS) {
6815 psa_key_derivation_abort(&derivation);
6816 psa_destroy_key(master_key);
6817 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006818 }
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6821 if (status != PSA_SUCCESS) {
6822 psa_key_derivation_abort(&derivation);
6823 psa_destroy_key(master_key);
6824 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006825 }
6826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 status = psa_key_derivation_abort(&derivation);
6828 if (status != PSA_SUCCESS) {
6829 psa_destroy_key(master_key);
6830 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006831 }
6832
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 if (!mbedtls_svc_key_id_is_null(master_key)) {
6834 status = psa_destroy_key(master_key);
6835 }
6836 if (status != PSA_SUCCESS) {
6837 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6838 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006841}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006842#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006843#else /* MBEDTLS_USE_PSA_CRYPTO */
6844
Andrzej Kurek57d10632022-10-24 10:32:01 -04006845#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006846 (defined(MBEDTLS_MD_CAN_SHA256) || \
6847 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006848MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006849static int tls_prf_generic(mbedtls_md_type_t md_type,
6850 const unsigned char *secret, size_t slen,
6851 const char *label,
6852 const unsigned char *random, size_t rlen,
6853 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006854{
6855 size_t nb;
6856 size_t i, j, k, md_len;
6857 unsigned char *tmp;
6858 size_t tmp_len = 0;
6859 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6860 const mbedtls_md_info_t *md_info;
6861 mbedtls_md_context_t md_ctx;
6862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6867 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6868 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 tmp_len = md_len + strlen(label) + rlen;
6873 tmp = mbedtls_calloc(1, tmp_len);
6874 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006875 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6876 goto exit;
6877 }
6878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 nb = strlen(label);
6880 memcpy(tmp + md_len, label, nb);
6881 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006882 nb += rlen;
6883
6884 /*
6885 * Compute P_<hash>(secret, label + random)[0..dlen]
6886 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006888 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6892 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006893 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 }
6895 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6896 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006897 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 }
6899 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6900 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006901 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 for (i = 0; i < dlen; i += md_len) {
6905 ret = mbedtls_md_hmac_reset(&md_ctx);
6906 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006907 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 }
6909 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6910 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006911 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 }
6913 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6914 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006915 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006917
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 ret = mbedtls_md_hmac_reset(&md_ctx);
6919 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006920 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 }
6922 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6923 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006924 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 }
6926 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6927 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006928 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006932
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006934 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006936 }
6937
6938exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 if (tmp != NULL) {
6942 mbedtls_platform_zeroize(tmp, tmp_len);
6943 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006948
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006950}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006951#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006952#endif /* MBEDTLS_USE_PSA_CRYPTO */
6953
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006954#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006955MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006956static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6957 const char *label,
6958 const unsigned char *random, size_t rlen,
6959 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006960{
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6962 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006963}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006964#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006965
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006966#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006968static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6969 const char *label,
6970 const unsigned char *random, size_t rlen,
6971 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006972{
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6974 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006975}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006976#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006977
Jerry Yuf009d862022-02-17 14:01:37 +08006978/*
6979 * Set appropriate PRF function and other SSL / TLS1.2 functions
6980 *
6981 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006982 * - hash associated with the ciphersuite (only used by TLS 1.2)
6983 *
6984 * Outputs:
6985 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6986 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006988static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6989 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006990{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006991#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006993 handshake->tls_prf = tls_prf_sha384;
6994 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6995 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006997#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006998#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006999 {
Ronald Cron81591aa2022-03-07 09:05:51 +01007000 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08007001 handshake->tls_prf = tls_prf_sha256;
7002 handshake->calc_verify = ssl_calc_verify_tls_sha256;
7003 handshake->calc_finished = ssl_calc_finished_tls_sha256;
7004 }
Ronald Cron81591aa2022-03-07 09:05:51 +01007005#else
Jerry Yuf009d862022-02-17 14:01:37 +08007006 {
Jerry Yuf009d862022-02-17 14:01:37 +08007007 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01007008 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08007010 }
Ronald Cron81591aa2022-03-07 09:05:51 +01007011#endif
Jerry Yuf009d862022-02-17 14:01:37 +08007012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08007014}
Jerry Yud6ab2352022-02-17 14:03:43 +08007015
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007016/*
7017 * Compute master secret if needed
7018 *
7019 * Parameters:
7020 * [in/out] handshake
7021 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
7022 * (PSA-PSK) ciphersuite_info, psk_opaque
7023 * [out] premaster (cleared)
7024 * [out] master
7025 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
7026 * debug: conf->f_dbg, conf->p_dbg
7027 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01007028 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007029 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007030MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007031static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
7032 unsigned char *master,
7033 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007034{
7035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7036
7037 /* cf. RFC 5246, Section 8.1:
7038 * "The master secret is always exactly 48 bytes in length." */
7039 size_t const master_secret_len = 48;
7040
7041#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7042 unsigned char session_hash[48];
7043#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
7044
7045 /* The label for the KDF used for key expansion.
7046 * This is either "master secret" or "extended master secret"
7047 * depending on whether the Extended Master Secret extension
7048 * is used. */
7049 char const *lbl = "master secret";
7050
Przemek Stekielae4ed302022-04-05 17:15:55 +02007051 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007052 * - If the Extended Master Secret extension is not used,
7053 * this is ClientHello.Random + ServerHello.Random
7054 * (see Sect. 8.1 in RFC 5246).
7055 * - If the Extended Master Secret extension is used,
7056 * this is the transcript of the handshake so far.
7057 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007058 unsigned char const *seed = handshake->randbytes;
7059 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007060
7061#if !defined(MBEDTLS_DEBUG_C) && \
7062 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7063 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007065 ssl = NULL; /* make sure we don't use it except for those cases */
7066 (void) ssl;
7067#endif
7068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 if (handshake->resume != 0) {
7070 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7071 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007072 }
7073
7074#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007076 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007077 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007078 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7079 if (ret != 0) {
7080 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7081 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007082
Gilles Peskine449bd832023-01-11 14:50:10 +01007083 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7084 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007085 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007086#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007087
Przemek Stekiel99114f32022-04-22 11:20:09 +02007088#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007089 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007091 /* Perform PSK-to-MS expansion in a single step. */
7092 psa_status_t status;
7093 psa_algorithm_t alg;
7094 mbedtls_svc_key_id_t psk;
7095 psa_key_derivation_operation_t derivation =
7096 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007097 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007104 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007106 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007108
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007109 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007113 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007114 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007115 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007116 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007117 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007118 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007119 other_secret_len = 48;
7120 other_secret = handshake->premaster + 2;
7121 break;
7122 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007123 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007124 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7125 other_secret = handshake->premaster + 2;
7126 break;
7127 default:
7128 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007129 }
7130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 status = setup_psa_key_derivation(&derivation, psk, alg,
7132 ssl->conf->psk, ssl->conf->psk_len,
7133 seed, seed_len,
7134 (unsigned char const *) lbl,
7135 (size_t) strlen(lbl),
7136 other_secret, other_secret_len,
7137 master_secret_len);
7138 if (status != PSA_SUCCESS) {
7139 psa_key_derivation_abort(&derivation);
7140 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007141 }
7142
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 status = psa_key_derivation_output_bytes(&derivation,
7144 master,
7145 master_secret_len);
7146 if (status != PSA_SUCCESS) {
7147 psa_key_derivation_abort(&derivation);
7148 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007149 }
7150
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 status = psa_key_derivation_abort(&derivation);
7152 if (status != PSA_SUCCESS) {
7153 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7154 }
7155 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007156#endif
7157 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007158#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7160 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007161 psa_status_t status;
7162 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7163 psa_key_derivation_operation_t derivation =
7164 PSA_KEY_DERIVATION_OPERATION_INIT;
7165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007167
7168 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 status = psa_key_derivation_setup(&derivation, alg);
7171 if (status != PSA_SUCCESS) {
7172 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007173 }
7174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 status = psa_key_derivation_set_capacity(&derivation,
7176 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7177 if (status != PSA_SUCCESS) {
7178 psa_key_derivation_abort(&derivation);
7179 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007180 }
7181
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7183 &derivation);
7184 if (status != PSA_SUCCESS) {
7185 psa_key_derivation_abort(&derivation);
7186 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007187 }
7188
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 status = psa_key_derivation_output_bytes(&derivation,
7190 handshake->premaster,
7191 handshake->pmslen);
7192 if (status != PSA_SUCCESS) {
7193 psa_key_derivation_abort(&derivation);
7194 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7195 }
7196
7197 status = psa_key_derivation_abort(&derivation);
7198 if (status != PSA_SUCCESS) {
7199 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007200 }
7201 }
7202#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7204 lbl, seed, seed_len,
7205 master,
7206 master_secret_len);
7207 if (ret != 0) {
7208 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7209 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007210 }
7211
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7213 handshake->premaster,
7214 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007215
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 mbedtls_platform_zeroize(handshake->premaster,
7217 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007218 }
7219
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007221}
7222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007224{
7225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7226 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7227 ssl->handshake->ciphersuite_info;
7228
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007230
7231 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007233 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 if (ret != 0) {
7235 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7236 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007237 }
7238
7239 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 ret = ssl_compute_master(ssl->handshake,
7241 ssl->session_negotiate->master,
7242 ssl);
7243 if (ret != 0) {
7244 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7245 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007246 }
7247
7248 /* Swap the client and server random values:
7249 * - MS derivation wanted client+server (RFC 5246 8.1)
7250 * - key derivation wants server+client (RFC 5246 6.3) */
7251 {
7252 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 memcpy(tmp, ssl->handshake->randbytes, 64);
7254 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7255 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7256 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007257 }
7258
7259 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7261 ssl->session_negotiate->ciphersuite,
7262 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007263#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007265#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 ssl->handshake->tls_prf,
7267 ssl->handshake->randbytes,
7268 ssl->tls_version,
7269 ssl->conf->endpoint,
7270 ssl);
7271 if (ret != 0) {
7272 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7273 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007274 }
7275
7276 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7278 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007283}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007284
Gilles Peskine449bd832023-01-11 14:50:10 +01007285int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007286{
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007288#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007289 case MBEDTLS_SSL_HASH_SHA384:
7290 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7291 break;
7292#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007293#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007294 case MBEDTLS_SSL_HASH_SHA256:
7295 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7296 break;
7297#endif
7298 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007300 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007301#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7302 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007303 (void) ssl;
7304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007306}
7307
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007308#if defined(MBEDTLS_USE_PSA_CRYPTO)
7309static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7310 const psa_hash_operation_t *hs_op,
7311 size_t buffer_size,
7312 unsigned char *hash,
7313 size_t *hlen)
7314{
7315 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007316 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007317
7318#if !defined(MBEDTLS_DEBUG_C)
7319 (void) ssl;
7320#endif
7321 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007322 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007323 if (status != PSA_SUCCESS) {
7324 goto exit;
7325 }
7326
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007327 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007328 if (status != PSA_SUCCESS) {
7329 goto exit;
7330 }
7331
7332 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7333 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7334
7335exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007336 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007337 return mbedtls_md_error_from_psa(status);
7338}
7339#else
7340static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7341 const mbedtls_md_context_t *hs_ctx,
7342 unsigned char *hash,
7343 size_t *hlen)
7344{
7345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007346 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007347
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007348 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007349
7350#if !defined(MBEDTLS_DEBUG_C)
7351 (void) ssl;
7352#endif
7353 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7354
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007355 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007356 if (ret != 0) {
7357 goto exit;
7358 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007359 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007360 if (ret != 0) {
7361 goto exit;
7362 }
7363
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007364 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007365 if (ret != 0) {
7366 goto exit;
7367 }
7368
7369 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7370
7371 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7372 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7373
7374exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007375 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007376 return ret;
7377}
7378#endif /* MBEDTLS_USE_PSA_CRYPTO */
7379
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007380#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007381int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7382 unsigned char *hash,
7383 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007384{
7385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007386 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7387 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007388#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007389 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7390 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007391#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007392}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007393#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007394
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007395#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007396int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7397 unsigned char *hash,
7398 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007399{
7400#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007401 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7402 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007403#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007404 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7405 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007406#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007407}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007408#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007409
Neil Armstrong80f6f322022-05-03 17:56:38 +02007410#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7411 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007412int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007413{
7414 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007416 const unsigned char *psk = NULL;
7417 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007421 /*
7422 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007423 * checked before calling this function.
7424 *
7425 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007426 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7429 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7430 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007431 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007432 }
7433
7434 /*
7435 * PMS = struct {
7436 * opaque other_secret<0..2^16-1>;
7437 * opaque psk<0..2^16-1>;
7438 * };
7439 * with "other_secret" depending on the particular key exchange
7440 */
7441#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7443 if (end - p < 2) {
7444 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7445 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007446
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007448 p += 2;
7449
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 if (end < p || (size_t) (end - p) < psk_len) {
7451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7452 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007455 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007457#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7458#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007460 /*
7461 * other_secret already set by the ClientKeyExchange message,
7462 * and is 48 bytes long
7463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 if (end - p < 2) {
7465 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7466 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007467
7468 *p++ = 0;
7469 *p++ = 48;
7470 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007472#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7473#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7476 size_t len;
7477
7478 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007480 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7482 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7483 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007484 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007486 p += 2 + len;
7487
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7489 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007490#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007491#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7494 size_t zlen;
7495
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007497 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7499 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7500 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007501 }
7502
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007504 p += 2 + zlen;
7505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7507 MBEDTLS_DEBUG_ECDH_Z);
7508 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007509#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007510 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7512 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007513 }
7514
7515 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 if (end - p < 2) {
7517 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7518 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007519
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007521 p += 2;
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 if (end < p || (size_t) (end - p) < psk_len) {
7524 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7525 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007528 p += psk_len;
7529
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007530 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007533}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007534#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007535
7536#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007538static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007539
7540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007541int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007542{
7543 /* If renegotiation is not enforced, retransmit until we would reach max
7544 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007546 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7547 unsigned char doublings = 1;
7548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007550 ++doublings;
7551 ratio >>= 1;
7552 }
7553
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 if (++ssl->renego_records_seen > doublings) {
7555 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7556 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007557 }
7558 }
7559
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007561}
7562#endif
7563#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007564
Jerry Yud9526692022-02-17 14:23:47 +08007565/*
7566 * Handshake functions
7567 */
7568#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7569/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007570int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007571{
7572 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7573 ssl->handshake->ciphersuite_info;
7574
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7578 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007579 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007581 }
7582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7584 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007585}
7586
Gilles Peskine449bd832023-01-11 14:50:10 +01007587int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007588{
7589 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7590 ssl->handshake->ciphersuite_info;
7591
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7595 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007596 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007598 }
7599
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7601 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007602}
7603
7604#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7605/* Some certificate support -> implement write and parse */
7606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007608{
7609 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7610 size_t i, n;
7611 const mbedtls_x509_crt *crt;
7612 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7613 ssl->handshake->ciphersuite_info;
7614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007616
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7618 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007619 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007621 }
7622
7623#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7625 if (ssl->handshake->client_auth == 0) {
7626 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007627 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007629 }
7630 }
7631#endif /* MBEDTLS_SSL_CLI_C */
7632#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7634 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007635 /* Should never happen because we shouldn't have picked the
7636 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007638 }
7639 }
7640#endif
7641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007643
7644 /*
7645 * 0 . 0 handshake type
7646 * 1 . 3 handshake length
7647 * 4 . 6 length of all certs
7648 * 7 . 9 length of cert. 1
7649 * 10 . n-1 peer certificate
7650 * n . n+2 length of cert. 2
7651 * n+3 . ... upper level cert, etc.
7652 */
7653 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007657 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7659 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7660 " > %" MBEDTLS_PRINTF_SIZET,
7661 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7662 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007663 }
7664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7666 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7667 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007668
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007670 i += n; crt = crt->next;
7671 }
7672
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7674 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7675 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007676
7677 ssl->out_msglen = i;
7678 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7679 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7680
7681 ssl->state++;
7682
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7685 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007686 }
7687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007689
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007691}
7692
7693#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7694
7695#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007697static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7698 unsigned char *crt_buf,
7699 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007700{
7701 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7702
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 if (peer_crt == NULL) {
7704 return -1;
7705 }
Jerry Yud9526692022-02-17 14:23:47 +08007706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 if (peer_crt->raw.len != crt_buf_len) {
7708 return -1;
7709 }
Jerry Yud9526692022-02-17 14:23:47 +08007710
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007712}
7713#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007715static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7716 unsigned char *crt_buf,
7717 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007718{
7719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7720 unsigned char const * const peer_cert_digest =
7721 ssl->session->peer_cert_digest;
7722 mbedtls_md_type_t const peer_cert_digest_type =
7723 ssl->session->peer_cert_digest_type;
7724 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007726 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7727 size_t digest_len;
7728
Gilles Peskine449bd832023-01-11 14:50:10 +01007729 if (peer_cert_digest == NULL || digest_info == NULL) {
7730 return -1;
7731 }
Jerry Yud9526692022-02-17 14:23:47 +08007732
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 digest_len = mbedtls_md_get_size(digest_info);
7734 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7735 return -1;
7736 }
Jerry Yud9526692022-02-17 14:23:47 +08007737
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7739 if (ret != 0) {
7740 return -1;
7741 }
Jerry Yud9526692022-02-17 14:23:47 +08007742
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007744}
7745#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7746#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7747
7748/*
7749 * Once the certificate message is read, parse it into a cert chain and
7750 * perform basic checks, but leave actual verification to the caller
7751 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007753static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7754 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007755{
7756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7757#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007759#endif
7760 size_t i, n;
7761 uint8_t alert;
7762
Gilles Peskine449bd832023-01-11 14:50:10 +01007763 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7764 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7765 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7766 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7767 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007768 }
7769
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7771 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7772 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7773 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007774 }
7775
Gilles Peskine449bd832023-01-11 14:50:10 +01007776 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7777 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7778 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7779 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7780 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007781 }
7782
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007784
7785 /*
7786 * Same message structure as in mbedtls_ssl_write_certificate()
7787 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007788 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 if (ssl->in_msg[i] != 0 ||
7791 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7792 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7793 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7794 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7795 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007796 }
7797
7798 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7799 i += 3;
7800
7801 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007803 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 if (i + 3 > ssl->in_hslen) {
7805 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7806 mbedtls_ssl_send_alert_message(ssl,
7807 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7808 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7809 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007810 }
7811 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7812 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007813 if (ssl->in_msg[i] != 0) {
7814 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7815 mbedtls_ssl_send_alert_message(ssl,
7816 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7817 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7818 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007819 }
7820
7821 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007822 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007823 i += 3;
7824
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 if (n < 128 || i + n > ssl->in_hslen) {
7826 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7827 mbedtls_ssl_send_alert_message(ssl,
7828 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7829 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7830 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007831 }
7832
7833 /* Check if we're handling the first CRT in the chain. */
7834#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007836 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007838 /* During client-side renegotiation, check that the server's
7839 * end-CRTs hasn't changed compared to the initial handshake,
7840 * mitigating the triple handshake attack. On success, reuse
7841 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7843 if (ssl_check_peer_crt_unchanged(ssl,
7844 &ssl->in_msg[i],
7845 n) != 0) {
7846 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7847 mbedtls_ssl_send_alert_message(ssl,
7848 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7849 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7850 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007851 }
7852
7853 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007855 }
7856#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7857
7858 /* Parse the next certificate in the chain. */
7859#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007861#else
7862 /* If we don't need to store the CRT chain permanently, parse
7863 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007865#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007867 case 0: /*ok*/
7868 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7869 /* Ignore certificate with an unknown algorithm: maybe a
7870 prior certificate was already trusted. */
7871 break;
7872
7873 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7874 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7875 goto crt_parse_der_failed;
7876
7877 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7878 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7879 goto crt_parse_der_failed;
7880
7881 default:
7882 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007883crt_parse_der_failed:
7884 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7885 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7886 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007887 }
7888
7889 i += n;
7890 }
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7893 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007894}
7895
7896#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007897MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007898static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007899{
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7901 return -1;
7902 }
Jerry Yud9526692022-02-17 14:23:47 +08007903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007905 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7906 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7908 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7909 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007912}
7913#endif /* MBEDTLS_SSL_SRV_C */
7914
7915/* Check if a certificate message is expected.
7916 * Return either
7917 * - SSL_CERTIFICATE_EXPECTED, or
7918 * - SSL_CERTIFICATE_SKIP
7919 * indicating whether a Certificate message is expected or not.
7920 */
7921#define SSL_CERTIFICATE_EXPECTED 0
7922#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007923MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007924static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7925 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007926{
7927 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7928 ssl->handshake->ciphersuite_info;
7929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7931 return SSL_CERTIFICATE_SKIP;
7932 }
Jerry Yud9526692022-02-17 14:23:47 +08007933
7934#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7936 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7937 return SSL_CERTIFICATE_SKIP;
7938 }
Jerry Yud9526692022-02-17 14:23:47 +08007939
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007941 ssl->session_negotiate->verify_result =
7942 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007944 }
7945 }
7946#else
7947 ((void) authmode);
7948#endif /* MBEDTLS_SSL_SRV_C */
7949
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007951}
7952
Jerry Yud9526692022-02-17 14:23:47 +08007953#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007954MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007955static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7956 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007957{
7958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7959 /* Remember digest of the peer's end-CRT. */
7960 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7962 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7963 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7964 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7965 mbedtls_ssl_send_alert_message(ssl,
7966 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7967 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007968
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007970 }
7971
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 ret = mbedtls_md(mbedtls_md_info_from_type(
7973 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7974 start, len,
7975 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007976
7977 ssl->session_negotiate->peer_cert_digest_type =
7978 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7979 ssl->session_negotiate->peer_cert_digest_len =
7980 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7981
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007983}
7984
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007986static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7987 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007988{
7989 unsigned char *end = start + len;
7990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7991
7992 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7994 ret = mbedtls_pk_parse_subpubkey(&start, end,
7995 &ssl->handshake->peer_pubkey);
7996 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007997 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007999 }
8000
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08008002}
8003#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8004
Gilles Peskine449bd832023-01-11 14:50:10 +01008005int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08008006{
8007 int ret = 0;
8008 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02008009 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08008010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8011 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
8012 ? ssl->handshake->sni_authmode
8013 : ssl->conf->authmode;
8014#else
8015 const int authmode = ssl->conf->authmode;
8016#endif
8017 void *rs_ctx = NULL;
8018 mbedtls_x509_crt *chain = NULL;
8019
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008021
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
8023 if (crt_expected == SSL_CERTIFICATE_SKIP) {
8024 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008025 goto exit;
8026 }
8027
8028#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008029 if (ssl->handshake->ecrs_enabled &&
8030 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08008031 chain = ssl->handshake->ecrs_peer_cert;
8032 ssl->handshake->ecrs_peer_cert = NULL;
8033 goto crt_verify;
8034 }
8035#endif
8036
Gilles Peskine449bd832023-01-11 14:50:10 +01008037 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008038 /* mbedtls_ssl_read_record may have sent an alert already. We
8039 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08008041 goto exit;
8042 }
8043
8044#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008046 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8047
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08008049 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 }
Jerry Yud9526692022-02-17 14:23:47 +08008051
8052 goto exit;
8053 }
8054#endif /* MBEDTLS_SSL_SRV_C */
8055
8056 /* Clear existing peer CRT structure in case we tried to
8057 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008059
Gilles Peskine449bd832023-01-11 14:50:10 +01008060 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8061 if (chain == NULL) {
8062 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8063 sizeof(mbedtls_x509_crt)));
8064 mbedtls_ssl_send_alert_message(ssl,
8065 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8066 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008067
8068 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8069 goto exit;
8070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008071 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 ret = ssl_parse_certificate_chain(ssl, chain);
8074 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008075 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 }
Jerry Yud9526692022-02-17 14:23:47 +08008077
8078#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008080 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 }
Jerry Yud9526692022-02-17 14:23:47 +08008082
8083crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008084 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008085 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008086 }
Jerry Yud9526692022-02-17 14:23:47 +08008087#endif
8088
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008089 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8090 ssl->handshake->ciphersuite_info,
8091 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008093 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 }
Jerry Yud9526692022-02-17 14:23:47 +08008095
8096#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8097 {
8098 unsigned char *crt_start, *pk_start;
8099 size_t crt_len, pk_len;
8100
8101 /* We parse the CRT chain without copying, so
8102 * these pointers point into the input buffer,
8103 * and are hence still valid after freeing the
8104 * CRT chain. */
8105
8106 crt_start = chain->raw.p;
8107 crt_len = chain->raw.len;
8108
8109 pk_start = chain->pk_raw.p;
8110 pk_len = chain->pk_raw.len;
8111
8112 /* Free the CRT structures before computing
8113 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 mbedtls_x509_crt_free(chain);
8115 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008116 chain = NULL;
8117
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8119 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 }
Jerry Yud9526692022-02-17 14:23:47 +08008122
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8124 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 }
Jerry Yud9526692022-02-17 14:23:47 +08008127 }
8128#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8129 /* Pass ownership to session structure. */
8130 ssl->session_negotiate->peer_cert = chain;
8131 chain = NULL;
8132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8133
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008135
8136exit:
8137
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008139 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 }
Jerry Yud9526692022-02-17 14:23:47 +08008141
8142#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008144 ssl->handshake->ecrs_peer_cert = chain;
8145 chain = NULL;
8146 }
8147#endif
8148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 if (chain != NULL) {
8150 mbedtls_x509_crt_free(chain);
8151 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008152 }
8153
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008155}
8156#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8157
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008158static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8159 unsigned char *padbuf, size_t hlen,
8160 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008161{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008162 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008163 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008165 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008166 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008167 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008168 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008169#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008171 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008172 mbedtls_md_context_t cloned_ctx;
8173 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008174#endif
8175
8176 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008178 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008179 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008182 ? "client finished"
8183 : "server finished";
8184
8185#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008186 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008187
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008188 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008189 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008190 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008191 }
8192
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008193 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008195 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008196 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008197 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008198#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008199 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008200
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008201 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008202 if (ret != 0) {
8203 goto exit;
8204 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008205 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008206 if (ret != 0) {
8207 goto exit;
8208 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008209
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008210 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008211 if (ret != 0) {
8212 goto exit;
8213 }
8214#endif /* MBEDTLS_USE_PSA_CRYPTO */
8215
8216 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8217
Jerry Yu615bd6f2022-02-17 14:25:15 +08008218 /*
8219 * TLSv1.2:
8220 * hash = PRF( master, finished_label,
8221 * Hash( handshake ) )[0.11]
8222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008224 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008225
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008227
Paul Elliottecb95be2023-08-11 16:41:04 +01008228 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008229
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008230 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008231
8232exit:
8233#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008234 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008235 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008236#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008237 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008238 return ret;
8239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008240}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008241
8242#if defined(MBEDTLS_MD_CAN_SHA256)
8243static int ssl_calc_finished_tls_sha256(
8244 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8245{
8246 unsigned char padbuf[32];
8247 return ssl_calc_finished_tls_generic(ssl,
8248#if defined(MBEDTLS_USE_PSA_CRYPTO)
8249 &ssl->handshake->fin_sha256_psa,
8250#else
8251 &ssl->handshake->fin_sha256,
8252#endif
8253 padbuf, sizeof(padbuf),
8254 buf, from);
8255}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008256#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008257
Jerry Yub7ba49e2022-02-17 14:25:53 +08008258
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008259#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008260static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008262{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008263 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008264 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008266 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008267#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008268 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008269#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008270 padbuf, sizeof(padbuf),
8271 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008272}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008273#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008274
Gilles Peskine449bd832023-01-11 14:50:10 +01008275void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008276{
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008278
8279 /*
8280 * Free our handshake params
8281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 mbedtls_ssl_handshake_free(ssl);
8283 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008284 ssl->handshake = NULL;
8285
8286 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008287 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 if (ssl->transform) {
8290 mbedtls_ssl_transform_free(ssl->transform);
8291 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008292 }
8293 ssl->transform = ssl->transform_negotiate;
8294 ssl->transform_negotiate = NULL;
8295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008297}
8298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008300{
8301 int resume = ssl->handshake->resume;
8302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008304
8305#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008307 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8308 ssl->renego_records_seen = 0;
8309 }
8310#endif
8311
8312 /*
8313 * Free the previous session and switch in the current one
8314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008316#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8317 /* RFC 7366 3.1: keep the EtM state */
8318 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008320#endif
8321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 mbedtls_ssl_session_free(ssl->session);
8323 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008324 }
8325 ssl->session = ssl->session_negotiate;
8326 ssl->session_negotiate = NULL;
8327
8328 /*
8329 * Add cache entry
8330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008332 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 resume == 0) {
8334 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8335 ssl->session->id,
8336 ssl->session->id_len,
8337 ssl->session) != 0) {
8338 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8339 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008340 }
8341
8342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8344 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008345 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008347
8348 /* Keep last flight around in case we need to resend it:
8349 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8351 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008352#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008354
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008355 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008356
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008358}
8359
Gilles Peskine449bd832023-01-11 14:50:10 +01008360int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008361{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008362 int ret;
8363 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008366
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008368
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008369 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8370 if (ret != 0) {
8371 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
David Horstmannb5ef7da2025-03-07 17:20:59 +00008372 return ret;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008373 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008374
8375 /*
8376 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8377 * may define some other value. Currently (early 2016), no defined
8378 * ciphersuite does this (and this is unlikely to change as activity has
8379 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8380 */
8381 hash_len = 12;
8382
8383#if defined(MBEDTLS_SSL_RENEGOTIATION)
8384 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008386#endif
8387
8388 ssl->out_msglen = 4 + hash_len;
8389 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8390 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8391
8392 /*
8393 * In case of session resuming, invert the client and server
8394 * ChangeCipherSpec messages order.
8395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008397#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008399 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008401#endif
8402#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008404 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008406#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008408 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008410
8411 /*
8412 * Switch to our negotiated transform and session parameters for outbound
8413 * data.
8414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008416
8417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008419 unsigned char i;
8420
8421 /* Remember current epoch settings for resending */
8422 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8424 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008425
8426 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008428
8429
8430 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 for (i = 2; i > 0; i--) {
8432 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008433 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 }
8435 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008436
8437 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 if (i == 0) {
8439 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8440 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008443#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008445
8446 ssl->transform_out = ssl->transform_negotiate;
8447 ssl->session_out = ssl->session_negotiate;
8448
8449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8451 mbedtls_ssl_send_flight_completed(ssl);
8452 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008453#endif
8454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8456 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8457 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008458 }
8459
8460#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8462 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8463 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8464 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008465 }
8466#endif
8467
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008469
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008471}
8472
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008473#define SSL_MAX_HASH_LEN 12
8474
Gilles Peskine449bd832023-01-11 14:50:10 +01008475int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008476{
8477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8478 unsigned int hash_len = 12;
8479 unsigned char buf[SSL_MAX_HASH_LEN];
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008482
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008483 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8484 if (ret != 0) {
8485 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
David Horstmannb5ef7da2025-03-07 17:20:59 +00008486 return ret;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008487 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008488
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008491 goto exit;
8492 }
8493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8495 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8496 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8497 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008498 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8499 goto exit;
8500 }
8501
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8503 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8504 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008505 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8506 goto exit;
8507 }
8508
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8511 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8512 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008513 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8514 goto exit;
8515 }
8516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8518 buf, hash_len) != 0) {
8519 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8520 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8521 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008522 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8523 goto exit;
8524 }
8525
8526#if defined(MBEDTLS_SSL_RENEGOTIATION)
8527 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008529#endif
8530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008532#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008534 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008536#endif
8537#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008539 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008541#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008542 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008543 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008545
8546#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8548 mbedtls_ssl_recv_flight_completed(ssl);
8549 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008550#endif
8551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008553
8554exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 mbedtls_platform_zeroize(buf, hash_len);
8556 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008557}
8558
Jerry Yu392112c2022-02-17 14:34:10 +08008559#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8560/*
8561 * Helper to get TLS 1.2 PRF from ciphersuite
8562 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008564static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008565{
Jerry Yu392112c2022-02-17 14:34:10 +08008566 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008568#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8570 return tls_prf_sha384;
8571 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008572#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008573#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008574 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8576 return tls_prf_sha256;
8577 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008578 }
8579#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008580#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8581 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008582 (void) ciphersuite_info;
8583#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008584
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008586}
8587#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008588
Gilles Peskine449bd832023-01-11 14:50:10 +01008589static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008590{
8591 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008592#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 if (tls_prf == tls_prf_sha384) {
8594 return MBEDTLS_SSL_TLS_PRF_SHA384;
8595 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008596#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008597#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 if (tls_prf == tls_prf_sha256) {
8599 return MBEDTLS_SSL_TLS_PRF_SHA256;
8600 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008601#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008602 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008603}
8604
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008605/*
8606 * Populate a transform structure with session keys and all the other
8607 * necessary information.
8608 *
8609 * Parameters:
8610 * - [in/out]: transform: structure to populate
8611 * [in] must be just initialised with mbedtls_ssl_transform_init()
8612 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8613 * - [in] ciphersuite
8614 * - [in] master
8615 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008616 * - [in] tls_prf: pointer to PRF to use for key derivation
8617 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008618 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008619 * - [in] endpoint: client or server
8620 * - [in] ssl: used for:
8621 * - ssl->conf->{f,p}_export_keys
8622 * [in] optionally used for:
8623 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8624 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008625MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008626static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8627 int ciphersuite,
8628 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008629#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008631#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 ssl_tls_prf_t tls_prf,
8633 const unsigned char randbytes[64],
8634 mbedtls_ssl_protocol_version tls_version,
8635 unsigned endpoint,
8636 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008637{
8638 int ret = 0;
8639 unsigned char keyblk[256];
8640 unsigned char *key1;
8641 unsigned char *key2;
8642 unsigned char *mac_enc;
8643 unsigned char *mac_dec;
8644 size_t mac_key_len = 0;
8645 size_t iv_copy_len;
8646 size_t keylen;
8647 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008648 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008649#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008650 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008651 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008652#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008653
8654#if defined(MBEDTLS_USE_PSA_CRYPTO)
8655 psa_key_type_t key_type;
8656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8657 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008658 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008659 size_t key_bits;
8660 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8661#endif
8662
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008663 /*
8664 * Some data just needs copying into the structure
8665 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008666#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008667 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008668#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008669 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008670
8671#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008673#endif
8674
8675#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008677 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8678 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008680 }
8681#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8682
8683 /*
8684 * Get various info structures
8685 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8687 if (ciphersuite_info == NULL) {
8688 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8689 ciphersuite));
8690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008691 }
8692
Neil Armstrongab555e02022-04-04 11:07:59 +02008693 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008694#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008696#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008700 transform->taglen =
8701 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008703
8704#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008705 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 transform->taglen,
8707 &alg,
8708 &key_type,
8709 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008710 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008712 goto end;
8713 }
8714#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008715 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 if (cipher_info == NULL) {
8717 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8718 ciphersuite_info->cipher));
8719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008720 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008721#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008722
Neil Armstronge4512952022-03-08 09:08:22 +01008723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008724 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008726 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 (unsigned) ciphersuite_info->mac));
8728 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008729 }
8730#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008731 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 if (md_info == NULL) {
8733 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8734 (unsigned) ciphersuite_info->mac));
8735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008736 }
Neil Armstronge4512952022-03-08 09:08:22 +01008737#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008738
8739#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8740 /* Copy own and peer's CID if the use of the CID
8741 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8743 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008744
8745 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8747 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8748 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008749
8750 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8752 ssl->handshake->peer_cid_len);
8753 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8754 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008755 }
8756#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8757
8758 /*
8759 * Compute key block using the PRF
8760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8762 if (ret != 0) {
8763 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8764 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008765 }
8766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8768 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8769 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8770 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8771 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008772
8773 /*
8774 * Determine the appropriate key, IV and MAC length.
8775 */
8776
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008777#if defined(MBEDTLS_USE_PSA_CRYPTO)
8778 keylen = PSA_BITS_TO_BYTES(key_bits);
8779#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008781#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008782
Valerio Settie5707042023-10-11 11:54:42 +02008783#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008785 size_t explicit_ivlen;
8786
8787 transform->maclen = 0;
8788 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008789
8790 /* All modes haves 96-bit IVs, but the length of the static parts vary
8791 * with mode and version:
8792 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8793 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8794 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8795 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8796 * sequence number).
8797 */
8798 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008799
8800 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008801#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008803#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8805 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008806#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008809 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008811 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008813
8814 /* Minimum length of encrypted record */
8815 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8816 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 } else
Valerio Settie5707042023-10-11 11:54:42 +02008818#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008819#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008821 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008823#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008825#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008826 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008827#endif /* MBEDTLS_USE_PSA_CRYPTO */
8828
8829#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008830 /* Get MAC length */
8831 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8832#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008833 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8835 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8836 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008837 goto end;
8838 }
8839
8840 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008842#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008843 transform->maclen = mac_key_len;
8844
8845 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008846#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008848#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008849 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008850#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008851
8852 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008854 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008856 /*
8857 * GenericBlockCipher:
8858 * 1. if EtM is in use: one block plus MAC
8859 * otherwise: * first multiple of blocklen greater than maclen
8860 * 2. IV
8861 */
8862#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008864 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 + block_size;
8866 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008867#endif
8868 {
8869 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 + block_size
8871 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008872 }
8873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008875 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 } else {
8877 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008878 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8879 goto end;
8880 }
8881 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008883#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8884 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8886 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008887 }
8888
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8890 (unsigned) keylen,
8891 (unsigned) transform->minlen,
8892 (unsigned) transform->ivlen,
8893 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008894
8895 /*
8896 * Finally setup the cipher contexts, IVs and MAC secrets.
8897 */
8898#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008900 key1 = keyblk + mac_key_len * 2;
8901 key2 = keyblk + mac_key_len * 2 + keylen;
8902
8903 mac_enc = keyblk;
8904 mac_dec = keyblk + mac_key_len;
8905
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 iv_copy_len = (transform->fixed_ivlen) ?
8907 transform->fixed_ivlen : transform->ivlen;
8908 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8909 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8910 iv_copy_len);
8911 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008912#endif /* MBEDTLS_SSL_CLI_C */
8913#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008915 key1 = keyblk + mac_key_len * 2 + keylen;
8916 key2 = keyblk + mac_key_len * 2;
8917
8918 mac_enc = keyblk + mac_key_len;
8919 mac_dec = keyblk;
8920
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 iv_copy_len = (transform->fixed_ivlen) ?
8922 transform->fixed_ivlen : transform->ivlen;
8923 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8924 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8925 iv_copy_len);
8926 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008927#endif /* MBEDTLS_SSL_SRV_C */
8928 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008930 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8931 goto end;
8932 }
8933
Paul Elliott4fb19552023-10-18 12:15:30 +01008934 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 ssl->f_export_keys(ssl->p_export_keys,
8936 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8937 master, 48,
8938 randbytes + 32,
8939 randbytes,
8940 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008941 }
8942
8943#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008944 transform->psa_alg = alg;
8945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8948 psa_set_key_algorithm(&attributes, alg);
8949 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 if ((status = psa_import_key(&attributes,
8952 key1,
8953 PSA_BITS_TO_BYTES(key_bits),
8954 &transform->psa_key_enc)) != PSA_SUCCESS) {
8955 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008956 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008958 goto end;
8959 }
8960
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 if ((status = psa_import_key(&attributes,
8964 key2,
8965 PSA_BITS_TO_BYTES(key_bits),
8966 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008967 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008969 goto end;
8970 }
8971 }
8972#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8974 cipher_info)) != 0) {
8975 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008976 goto end;
8977 }
8978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8980 cipher_info)) != 0) {
8981 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008982 goto end;
8983 }
8984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8986 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8987 MBEDTLS_ENCRYPT)) != 0) {
8988 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008989 goto end;
8990 }
8991
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8993 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8994 MBEDTLS_DECRYPT)) != 0) {
8995 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008996 goto end;
8997 }
8998
8999#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
9001 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
9002 MBEDTLS_PADDING_NONE)) != 0) {
9003 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009004 goto end;
9005 }
9006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
9008 MBEDTLS_PADDING_NONE)) != 0) {
9009 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009010 goto end;
9011 }
9012 }
9013#endif /* MBEDTLS_CIPHER_MODE_CBC */
9014#endif /* MBEDTLS_USE_PSA_CRYPTO */
9015
Neil Armstrong29c0c042022-03-17 17:47:28 +01009016#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
9017 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
9018 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009020#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
9024 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
9025 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 if ((status = psa_import_key(&attributes,
9028 mac_enc, mac_key_len,
9029 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009030 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009032 goto end;
9033 }
9034
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
9036 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04009037#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04009039#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009041 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
9043 PSA_KEY_USAGE_VERIFY_HASH);
9044 } else {
9045 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
9046 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009047
Gilles Peskine449bd832023-01-11 14:50:10 +01009048 if ((status = psa_import_key(&attributes,
9049 mac_dec, mac_key_len,
9050 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009051 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009053 goto end;
9054 }
9055#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9057 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009058 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 }
9060 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9061 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009062 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009064#endif /* MBEDTLS_USE_PSA_CRYPTO */
9065 }
9066#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9067
9068 ((void) mac_dec);
9069 ((void) mac_enc);
9070
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009071end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9073 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009074}
9075
Valerio Settia08b1a42022-11-17 15:10:02 +01009076#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9077 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009078int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 psa_pake_operation_t *pake_ctx,
9080 const unsigned char *buf,
9081 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009082{
9083 psa_status_t status;
9084 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009085 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009086 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9087 * At round two perform a single cycle
9088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009090
Gilles Peskine449bd832023-01-11 14:50:10 +01009091 for (; remaining_steps > 0; remaining_steps--) {
9092 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009093 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009095 /* Length is stored at the first byte */
9096 size_t length = buf[input_offset];
9097 input_offset += 1;
9098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009100 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9101 }
9102
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 status = psa_pake_input(pake_ctx, step,
9104 buf + input_offset, length);
9105 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009106 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009107 }
9108
9109 input_offset += length;
9110 }
9111 }
9112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009114 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009116
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009118}
9119
Valerio Setti6b3dab02022-11-17 17:14:54 +01009120int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 psa_pake_operation_t *pake_ctx,
9122 unsigned char *buf,
9123 size_t len, size_t *olen,
9124 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009125{
9126 psa_status_t status;
9127 size_t output_offset = 0;
9128 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009129 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009130 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9131 * At round two perform a single cycle
9132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009134
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 for (; remaining_steps > 0; remaining_steps--) {
9136 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9137 step <= PSA_PAKE_STEP_ZK_PROOF;
9138 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009139 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009140 * For each step, prepend 1 byte with the length of the data as
9141 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 status = psa_pake_output(pake_ctx, step,
9144 buf + output_offset + 1,
9145 len - output_offset - 1,
9146 &output_len);
9147 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009148 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009149 }
9150
Valerio Setti99d88c12022-11-22 16:03:43 +01009151 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009152
9153 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009154 }
9155 }
9156
9157 *olen = output_offset;
9158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009160}
Valerio Settia08b1a42022-11-17 15:10:02 +01009161#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9162
Jerry Yuee40f9d2022-02-17 14:55:16 +08009163#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009164int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9165 unsigned char *hash, size_t *hashlen,
9166 unsigned char *data, size_t data_len,
9167 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009168{
9169 psa_status_t status;
9170 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009171 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009172
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 if ((status = psa_hash_setup(&hash_operation,
9176 hash_alg)) != PSA_SUCCESS) {
9177 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009178 goto exit;
9179 }
9180
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9182 64)) != PSA_SUCCESS) {
9183 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009184 goto exit;
9185 }
9186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 if ((status = psa_hash_update(&hash_operation,
9188 data, data_len)) != PSA_SUCCESS) {
9189 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009190 goto exit;
9191 }
9192
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9194 hashlen)) != PSA_SUCCESS) {
9195 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9196 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009197 }
9198
9199exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009200 if (status != PSA_SUCCESS) {
9201 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9202 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9203 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009204 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009206 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9207 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009208 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009209 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009211 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009213 }
9214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009216}
9217
9218#else
9219
Gilles Peskine449bd832023-01-11 14:50:10 +01009220int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9221 unsigned char *hash, size_t *hashlen,
9222 unsigned char *data, size_t data_len,
9223 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009224{
9225 int ret = 0;
9226 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9228 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009229
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009231
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009233
9234 /*
9235 * digitally-signed struct {
9236 * opaque client_random[32];
9237 * opaque server_random[32];
9238 * ServerDHParams params;
9239 * };
9240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9242 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009243 goto exit;
9244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009245 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9246 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009247 goto exit;
9248 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9250 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009251 goto exit;
9252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9254 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009255 goto exit;
9256 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9258 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009259 goto exit;
9260 }
9261
9262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 if (ret != 0) {
9266 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9267 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9268 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009271}
9272#endif /* MBEDTLS_USE_PSA_CRYPTO */
9273
Jerry Yud9d91da2022-02-17 14:57:06 +08009274#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9275
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009276/* Find the preferred hash for a given signature algorithm. */
9277unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 mbedtls_ssl_context *ssl,
9279 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009280{
Gabor Mezei078e8032022-04-27 21:17:56 +02009281 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009282 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009283
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9285 return MBEDTLS_SSL_HASH_NONE;
9286 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009287
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009289 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9291 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009292 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9294 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009295
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009296 mbedtls_md_type_t md_alg =
9297 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9298 if (md_alg == MBEDTLS_MD_NONE) {
9299 continue;
9300 }
9301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009304 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009305 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009306 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009307
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9309 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9310 PSA_ALG_ECDSA(psa_hash_alg),
9311 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009312 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009314
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9316 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9317 PSA_ALG_RSA_PKCS1V15_SIGN(
9318 psa_hash_alg),
9319 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009320 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009322 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009323#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009324
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009326 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009327 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009328
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009330}
9331
9332#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9333
Jerry Yudc7bd172022-02-17 13:44:15 +08009334#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9335
XiaokangQian75d40ef2022-04-20 11:05:24 +00009336int mbedtls_ssl_validate_ciphersuite(
9337 const mbedtls_ssl_context *ssl,
9338 const mbedtls_ssl_ciphersuite_t *suite_info,
9339 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009341{
9342 (void) ssl;
9343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 if (suite_info == NULL) {
9345 return -1;
9346 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009347
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 if ((suite_info->min_tls_version > max_tls_version) ||
9349 (suite_info->max_tls_version < min_tls_version)) {
9350 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009351 }
9352
XiaokangQian060d8672022-04-21 09:24:56 +00009353#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009354#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009355#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009356 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9357 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009358#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9360 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009361#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009362 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009364 }
9365#endif
9366
9367 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9368#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9370 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9371 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009372 }
9373#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9374#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009377}
9378
Ronald Crone68ab4f2022-10-05 12:46:29 +02009379#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009380/*
9381 * Function for writing a signature algorithm extension.
9382 *
9383 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9384 * value (TLS 1.3 RFC8446):
9385 * enum {
9386 * ....
9387 * ecdsa_secp256r1_sha256( 0x0403 ),
9388 * ecdsa_secp384r1_sha384( 0x0503 ),
9389 * ecdsa_secp521r1_sha512( 0x0603 ),
9390 * ....
9391 * } SignatureScheme;
9392 *
9393 * struct {
9394 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9395 * } SignatureSchemeList;
9396 *
9397 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9398 * value (TLS 1.2 RFC5246):
9399 * enum {
9400 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9401 * sha512(6), (255)
9402 * } HashAlgorithm;
9403 *
9404 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9405 * SignatureAlgorithm;
9406 *
9407 * struct {
9408 * HashAlgorithm hash;
9409 * SignatureAlgorithm signature;
9410 * } SignatureAndHashAlgorithm;
9411 *
9412 * SignatureAndHashAlgorithm
9413 * supported_signature_algorithms<2..2^16-2>;
9414 *
9415 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9416 * generalization of the TLS 1.2 signature algorithm extension.
9417 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9418 * `SignatureScheme` field of TLS 1.3
9419 *
9420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009421int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9422 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009423{
9424 unsigned char *p = buf;
9425 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9426 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9427
9428 *out_len = 0;
9429
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009431
9432 /* Check if we have space for header and length field:
9433 * - extension_type (2 bytes)
9434 * - extension_data_length (2 bytes)
9435 * - supported_signature_algorithms_length (2 bytes)
9436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009437 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009438 p += 6;
9439
9440 /*
9441 * Write supported_signature_algorithms
9442 */
9443 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009444 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9445 if (sig_alg == NULL) {
9446 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9447 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9450 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9451 *sig_alg,
9452 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9453 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009454 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 }
9456 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9457 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009458 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9460 *sig_alg,
9461 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009462 }
9463
9464 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009465 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 if (supported_sig_alg_len == 0) {
9467 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9468 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009469 }
9470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9472 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9473 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009474
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009475 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009476
9477#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009479#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009480
Gilles Peskine449bd832023-01-11 14:50:10 +01009481 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009482}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009483#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009484
XiaokangQian40a35232022-05-07 09:02:40 +00009485#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009486/*
9487 * mbedtls_ssl_parse_server_name_ext
9488 *
9489 * Structure of server_name extension:
9490 *
9491 * enum {
9492 * host_name(0), (255)
9493 * } NameType;
9494 * opaque HostName<1..2^16-1>;
9495 *
9496 * struct {
9497 * NameType name_type;
9498 * select (name_type) {
9499 * case host_name: HostName;
9500 * } name;
9501 * } ServerName;
9502 * struct {
9503 * ServerName server_name_list<1..2^16-1>
9504 * } ServerNameList;
9505 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009506MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009507int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9508 const unsigned char *buf,
9509 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009510{
9511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9512 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009513 size_t server_name_list_len, hostname_len;
9514 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009515
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009517
Gilles Peskine449bd832023-01-11 14:50:10 +01009518 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9519 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009520 p += 2;
9521
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009523 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 while (p < server_name_list_end) {
9525 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9526 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9527 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9528 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009529
Gilles Peskine449bd832023-01-11 14:50:10 +01009530 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009531 /* sni_name is intended to be used only during the parsing of the
9532 * ClientHello message (it is reset to NULL before the end of
9533 * the message parsing). Thus it is ok to just point to the
9534 * reception buffer and not make a copy of it.
9535 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009536 ssl->handshake->sni_name = p + 3;
9537 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009538 if (ssl->conf->f_sni == NULL) {
9539 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009540 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9542 ssl, p + 3, hostname_len);
9543 if (ret != 0) {
9544 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9545 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9546 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9547 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9548 }
9549 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009550 }
9551
9552 p += hostname_len + 3;
9553 }
9554
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009556}
9557#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9558
XiaokangQianacb39922022-06-17 10:18:48 +00009559#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009560MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009561int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9562 const unsigned char *buf,
9563 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009564{
9565 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009566 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009567 const unsigned char *protocol_name_list;
9568 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009569 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009570
9571 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 if (ssl->conf->alpn_list == NULL) {
9573 return 0;
9574 }
XiaokangQianacb39922022-06-17 10:18:48 +00009575
9576 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009577 * RFC7301, section 3.1
9578 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009579 *
XiaokangQianc7403452022-06-23 03:24:12 +00009580 * struct {
9581 * ProtocolName protocol_name_list<2..2^16-1>
9582 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009583 */
9584
XiaokangQianc7403452022-06-23 03:24:12 +00009585 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009586 * protocol_name_list_len 2 bytes
9587 * protocol_name_len 1 bytes
9588 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009589 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009593 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009594 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009595 protocol_name_list = p;
9596 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009597
9598 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009600 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009601 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9602 protocol_name_len);
9603 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009604 MBEDTLS_SSL_PEND_FATAL_ALERT(
9605 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9607 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009608 }
9609
9610 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009611 }
9612
9613 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9615 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009616 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009618 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 if (protocol_name_len == alpn_len &&
9620 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009621 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009623 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009624
9625 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009626 }
9627 }
9628
XiaokangQian95d5f542022-06-24 02:29:26 +00009629 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009630 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9632 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9633 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009634}
9635
Gilles Peskine449bd832023-01-11 14:50:10 +01009636int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9637 unsigned char *buf,
9638 unsigned char *end,
9639 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009640{
9641 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009642 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009643 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009644
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 if (ssl->alpn_chosen == NULL) {
9646 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009647 }
9648
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 protocol_name_len = strlen(ssl->alpn_chosen);
9650 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009651
Gilles Peskine449bd832023-01-11 14:50:10 +01009652 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009653 /*
9654 * 0 . 1 ext identifier
9655 * 2 . 3 ext length
9656 * 4 . 5 protocol list length
9657 * 6 . 6 protocol name length
9658 * 7 . 7+n protocol name
9659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009661
XiaokangQian95d5f542022-06-24 02:29:26 +00009662 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009663
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9665 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009666 /* Note: the length of the chosen protocol has been checked to be less
9667 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9668 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009669 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009670
Gilles Peskine449bd832023-01-11 14:50:10 +01009671 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009672
9673#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009674 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009675#endif
9676
Gilles Peskine449bd832023-01-11 14:50:10 +01009677 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009678}
9679#endif /* MBEDTLS_SSL_ALPN */
9680
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009681#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009682 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009683 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009684 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009685int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9686 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009687{
9688 /* Initialize to suppress unnecessary compiler warning */
9689 size_t hostname_len = 0;
9690
9691 /* Check if new hostname is valid before
9692 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009693 if (hostname != NULL) {
9694 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009695
Gilles Peskine449bd832023-01-11 14:50:10 +01009696 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9697 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9698 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009699 }
9700
9701 /* Now it's clear that we will overwrite the old hostname,
9702 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009703 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009704 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009705 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009706 }
9707
9708 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009709 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009710 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009711 } else {
9712 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9713 if (session->hostname == NULL) {
9714 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9715 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009716
Gilles Peskine449bd832023-01-11 14:50:10 +01009717 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009718 }
9719
Gilles Peskine449bd832023-01-11 14:50:10 +01009720 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009721}
Xiaokang Qian03409292022-10-12 02:49:52 +00009722#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9723 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009724 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009725 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009726
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009727#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9728 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009729int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9730 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009731{
9732 size_t alpn_len = 0;
9733
9734 if (alpn != NULL) {
9735 alpn_len = strlen(alpn);
9736
9737 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9739 }
9740 }
9741
9742 if (session->ticket_alpn != NULL) {
9743 mbedtls_zeroize_and_free(session->ticket_alpn,
9744 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009745 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009746 }
9747
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009748 if (alpn != NULL) {
9749 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009750 if (session->ticket_alpn == NULL) {
9751 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9752 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009753 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009754 }
9755
9756 return 0;
9757}
9758#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009759
9760/*
9761 * The following functions are used by 1.2 and 1.3, client and server.
9762 */
9763#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9764int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9765 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9766 int recv_endpoint,
9767 mbedtls_ssl_protocol_version tls_version,
9768 uint32_t *flags)
9769{
9770 int ret = 0;
9771 unsigned int usage = 0;
9772 const char *ext_oid;
9773 size_t ext_len;
9774
9775 /*
9776 * keyUsage
9777 */
9778
9779 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9780 * to check what a compliant client will think while choosing which cert
9781 * to send to the client. */
9782#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9783 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9784 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9785 /* TLS 1.2 server part of the key exchange */
9786 switch (ciphersuite->key_exchange) {
9787 case MBEDTLS_KEY_EXCHANGE_RSA:
9788 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9789 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9790 break;
9791
9792 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9793 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9794 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9795 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9796 break;
9797
9798 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9799 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9800 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9801 break;
9802
9803 /* Don't use default: we want warnings when adding new values */
9804 case MBEDTLS_KEY_EXCHANGE_NONE:
9805 case MBEDTLS_KEY_EXCHANGE_PSK:
9806 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9807 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9808 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9809 usage = 0;
9810 }
9811 } else
9812#endif
9813 {
9814 /* This is either TLS 1.3 authentication, which always uses signatures,
9815 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9816 * options we implement, both using signatures. */
9817 (void) tls_version;
9818 (void) ciphersuite;
9819 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9820 }
9821
9822 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9823 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9824 ret = -1;
9825 }
9826
9827 /*
9828 * extKeyUsage
9829 */
9830
9831 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9832 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9833 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9834 } else {
9835 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9836 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9837 }
9838
9839 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9840 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9841 ret = -1;
9842 }
9843
9844 return ret;
9845}
9846
Gilles Peskinee61852e2025-02-12 23:28:48 +01009847static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
9848 const char **hostname)
9849{
9850 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
9851 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
Gilles Peskine2c33c752025-02-13 14:39:02 +01009852#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
9853 if (mbedtls_ssl_conf_get_endpoint(ssl->conf) == MBEDTLS_SSL_IS_CLIENT &&
9854 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9855 return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
9856 }
9857#endif
Gilles Peskinee61852e2025-02-12 23:28:48 +01009858 }
9859
9860 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
9861 if (*hostname == NULL) {
9862 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
9863 }
9864
9865 return 0;
9866}
9867
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009868int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9869 int authmode,
9870 mbedtls_x509_crt *chain,
9871 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9872 void *rs_ctx)
9873{
9874 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9875 return 0;
9876 }
9877
9878 /*
9879 * Primary check: use the appropriate X.509 verification function
9880 */
9881 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9882 void *p_vrfy;
9883 if (ssl->f_vrfy != NULL) {
9884 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9885 f_vrfy = ssl->f_vrfy;
9886 p_vrfy = ssl->p_vrfy;
9887 } else {
9888 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9889 f_vrfy = ssl->conf->f_vrfy;
9890 p_vrfy = ssl->conf->p_vrfy;
9891 }
9892
Gilles Peskinee61852e2025-02-12 23:28:48 +01009893 const char *hostname = "";
9894 int ret = get_hostname_for_verification(ssl, &hostname);
9895 if (ret != 0) {
9896 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
9897 return ret;
9898 }
9899
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009900 int have_ca_chain_or_callback = 0;
9901#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9902 if (ssl->conf->f_ca_cb != NULL) {
9903 ((void) rs_ctx);
9904 have_ca_chain_or_callback = 1;
9905
9906 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9907 ret = mbedtls_x509_crt_verify_with_ca_cb(
9908 chain,
9909 ssl->conf->f_ca_cb,
9910 ssl->conf->p_ca_cb,
9911 ssl->conf->cert_profile,
Gilles Peskinee61852e2025-02-12 23:28:48 +01009912 hostname,
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009913 &ssl->session_negotiate->verify_result,
9914 f_vrfy, p_vrfy);
9915 } else
9916#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9917 {
9918 mbedtls_x509_crt *ca_chain;
9919 mbedtls_x509_crl *ca_crl;
9920#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9921 if (ssl->handshake->sni_ca_chain != NULL) {
9922 ca_chain = ssl->handshake->sni_ca_chain;
9923 ca_crl = ssl->handshake->sni_ca_crl;
9924 } else
9925#endif
9926 {
9927 ca_chain = ssl->conf->ca_chain;
9928 ca_crl = ssl->conf->ca_crl;
9929 }
9930
9931 if (ca_chain != NULL) {
9932 have_ca_chain_or_callback = 1;
9933 }
9934
9935 ret = mbedtls_x509_crt_verify_restartable(
9936 chain,
9937 ca_chain, ca_crl,
9938 ssl->conf->cert_profile,
Gilles Peskinee61852e2025-02-12 23:28:48 +01009939 hostname,
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009940 &ssl->session_negotiate->verify_result,
9941 f_vrfy, p_vrfy, rs_ctx);
9942 }
9943
9944 if (ret != 0) {
9945 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9946 }
9947
9948#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9949 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9950 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9951 }
9952#endif
9953
9954 /*
9955 * Secondary checks: always done, but change 'ret' only if it was 0
9956 */
9957
9958 /* With TLS 1.2 and ECC certs, check that the curve used by the
9959 * certificate is on our list of acceptable curves.
9960 *
9961 * With TLS 1.3 this is not needed because the curve is part of the
9962 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9963 * we validate the signature made with the key associated to this cert.
9964 */
9965#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9966 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9967 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9968 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9969 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9970 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9971 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9972 if (ret == 0) {
9973 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9974 }
9975 }
9976 }
9977#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9978
9979 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9980 if (mbedtls_ssl_check_cert_usage(chain,
9981 ciphersuite_info,
9982 ssl->conf->endpoint,
9983 ssl->tls_version,
9984 &ssl->session_negotiate->verify_result) != 0) {
9985 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9986 if (ret == 0) {
9987 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9988 }
9989 }
9990
9991 /* With authmode optional, we want to keep going if the certificate was
9992 * unacceptable, but still fail on other errors (out of memory etc),
9993 * including fatal errors from the f_vrfy callback.
9994 *
9995 * The only acceptable errors are:
9996 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9997 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9998 * Anything else is a fatal error. */
9999 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
10000 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
10001 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
10002 ret = 0;
10003 }
10004
10005 /* Return a specific error as this is a user error: inconsistent
10006 * configuration - can't verify without trust anchors. */
10007 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
10008 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
10009 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
10010 }
10011
10012 if (ret != 0) {
10013 uint8_t alert;
10014
10015 /* The certificate may have been rejected for several reasons.
10016 Pick one and send the corresponding alert. Which alert to send
10017 may be a subject of debate in some cases. */
10018 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
10019 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
10020 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
10021 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
10022 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
10023 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10024 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
10025 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10026 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
10027 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10028 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
10029 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
10030 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
10031 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
10032 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
10033 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
10034 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
10035 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
10036 } else {
10037 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
10038 }
10039 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10040 alert);
10041 }
10042
10043#if defined(MBEDTLS_DEBUG_C)
10044 if (ssl->session_negotiate->verify_result != 0) {
10045 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
10046 (unsigned int) ssl->session_negotiate->verify_result));
10047 } else {
10048 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
10049 }
10050#endif /* MBEDTLS_DEBUG_C */
10051
10052 return ret;
10053}
10054#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#endif /* MBEDTLS_SSL_TLS_C */