blob: 4bb170b15e80771fae565e8ffb9a290317ccc29f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * http://www.ietf.org/rfc/rfc2246.txt
9 * http://www.ietf.org/rfc/rfc4346.txt
10 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000015
SimonBd5800b72016-04-26 07:43:27 +010016#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010019#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010020#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000021#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040022
Valerio Settib4f50762024-01-17 10:24:52 +010023#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000024#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010026#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020027#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050031#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010032#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020033#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050035#include "psa/crypto.h"
36#endif
37
Janos Follath23bdca02016-10-07 14:47:14 +010038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020040#endif
41
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040043/* Define local translating functions to save code size by not using too many
44 * arguments in each translating place. */
45static int local_err_translation(psa_status_t status)
46{
47 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040048 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040049 psa_generic_status_to_mbedtls);
50}
51#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050052#endif
53
Ronald Cronad8c17b2022-06-10 17:18:09 +020054#if defined(MBEDTLS_TEST_HOOKS)
55static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
56
57void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010058 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020059{
60 chk_buf_ptr_fail_args.cur = cur;
61 chk_buf_ptr_fail_args.end = end;
62 chk_buf_ptr_fail_args.need = need;
63}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020066{
Gilles Peskine449bd832023-01-11 14:50:10 +010067 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020068}
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020071{
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return (chk_buf_ptr_fail_args.cur != args->cur) ||
73 (chk_buf_ptr_fail_args.end != args->end) ||
74 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020075}
76#endif /* MBEDTLS_TEST_HOOKS */
77
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010079
Hanno Beckera0e20d02019-05-15 14:03:01 +010080#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010081/* Top-level Connection ID API */
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
84 size_t len,
85 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
88 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
89 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
93 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010094 }
95
96 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010097 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
102 int enable,
103 unsigned char const *own_cid,
104 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
108 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (enable == MBEDTLS_SSL_CID_DISABLED) {
112 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
113 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
116 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (own_cid_len != ssl->conf->cid_len) {
119 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
120 (unsigned) own_cid_len,
121 (unsigned) ssl->conf->cid_len));
122 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100126 /* Truncation is not an issue here because
127 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
128 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100131}
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
134 int *enabled,
Sam Berry9722fd12024-06-11 14:34:17 +0100135 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000137{
138 *enabled = MBEDTLS_SSL_CID_DISABLED;
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
142 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000143
144 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
145 * zero as this is indistinguishable from not requesting to use
146 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
148 return 0;
149 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000152 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (own_cid != NULL) {
154 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
155 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
166 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
171 mbedtls_ssl_is_handshake_over(ssl) == 0) {
172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100173 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100174
Hanno Beckerc5f24222019-05-03 12:54:52 +0100175 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
176 * were used, but client and server requested the empty CID.
177 * This is indistinguishable from not using the CID extension
178 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (ssl->transform_in->in_cid_len == 0 &&
180 ssl->transform_in->out_cid_len == 0) {
181 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100185 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (peer_cid != NULL) {
187 memcpy(peer_cid, ssl->transform_in->out_cid,
188 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100189 }
190 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100191
192 *enabled = MBEDTLS_SSL_CID_ENABLED;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100195}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200201/*
202 * Convert max_fragment_length codes to length.
203 * RFC 6066 says:
204 * enum{
205 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
206 * } MaxFragmentLength;
207 * and we add 0 -> extension unused
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200210{
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 switch (mfl) {
212 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
213 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
214 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
215 return 512;
216 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
217 return 1024;
218 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
219 return 2048;
220 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
221 return 4096;
222 default:
223 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000224 }
225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
229 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_ssl_session_free(dst);
232 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800233#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
234 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000235#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
236 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000237 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800238#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000239#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800240
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000241#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
242 defined(MBEDTLS_SSL_EARLY_DATA)
243 dst->ticket_alpn = NULL;
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000247
248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
253 if (dst->peer_cert == NULL) {
254 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
255 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
260 src->peer_cert->raw.len)) != 0) {
261 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200262 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264 }
265 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000266#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000268 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 mbedtls_calloc(1, src->peer_cert_digest_len);
270 if (dst->peer_cert_digest == NULL) {
271 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
272 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
275 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000276 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000277 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000278 }
279#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200282
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000283#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
284 defined(MBEDTLS_SSL_EARLY_DATA)
285 {
286 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
287 if (ret != 0) {
288 return ret;
289 }
290 }
291#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
292
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200293#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (src->ticket != NULL) {
295 dst->ticket = mbedtls_calloc(1, src->ticket_len);
296 if (dst->ticket == NULL) {
297 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
298 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000302
303#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
304 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
308 if (ret != 0) {
309 return ret;
310 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000311 }
312#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
313 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200314#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200317}
318
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200320MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
324 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800325 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500327
328 /* We want to copy len_new bytes when downsizing the buffer, and
329 * len_old bytes when upsizing, so we choose the smaller of two sizes,
330 * to fit one buffer into another. Size checks, ensuring that no data is
331 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 memcpy(resized_buffer, *buffer,
333 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100334 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500335
336 *buffer = resized_buffer;
337 *len_old = len_new;
338
339 return 0;
340}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
343 size_t in_buf_new_len,
344 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345{
346 int modified = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000347 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000349 size_t hshdr_in = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200351 written_in = ssl->in_msg - ssl->in_buf;
352 iv_offset_in = ssl->in_iv - ssl->in_buf;
353 len_offset_in = ssl->in_len - ssl->in_buf;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000354 hdr_in = ssl->in_hdr - ssl->in_buf;
355 if (ssl->in_hshdr != NULL) {
356 hshdr_in = ssl->in_hshdr - ssl->in_buf;
357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200359 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 ssl->in_buf_len < in_buf_new_len) {
361 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
362 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
363 } else {
364 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
365 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 modified = 1;
367 }
368 }
369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 written_out = ssl->out_msg - ssl->out_buf;
373 iv_offset_out = ssl->out_iv - ssl->out_buf;
374 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200376 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ssl->out_buf_len < out_buf_new_len) {
378 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
379 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
380 } else {
381 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
382 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 modified = 1;
384 }
385 }
386 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200388 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000389 ssl->in_hdr = ssl->in_buf + hdr_in;
390 mbedtls_ssl_update_in_pointers(ssl);
391 mbedtls_ssl_reset_out_pointers(ssl);
392
Andrzej Kurek4a063792020-10-21 15:08:44 +0200393 /* Fields below might not be properly updated with record
394 * splitting or with CID, so they are manually updated here. */
395 ssl->out_msg = ssl->out_buf + written_out;
396 ssl->out_len = ssl->out_buf + len_offset_out;
397 ssl->out_iv = ssl->out_buf + iv_offset_out;
398
399 ssl->in_msg = ssl->in_buf + written_in;
400 ssl->in_len = ssl->in_buf + len_offset_in;
401 ssl->in_iv = ssl->in_buf + iv_offset_in;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +0000402 if (ssl->in_hshdr != NULL) {
403 ssl->in_hshdr = ssl->in_buf + hshdr_in;
404 }
Andrzej Kurek4a063792020-10-21 15:08:44 +0200405 }
406}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500407#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
408
Jerry Yudb8c48a2022-01-27 14:54:54 +0800409#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800410
411#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
413 const char *label,
414 const unsigned char *random, size_t rlen,
415 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800418
419#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
420
421/* Type for the TLS PRF */
422typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
423 const unsigned char *, size_t,
424 unsigned char *, size_t);
425
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200426MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100427static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
428 int ciphersuite,
429 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200430#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200432#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 ssl_tls_prf_t tls_prf,
434 const unsigned char randbytes[64],
435 mbedtls_ssl_protocol_version tls_version,
436 unsigned endpoint,
437 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800438
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100439#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200440MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300442 const char *label,
443 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100445static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
446static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100447
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100448#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100449
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100450#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100451MBEDTLS_CHECK_RETURN_CRITICAL
452static int tls_prf_sha384(const unsigned char *secret, size_t slen,
453 const char *label,
454 const unsigned char *random, size_t rlen,
455 unsigned char *dstbuf, size_t dlen);
456
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100457static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
458static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100459#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461MBEDTLS_CHECK_RETURN_CRITICAL
462static int ssl_tls12_session_load(mbedtls_ssl_session *session,
463 const unsigned char *buf,
464 size_t len);
465#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
466
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100467static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100468
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100469#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100470static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100471#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100472
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100473#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100474static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100475#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100476
477int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
478 const unsigned char *secret, size_t slen,
479 const char *label,
480 const unsigned char *random, size_t rlen,
481 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482{
483 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100487#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 case MBEDTLS_SSL_TLS_PRF_SHA384:
489 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100491#endif /* MBEDTLS_MD_CAN_SHA384*/
492#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 case MBEDTLS_SSL_TLS_PRF_SHA256:
494 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100496#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 default:
499 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300503}
504
Jerry Yuc73c6182022-02-08 20:29:25 +0800505#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100506static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800507{
508#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (session->peer_cert != NULL) {
510 mbedtls_x509_crt_free(session->peer_cert);
511 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800512 session->peer_cert = NULL;
513 }
514#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800516 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800518 session->peer_cert_digest = NULL;
519 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
520 session->peer_cert_digest_len = 0;
521 }
522#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
523}
524#endif /* MBEDTLS_X509_CRT_PARSE_C */
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800527{
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800529 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800531
532 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800534
535 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800537
538 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800540
541 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800543
544 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800546
547 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800549
550 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800552
553 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800555
556 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800558
559 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800561
562 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800564
565 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800567
568 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800570
571 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800573
574 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800576
577 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800579
580 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800582
583 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800585
586 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800588
589 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800591
592 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800594
595 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800597
598 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800600
601 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800603
604 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800606
Jan Bruckner151f6422023-02-10 12:45:19 +0100607 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
608 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
609
Jerry Yu7a485c12022-10-31 13:08:18 +0800610 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800612
613 }
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800616}
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800619{
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800621}
622
Jerry Yud25cab02022-10-31 12:48:30 +0800623#if defined(MBEDTLS_DEBUG_C)
624static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800625 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800626 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
627 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
628 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
629 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
630 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
631 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
632 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
633 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
634 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
635 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
636 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
637 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
638 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
639 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
640 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
641 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
642 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
643 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
644 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
645 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
646 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
647 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
648 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
649 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
650 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
651 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100652 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
653 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800654};
655
Chien Wong4e9683e2023-12-28 17:07:43 +0800656static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800657 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
658 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
659 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
660 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
661 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
662 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
663 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
664 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
665 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
666 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
667 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
668 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
669 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
670 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
671 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
672 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
673 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
674 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
675 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
676 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
677 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
678 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
679 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
680 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
681 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
682 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
683 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100684 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
685 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800686};
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800689{
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return extension_name_table[
691 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800692}
693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800695{
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800701 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800703 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800705 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800707 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800709 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800713}
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
716 int level, const char *file, int line,
717 int hs_msg_type, unsigned int extension_type,
718 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800719{
720 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800722 mbedtls_debug_print_msg(
723 ssl, level, file, line,
724 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 ssl_tls13_get_hs_msg_name(hs_msg_type),
726 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800727 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800729 return;
730 }
731
732 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800734 mbedtls_debug_print_msg(
735 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
737 mbedtls_ssl_get_extension_name(extension_type), extension_type,
738 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800739 return;
740 }
741
742 mbedtls_debug_print_msg(
743 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
745 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800746}
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 int hs_msg_type, uint32_t extensions_mask,
751 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800752{
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 for (unsigned i = 0;
755 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
756 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800757 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800758 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800760 }
761}
762
Pengyu Lvee455c02023-01-12 14:37:24 +0800763#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800764static const char *ticket_flag_name_table[] =
765{
766 [0] = "ALLOW_PSK_RESUMPTION",
767 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
768 [3] = "ALLOW_EARLY_DATA",
769};
770
Pengyu Lv4938a562023-01-16 11:28:49 +0800771void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
772 int level, const char *file, int line,
773 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800774{
775 size_t i;
776
777 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800778 "print ticket_flags (0x%02x)", flags);
779
780 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800781
782 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800783 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800784 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
785 ticket_flag_name_table[i]);
786 }
787 }
788}
789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
790
Jerry Yud25cab02022-10-31 12:48:30 +0800791#endif /* MBEDTLS_DEBUG_C */
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
794 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800795{
796 ((void) ciphersuite_info);
797
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100798#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800800 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800802#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100803#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800805 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800807#endif
808 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800810 return;
811 }
812}
813
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 unsigned hs_type,
816 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817{
818 unsigned char hs_hdr[4];
819
820 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
822 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
823 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
824 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100825
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100826 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100827}
828
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100829int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100830 unsigned hs_type,
831 unsigned char const *msg,
832 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100833{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100834 int ret;
835 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100836 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100837 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100838 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100839 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100840}
841
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100842int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800843{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100844#if defined(MBEDTLS_MD_CAN_SHA256) || \
845 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100846#if defined(MBEDTLS_USE_PSA_CRYPTO)
847 psa_status_t status;
848#else
849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100851#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800852 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100853#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100854#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
857 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200858 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 }
860 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
861 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200862 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100863 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800864#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100865 mbedtls_md_free(&ssl->handshake->fin_sha256);
866 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100867 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
868 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
869 0);
870 if (ret != 0) {
871 return ret;
872 }
873 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 if (ret != 0) {
875 return ret;
876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#endif
878#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100879#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100881 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
882 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200883 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100884 }
885 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
886 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200887 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100890 mbedtls_md_free(&ssl->handshake->fin_sha384);
891 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100892 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
893 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
894 if (ret != 0) {
895 return ret;
896 }
897 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100898 if (ret != 0) {
899 return ret;
900 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800901#endif
902#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100903 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800904}
905
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100906static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100907 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800908{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100909#if defined(MBEDTLS_MD_CAN_SHA256) || \
910 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911#if defined(MBEDTLS_USE_PSA_CRYPTO)
912 psa_status_t status;
913#else
914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
915#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100916#else /* SHA-256 or SHA-384 */
917 ((void) ssl);
918 (void) buf;
919 (void) len;
920#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100921#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100923 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
924 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200925 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100926 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100928 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 if (ret != 0) {
930 return ret;
931 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#endif
933#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100934#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800935#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100936 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
937 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200938 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100939 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800940#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100941 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100942 if (ret != 0) {
943 return ret;
944 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100947 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800948}
949
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100950#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100951static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100952 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800953{
954#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200955 return mbedtls_md_error_from_psa(psa_hash_update(
956 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800957#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100958 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800959#endif
960}
961#endif
962
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100963#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100964static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100965 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800966{
967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200968 return mbedtls_md_error_from_psa(psa_hash_update(
969 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800970#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100971 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800972#endif
973}
974#endif
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100980#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#if defined(MBEDTLS_USE_PSA_CRYPTO)
982 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500983#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100984 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500986#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100987#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500988#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500989 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500990#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100991 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500993#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200994
995 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200999#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02001000#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02001001 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001003#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001004#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006 handshake->psa_pake_ctx = psa_pake_operation_init();
1007 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1008#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001010#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001011#if defined(MBEDTLS_SSL_CLI_C)
1012 handshake->ecjpake_cache = NULL;
1013 handshake->ecjpake_cache_len = 0;
1014#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001015#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001016
Gilles Peskineeccd8882020-03-10 12:19:08 +01001017#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001019#endif
1020
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001021#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1022 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1023#endif
Hanno Becker75173122019-02-06 16:18:31 +00001024
1025#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1026 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001028#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001029}
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001032{
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001034
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001035#if defined(MBEDTLS_USE_PSA_CRYPTO)
1036 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1037 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001038#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1040 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001041#endif
1042
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001043#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001044#if defined(MBEDTLS_USE_PSA_CRYPTO)
1045 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1046 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001047#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_md_init(&transform->md_ctx_enc);
1049 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001050#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001051#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001052}
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001055{
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001057}
1058
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001059MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001060static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001061{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1063
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001064 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (ssl->transform_negotiate) {
1067 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1068 }
Jerry Yu2e199812022-12-01 18:57:19 +08001069#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (ssl->session_negotiate) {
1071 mbedtls_ssl_session_free(ssl->session_negotiate);
1072 }
1073 if (ssl->handshake) {
1074 mbedtls_ssl_handshake_free(ssl);
1075 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001076
Jerry Yu2e199812022-12-01 18:57:19 +08001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001078 /*
1079 * Either the pointers are now NULL or cleared properly and can be freed.
1080 * Now allocate missing structures.
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (ssl->transform_negotiate == NULL) {
1083 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001084 }
Jerry Yu2e199812022-12-01 18:57:19 +08001085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl->session_negotiate == NULL) {
1088 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001089 }
Paul Bakker48916f92012-09-16 19:57:18 +00001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ssl->handshake == NULL) {
1092 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001093 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001094#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1095 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1098 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001099#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001100
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001101 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001104 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001105#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 ssl->session_negotiate == NULL) {
1107 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001110 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001111
1112#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001114 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001115#endif
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001118 ssl->session_negotiate = NULL;
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001121 }
1122
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001123#if defined(MBEDTLS_SSL_EARLY_DATA)
1124#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001125 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001126#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001127#if defined(MBEDTLS_SSL_SRV_C)
1128 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1129#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001130 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001131#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001132
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001133 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_ssl_session_init(ssl->session_negotiate);
1135 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001136
Jerry Yu2e199812022-12-01 18:57:19 +08001137#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001139#endif
1140
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001141 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001142 ret = mbedtls_ssl_reset_checksum(ssl);
1143 if (ret != 0) {
1144 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1145 return ret;
1146 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001147
Jerry Yud0766ec2022-09-22 10:46:57 +08001148#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1149 defined(MBEDTLS_SSL_SRV_C) && \
1150 defined(MBEDTLS_SSL_SESSION_TICKETS)
1151 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001153#endif
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001157 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001160 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001162 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001166 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001167#endif
1168
Brett Warrene0edc842021-08-17 09:53:13 +01001169/*
1170 * curve_list is translated to IANA TLS group identifiers here because
1171 * mbedtls_ssl_conf_curves returns void and so can't return
1172 * any error codes.
1173 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001174#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001175#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1176 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001178 size_t length;
1179 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1180
Thomas Daubney850a0792023-05-22 12:05:03 +01001181 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 }
Brett Warrene0edc842021-08-17 09:53:13 +01001183
1184 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1186 if (group_list == NULL) {
1187 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1188 }
Brett Warrene0edc842021-08-17 09:53:13 +01001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001191 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 curve_list[i]);
1193 if (tls_id == 0) {
1194 mbedtls_free(group_list);
1195 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001196 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001197 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001198 }
1199
1200 group_list[length] = 0;
1201
1202 ssl->handshake->group_list = group_list;
1203 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001205 ssl->handshake->group_list = ssl->conf->group_list;
1206 ssl->handshake->group_list_heap_allocated = 0;
1207 }
1208#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001209#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001210
Ronald Crone68ab4f2022-10-05 12:46:29 +02001211#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001212#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001214 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1215 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1217 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001218 const int *md;
1219 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001220 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 uint16_t *p;
1222
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001223 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1224 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1225 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1228 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001229 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001231#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001233#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001234
Jerry Yub476a442022-01-21 18:14:45 +08001235#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001237#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1239 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1240 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001241 }
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1244 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1245 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1248 sizeof(uint16_t));
1249 if (ssl->handshake->sig_algs == NULL) {
1250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1251 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 p = (uint16_t *) ssl->handshake->sig_algs;
1254 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1255 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1256 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001257 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001259#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001261 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001262#endif
1263#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001265 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001266#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001267 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001268 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001269 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001272 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001273 ssl->handshake->sig_algs_heap_allocated = 0;
1274 }
Jerry Yucc539102022-06-27 16:27:35 +08001275#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001276#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001278}
1279
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001280#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001281/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001283static int ssl_cookie_write_dummy(void *ctx,
1284 unsigned char **p, unsigned char *end,
1285 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286{
1287 ((void) ctx);
1288 ((void) p);
1289 ((void) end);
1290 ((void) cli_id);
1291 ((void) cli_id_len);
1292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001294}
1295
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001297static int ssl_cookie_check_dummy(void *ctx,
1298 const unsigned char *cookie, size_t cookie_len,
1299 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001300{
1301 ((void) ctx);
1302 ((void) cookie);
1303 ((void) cookie_len);
1304 ((void) cli_id);
1305 ((void) cli_id_len);
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001308}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001309#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001310
Paul Bakker5121ce52009-01-03 21:22:43 +00001311/*
1312 * Initialize an SSL context
1313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001315{
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001317}
1318
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001321{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001322 const mbedtls_ssl_config *conf = ssl->conf;
1323
Ronald Cron6f135e12021-12-08 16:57:54 +01001324#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1326 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1327 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1328 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001329 }
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1332 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001333 }
1334#endif
1335
1336#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1338 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1339 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001340 }
1341#endif
1342
Ronald Cron6f135e12021-12-08 16:57:54 +01001343#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1345 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1346 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1347 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001348 }
1349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1351 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001352 }
1353#endif
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1356 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001357}
1358
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001359MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001360static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1361{
1362 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 ret = ssl_conf_version_check(ssl);
1364 if (ret != 0) {
1365 return ret;
1366 }
Jerry Yu60835a82021-08-04 10:13:52 +08001367
Yanray Wangb422cab2023-12-01 16:18:10 +08001368 if (ssl->conf->f_rng == NULL) {
1369 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1370 return MBEDTLS_ERR_SSL_NO_RNG;
1371 }
1372
Jerry Yu60835a82021-08-04 10:13:52 +08001373 /* Space for further checks */
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001376}
1377
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001378/*
1379 * Setup an SSL context
1380 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1383 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Janos Follath865b3eb2019-12-16 11:46:15 +00001385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001386 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1387 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001389 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 if ((ret = ssl_conf_check(ssl)) != 0) {
1392 return ret;
1393 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001394 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001395
Paul Bakker62f2dee2012-09-28 07:31:51 +00001396 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001397 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001398 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001399
1400 /* Set to NULL in case of an error condition */
1401 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001402
Darryl Greenb33cc762019-11-28 14:29:44 +00001403#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1404 ssl->in_buf_len = in_buf_len;
1405#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1407 if (ssl->in_buf == NULL) {
1408 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001409 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001410 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001411 }
1412
Darryl Greenb33cc762019-11-28 14:29:44 +00001413#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1414 ssl->out_buf_len = out_buf_len;
1415#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1417 if (ssl->out_buf == NULL) {
1418 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001419 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001420 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 }
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001424
Johan Pascalb62bb512015-12-03 21:56:45 +01001425#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001427#endif
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001430 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001434
1435error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_free(ssl->in_buf);
1437 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001438
1439 ssl->conf = NULL;
1440
Darryl Greenb33cc762019-11-28 14:29:44 +00001441#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1442 ssl->in_buf_len = 0;
1443 ssl->out_buf_len = 0;
1444#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001445 ssl->in_buf = NULL;
1446 ssl->out_buf = NULL;
1447
1448 ssl->in_hdr = NULL;
1449 ssl->in_ctr = NULL;
1450 ssl->in_len = NULL;
1451 ssl->in_iv = NULL;
1452 ssl->in_msg = NULL;
1453
1454 ssl->out_hdr = NULL;
1455 ssl->out_ctr = NULL;
1456 ssl->out_len = NULL;
1457 ssl->out_iv = NULL;
1458 ssl->out_msg = NULL;
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001461}
1462
1463/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001464 * Reset an initialized and used SSL context for re-use while retaining
1465 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001466 *
1467 * If partial is non-zero, keep data in the input buffer and client ID.
1468 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1471 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001472{
Darryl Greenb33cc762019-11-28 14:29:44 +00001473#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1474 size_t in_buf_len = ssl->in_buf_len;
1475 size_t out_buf_len = ssl->out_buf_len;
1476#else
1477 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1478 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1479#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001480
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1482 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001483#endif
1484
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001485 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001489
1490 /* Reset incoming message parsing */
1491 ssl->in_offt = NULL;
1492 ssl->nb_zero = 0;
1493 ssl->in_msgtype = 0;
1494 ssl->in_msglen = 0;
1495 ssl->in_hslen = 0;
1496 ssl->keep_current_message = 0;
1497 ssl->transform_in = NULL;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00001498 ssl->in_hshdr = NULL;
1499 ssl->in_hsfraglen = 0;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001500
1501#if defined(MBEDTLS_SSL_PROTO_DTLS)
1502 ssl->next_record_offset = 0;
1503 ssl->in_epoch = 0;
1504#endif
1505
1506 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510 }
1511
Ronald Cronad8c17b2022-06-10 17:18:09 +02001512 ssl->send_alert = 0;
1513
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 /* Reset outgoing message writing */
1515 ssl->out_msgtype = 0;
1516 ssl->out_msglen = 0;
1517 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 memset(ssl->out_buf, 0, out_buf_len);
1519 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001520 ssl->transform_out = NULL;
1521
1522#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001524#endif
1525
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ssl->transform) {
1528 mbedtls_ssl_transform_free(ssl->transform);
1529 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001530 ssl->transform = NULL;
1531 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1533
XiaokangQian2b01dc32022-01-21 02:53:13 +00001534#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_ssl_transform_free(ssl->transform_application);
1536 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537 ssl->transform_application = NULL;
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001540#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1542 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001543 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001544#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1547 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001548 ssl->handshake->transform_handshake = NULL;
1549 }
1550
XiaokangQian2b01dc32022-01-21 02:53:13 +00001551#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001552}
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001555{
1556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1557
1558 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001559 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001562
1563 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_SSL_RENEGOTIATION)
1565 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001566 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001567
1568 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1570 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001573
Hanno Beckerb0302c42021-08-03 09:39:42 +01001574 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001575 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (ssl->session) {
1577 mbedtls_ssl_session_free(ssl->session);
1578 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001579 ssl->session = NULL;
1580 }
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001583 ssl->alpn_chosen = NULL;
1584#endif
1585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001587 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001588#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001590#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (free_cli_id) {
1592 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001593 ssl->cli_id = NULL;
1594 ssl->cli_id_len = 0;
1595 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001596#endif
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if ((ret = ssl_handshake_init(ssl)) != 0) {
1599 return ret;
1600 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001603}
1604
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001605/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001606 * Reset an initialized and used SSL context for re-use while retaining
1607 * all application-set variables, function pointers and data.
1608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001609int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001610{
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001612}
1613
1614/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 * SSL set accessors
1616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001623{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001624 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001625}
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001628void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001630 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001631}
1632#endif
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001635{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001636 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1642 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001643{
1644 ssl->disable_datagram_packing = !allow_packing;
1645}
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1648 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->hs_timeout_min = min;
1651 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001652}
1653#endif
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001657 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001658}
1659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1662 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1663 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001665 conf->f_vrfy = f_vrfy;
1666 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1671 int (*f_rng)(void *, unsigned char *, size_t),
1672 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001674 conf->f_rng = f_rng;
1675 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676}
1677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1679 void (*f_dbg)(void *, int, const char *, int, const char *),
1680 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001681{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001682 conf->f_dbg = f_dbg;
1683 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684}
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1687 void *p_bio,
1688 mbedtls_ssl_send_t *f_send,
1689 mbedtls_ssl_recv_t *f_recv,
1690 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001691{
1692 ssl->p_bio = p_bio;
1693 ssl->f_send = f_send;
1694 ssl->f_recv = f_recv;
1695 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001696}
1697
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001698#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001699void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001700{
1701 ssl->mtu = mtu;
1702}
1703#endif
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001706{
1707 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001708}
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1711 void *p_timer,
1712 mbedtls_ssl_set_timer_t *f_set_timer,
1713 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001714{
1715 ssl->p_timer = p_timer;
1716 ssl->f_set_timer = f_set_timer;
1717 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001718
1719 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001721}
1722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001724void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1725 void *p_cache,
1726 mbedtls_ssl_cache_get_t *f_get_cache,
1727 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001728{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001729 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001730 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001731 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001736int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001737{
Janos Follath865b3eb2019-12-16 11:46:15 +00001738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001741 session == NULL ||
1742 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001745 }
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ssl->handshake->resume == 1) {
1748 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1749 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001750
Jerry Yu21092062022-10-10 21:21:31 +08001751#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron8d630842024-04-04 14:05:21 +02001753#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001754 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001758 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1760 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1761 session->ciphersuite));
1762 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001763 }
Ronald Cron8d630842024-04-04 14:05:21 +02001764#else
1765 /*
1766 * If session tickets are not enabled, it is not possible to resume a
1767 * TLS 1.3 session, thus do not make any change to the SSL context in
1768 * the first place.
1769 */
1770 return 0;
1771#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001772 }
Jerry Yu21092062022-10-10 21:21:31 +08001773#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1776 session)) != 0) {
1777 return ret;
1778 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001779
Paul Bakker0a597072012-09-25 21:55:46 +00001780 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1787 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001788{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001789 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001790}
1791
Ronald Cron6f135e12021-12-08 16:57:54 +01001792#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001793void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1794 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001795{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001796 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001797}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001798
1799#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001800void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1801 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001802{
1803 conf->early_data_enabled = early_data_enabled;
1804}
Jerry Yucc4e0072022-11-22 17:22:22 +08001805
1806#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001807void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001809{
Jerry Yu39da9852022-12-06 16:58:36 +08001810 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001811}
1812#endif /* MBEDTLS_SSL_SRV_C */
1813
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001814#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001815#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001818void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1819 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001820{
1821 conf->cert_profile = profile;
1822}
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001825{
1826 mbedtls_ssl_key_cert *cur = key_cert, *next;
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001829 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001831 cur = next;
1832 }
1833}
1834
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001835/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001836MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001837static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1838 mbedtls_x509_crt *cert,
1839 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001840{
niisato8ee24222018-06-25 19:05:48 +09001841 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001844 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001846 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1851 if (new_cert == NULL) {
1852 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1853 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001854
niisato8ee24222018-06-25 19:05:48 +09001855 new_cert->cert = cert;
1856 new_cert->key = key;
1857 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001858
Glenn Strauss36872db2022-01-22 05:06:31 -05001859 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001861 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001863 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
niisato8ee24222018-06-25 19:05:48 +09001867 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001868 }
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001871}
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001874 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001876{
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001878}
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001881 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001883{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001884 conf->ca_chain = ca_chain;
1885 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001886
1887#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1888 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1889 * cannot be used together. */
1890 conf->f_ca_cb = NULL;
1891 conf->p_ca_cb = NULL;
1892#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001893}
Hanno Becker5adaad92019-03-27 16:54:37 +00001894
1895#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001896void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1897 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1898 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001899{
1900 conf->f_ca_cb = f_ca_cb;
1901 conf->p_ca_cb = p_ca_cb;
1902
1903 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1904 * cannot be used together. */
1905 conf->ca_chain = NULL;
1906 conf->ca_crl = NULL;
1907}
1908#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001910
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001911#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001912const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1913 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001914{
1915 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001917}
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1920 mbedtls_x509_crt *own_cert,
1921 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001922{
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1924 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001925}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1928 mbedtls_x509_crt *ca_chain,
1929 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001930{
1931 ssl->handshake->sni_ca_chain = ca_chain;
1932 ssl->handshake->sni_ca_crl = ca_crl;
1933}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001934
Glenn Strauss999ef702022-03-11 01:37:23 -05001935#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001936void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1937 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001938{
1939 ssl->handshake->dn_hints = crt;
1940}
1941#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1944 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001945{
1946 ssl->handshake->sni_authmode = authmode;
1947}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001948#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1949
Hanno Becker8927c832019-04-03 12:52:50 +01001950#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001951void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1952 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1953 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001954{
1955 ssl->f_vrfy = f_vrfy;
1956 ssl->p_vrfy = p_vrfy;
1957}
1958#endif
1959
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001960#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001961
Valerio Setti016f6822022-12-09 14:17:50 +01001962#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001963static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1964static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1965
Valerio Setti016f6822022-12-09 14:17:50 +01001966static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 mbedtls_ssl_context *ssl,
1968 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001969{
1970 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001971 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001972 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001973 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001974 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001975 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1977 psa_pake_cs_set_primitive(&cipher_suite,
1978 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1979 PSA_ECC_FAMILY_SECP_R1,
1980 256));
1981 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1984 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001985 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001989 user = jpake_server_id;
1990 user_len = sizeof(jpake_server_id);
1991 peer = jpake_client_id;
1992 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001994 user = jpake_client_id;
1995 user_len = sizeof(jpake_client_id);
1996 peer = jpake_server_id;
1997 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001999
Przemek Stekiel4cd20312023-03-01 11:11:28 +01002000 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2001 if (status != PSA_SUCCESS) {
2002 return status;
2003 }
2004
2005 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002007 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 }
Valerio Setti016f6822022-12-09 14:17:50 +01002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2011 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002012 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 }
Valerio Setti016f6822022-12-09 14:17:50 +01002014
2015 ssl->handshake->psa_pake_ctx_is_ok = 1;
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002018}
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2021 const unsigned char *pw,
2022 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002023{
2024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2025 psa_status_t status;
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (ssl->handshake == NULL || ssl->conf == NULL) {
2028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002029 }
2030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 /* Empty password is not valid */
2032 if ((pw == NULL) || (pw_len == 0)) {
2033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2034 }
2035
2036 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2037 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2038 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2039
2040 status = psa_import_key(&attributes, pw, pw_len,
2041 &ssl->handshake->psa_pake_password);
2042 if (status != PSA_SUCCESS) {
2043 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2044 }
2045
2046 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2047 ssl->handshake->psa_pake_password);
2048 if (status != PSA_SUCCESS) {
2049 psa_destroy_key(ssl->handshake->psa_pake_password);
2050 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2051 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2052 }
2053
2054 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002055}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2058 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002059{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002060 psa_status_t status;
2061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (ssl->handshake == NULL || ssl->conf == NULL) {
2063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002064 }
2065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (mbedtls_svc_key_id_is_null(pwd)) {
2067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2068 }
2069
2070 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2071 if (status != PSA_SUCCESS) {
2072 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2073 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2074 }
2075
2076 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002077}
2078#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2080 const unsigned char *pw,
2081 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002082{
2083 mbedtls_ecjpake_role role;
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (ssl->handshake == NULL || ssl->conf == NULL) {
2086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2087 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002088
Valerio Settic689ed82022-12-07 14:40:38 +01002089 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if ((pw == NULL) || (pw_len == 0)) {
2091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2092 }
Valerio Settic689ed82022-12-07 14:40:38 +01002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002095 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002097 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2101 role,
2102 MBEDTLS_MD_SHA256,
2103 MBEDTLS_ECP_DP_SECP256R1,
2104 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002105}
Valerio Setti02c25b52022-11-15 14:08:42 +01002106#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002107#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002108
Ronald Cron73fe8df2022-10-05 14:31:43 +02002109#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002110int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002111{
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 if (conf->psk_identity == NULL ||
2113 conf->psk_identity_len == 0) {
2114 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002115 }
2116
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2119 return 1;
2120 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002121#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 if (conf->psk != NULL && conf->psk_len != 0) {
2124 return 1;
2125 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002128}
2129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131{
2132 /* Remove reference to existing PSK, if any. */
2133#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002135 /* The maintenance of the PSK key slot is the
2136 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002137 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002138 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002139#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002141 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002142 conf->psk = NULL;
2143 conf->psk_len = 0;
2144 }
2145
2146 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (conf->psk_identity != NULL) {
2148 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002149 conf->psk_identity = NULL;
2150 conf->psk_identity_len = 0;
2151 }
2152}
2153
Hanno Becker7390c712018-11-15 13:33:04 +00002154/* This function assumes that PSK identity in the SSL config is unset.
2155 * It checks that the provided identity is well-formed and attempts
2156 * to make a copy of it in the SSL config.
2157 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002159static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2160 unsigned char const *psk_identity,
2161 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002162{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002163 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002165 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 (psk_identity_len >> 16) != 0 ||
2167 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2168 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002169 }
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2172 if (conf->psk_identity == NULL) {
2173 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2174 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002175
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002176 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002180}
2181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2183 const unsigned char *psk, size_t psk_len,
2184 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002185{
Janos Follath865b3eb2019-12-16 11:46:15 +00002186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002187
2188 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2190 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2191 }
Hanno Becker7390c712018-11-15 13:33:04 +00002192
2193 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 if (psk == NULL) {
2195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2196 }
2197 if (psk_len == 0) {
2198 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2199 }
2200 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2201 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2202 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2205 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2206 }
Hanno Becker7390c712018-11-15 13:33:04 +00002207 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002209
2210 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2212 if (ret != 0) {
2213 ssl_conf_remove_psk(conf);
2214 }
Hanno Becker7390c712018-11-15 13:33:04 +00002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002217}
2218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002220{
2221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002223 /* The maintenance of the external PSK key slot is the
2224 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (ssl->handshake->psk_opaque_is_internal) {
2226 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002227 ssl->handshake->psk_opaque_is_internal = 0;
2228 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002229 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002230 }
Neil Armstronge952a302022-05-03 10:22:14 +02002231#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002233 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002235 ssl->handshake->psk_len = 0;
lhuang046d4d94f2024-06-11 12:37:02 -07002236 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002237 }
Neil Armstronge952a302022-05-03 10:22:14 +02002238#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002239}
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2242 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002243{
Neil Armstrong501c9322022-05-03 09:35:09 +02002244#if defined(MBEDTLS_USE_PSA_CRYPTO)
2245 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002247 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002249#endif /* MBEDTLS_USE_PSA_CRYPTO */
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 if (psk == NULL || ssl->handshake == NULL) {
2252 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2253 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2256 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2257 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002260
Neil Armstrong501c9322022-05-03 09:35:09 +02002261#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2264 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2265 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2266 } else {
2267 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2268 }
2269 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002270 }
2271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002272
Jerry Yu568ec252022-07-22 21:27:34 +08002273#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2275 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2276 psa_set_key_usage_flags(&key_attributes,
2277 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002278 }
2279#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 psa_set_key_algorithm(&key_attributes, alg);
2282 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2285 if (status != PSA_SUCCESS) {
2286 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2287 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002288
2289 /* Allow calling psa_destroy_key() on psk remove */
2290 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002292#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2294 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2295 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002296
2297 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002301#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002302}
2303
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002305int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2306 mbedtls_svc_key_id_t psk,
2307 const unsigned char *psk_identity,
2308 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002309{
Janos Follath865b3eb2019-12-16 11:46:15 +00002310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002311
2312 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2314 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2315 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002316
Hanno Becker7390c712018-11-15 13:33:04 +00002317 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (mbedtls_svc_key_id_is_null(psk)) {
2319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2320 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002321 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002322
2323 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2325 psk_identity_len);
2326 if (ret != 0) {
2327 ssl_conf_remove_psk(conf);
2328 }
Hanno Becker7390c712018-11-15 13:33:04 +00002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002331}
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2334 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002335{
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if ((mbedtls_svc_key_id_is_null(psk)) ||
2337 (ssl->handshake == NULL)) {
2338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2339 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002342 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002344}
2345#endif /* MBEDTLS_USE_PSA_CRYPTO */
2346
Jerry Yu8897c072022-08-12 13:56:53 +08002347#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002348void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2349 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2350 size_t),
2351 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002352{
2353 conf->f_psk = f_psk;
2354 conf->p_psk = p_psk;
2355}
Jerry Yu8897c072022-08-12 13:56:53 +08002356#endif /* MBEDTLS_SSL_SRV_C */
2357
Ronald Cron73fe8df2022-10-05 14:31:43 +02002358#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002359
2360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002361static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002363{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002364#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (alg == PSA_ALG_CBC_NO_PADDING) {
2366 return MBEDTLS_SSL_MODE_CBC;
2367 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002368#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 if (PSA_ALG_IS_AEAD(alg)) {
2370 return MBEDTLS_SSL_MODE_AEAD;
2371 }
2372 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002373}
2374
2375#else /* MBEDTLS_USE_PSA_CRYPTO */
2376
2377static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002379{
2380#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (mode == MBEDTLS_MODE_CBC) {
2382 return MBEDTLS_SSL_MODE_CBC;
2383 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002384#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2385
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002386#if defined(MBEDTLS_GCM_C) || \
2387 defined(MBEDTLS_CCM_C) || \
2388 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002390 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 mode == MBEDTLS_MODE_CHACHAPOLY) {
2392 return MBEDTLS_SSL_MODE_AEAD;
2393 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002394#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002397}
Gilles Peskine301711e2022-04-26 16:57:05 +02002398#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002399
Gilles Peskinee108d982022-04-26 16:50:40 +02002400static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2401 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002403{
2404#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2406 base_mode == MBEDTLS_SSL_MODE_CBC) {
2407 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002408 }
2409#else
2410 (void) encrypt_then_mac;
2411#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002413}
2414
Neil Armstrongab555e02022-04-04 11:07:59 +02002415mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002417{
Gilles Peskinee108d982022-04-26 16:50:40 +02002418 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002419#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002421#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002423#endif
2424 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002425
Gilles Peskinee108d982022-04-26 16:50:40 +02002426 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002428 encrypt_then_mac = transform->encrypt_then_mac;
2429#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002431}
2432
Neil Armstrongab555e02022-04-04 11:07:59 +02002433mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002434#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002436#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002438{
Gilles Peskinee108d982022-04-26 16:50:40 +02002439 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2440
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002441#if defined(MBEDTLS_USE_PSA_CRYPTO)
2442 psa_status_t status;
2443 psa_algorithm_t alg;
2444 psa_key_type_t type;
2445 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002446 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2447 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (status == PSA_SUCCESS) {
2449 base_mode = mbedtls_ssl_get_base_mode(alg);
2450 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002451#else
2452 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002453 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002455 base_mode =
2456 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002458 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002459#endif /* MBEDTLS_USE_PSA_CRYPTO */
2460
Gilles Peskinee108d982022-04-26 16:50:40 +02002461#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2462 int encrypt_then_mac = 0;
2463#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002465}
2466
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002467#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2470 size_t taglen,
2471 psa_algorithm_t *alg,
2472 psa_key_type_t *key_type,
2473 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002474{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002475#if !defined(MBEDTLS_SSL_HAVE_CCM)
2476 (void) taglen;
2477#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002479#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002480 case MBEDTLS_CIPHER_AES_128_CBC:
2481 *alg = PSA_ALG_CBC_NO_PADDING;
2482 *key_type = PSA_KEY_TYPE_AES;
2483 *key_size = 128;
2484 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002485#endif
2486#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002487 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002489 *key_type = PSA_KEY_TYPE_AES;
2490 *key_size = 128;
2491 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002492#endif
2493#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002494 case MBEDTLS_CIPHER_AES_128_GCM:
2495 *alg = PSA_ALG_GCM;
2496 *key_type = PSA_KEY_TYPE_AES;
2497 *key_size = 128;
2498 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002499#endif
2500#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002501 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002503 *key_type = PSA_KEY_TYPE_AES;
2504 *key_size = 192;
2505 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002506#endif
2507#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002508 case MBEDTLS_CIPHER_AES_192_GCM:
2509 *alg = PSA_ALG_GCM;
2510 *key_type = PSA_KEY_TYPE_AES;
2511 *key_size = 192;
2512 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002513#endif
2514#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002515 case MBEDTLS_CIPHER_AES_256_CBC:
2516 *alg = PSA_ALG_CBC_NO_PADDING;
2517 *key_type = PSA_KEY_TYPE_AES;
2518 *key_size = 256;
2519 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002520#endif
2521#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002522 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002524 *key_type = PSA_KEY_TYPE_AES;
2525 *key_size = 256;
2526 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002527#endif
2528#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002529 case MBEDTLS_CIPHER_AES_256_GCM:
2530 *alg = PSA_ALG_GCM;
2531 *key_type = PSA_KEY_TYPE_AES;
2532 *key_size = 256;
2533 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002534#endif
2535#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002536 case MBEDTLS_CIPHER_ARIA_128_CBC:
2537 *alg = PSA_ALG_CBC_NO_PADDING;
2538 *key_type = PSA_KEY_TYPE_ARIA;
2539 *key_size = 128;
2540 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002541#endif
2542#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002543 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002545 *key_type = PSA_KEY_TYPE_ARIA;
2546 *key_size = 128;
2547 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002548#endif
2549#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002550 case MBEDTLS_CIPHER_ARIA_128_GCM:
2551 *alg = PSA_ALG_GCM;
2552 *key_type = PSA_KEY_TYPE_ARIA;
2553 *key_size = 128;
2554 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002555#endif
2556#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002557 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002559 *key_type = PSA_KEY_TYPE_ARIA;
2560 *key_size = 192;
2561 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002562#endif
2563#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002564 case MBEDTLS_CIPHER_ARIA_192_GCM:
2565 *alg = PSA_ALG_GCM;
2566 *key_type = PSA_KEY_TYPE_ARIA;
2567 *key_size = 192;
2568 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002569#endif
2570#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002571 case MBEDTLS_CIPHER_ARIA_256_CBC:
2572 *alg = PSA_ALG_CBC_NO_PADDING;
2573 *key_type = PSA_KEY_TYPE_ARIA;
2574 *key_size = 256;
2575 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002576#endif
2577#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002578 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002580 *key_type = PSA_KEY_TYPE_ARIA;
2581 *key_size = 256;
2582 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002583#endif
2584#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002585 case MBEDTLS_CIPHER_ARIA_256_GCM:
2586 *alg = PSA_ALG_GCM;
2587 *key_type = PSA_KEY_TYPE_ARIA;
2588 *key_size = 256;
2589 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002590#endif
2591#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002592 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2593 *alg = PSA_ALG_CBC_NO_PADDING;
2594 *key_type = PSA_KEY_TYPE_CAMELLIA;
2595 *key_size = 128;
2596 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002597#endif
2598#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002599 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002601 *key_type = PSA_KEY_TYPE_CAMELLIA;
2602 *key_size = 128;
2603 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002604#endif
2605#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002606 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2607 *alg = PSA_ALG_GCM;
2608 *key_type = PSA_KEY_TYPE_CAMELLIA;
2609 *key_size = 128;
2610 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002611#endif
2612#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002613 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002615 *key_type = PSA_KEY_TYPE_CAMELLIA;
2616 *key_size = 192;
2617 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002618#endif
2619#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002620 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2621 *alg = PSA_ALG_GCM;
2622 *key_type = PSA_KEY_TYPE_CAMELLIA;
2623 *key_size = 192;
2624 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002625#endif
2626#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002627 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2628 *alg = PSA_ALG_CBC_NO_PADDING;
2629 *key_type = PSA_KEY_TYPE_CAMELLIA;
2630 *key_size = 256;
2631 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002632#endif
2633#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002634 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002636 *key_type = PSA_KEY_TYPE_CAMELLIA;
2637 *key_size = 256;
2638 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002639#endif
2640#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002641 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2642 *alg = PSA_ALG_GCM;
2643 *key_type = PSA_KEY_TYPE_CAMELLIA;
2644 *key_size = 256;
2645 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002646#endif
2647#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002648 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2649 *alg = PSA_ALG_CHACHA20_POLY1305;
2650 *key_type = PSA_KEY_TYPE_CHACHA20;
2651 *key_size = 256;
2652 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002653#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002654 case MBEDTLS_CIPHER_NULL:
2655 *alg = MBEDTLS_SSL_NULL_CIPHER;
2656 *key_type = 0;
2657 *key_size = 0;
2658 break;
2659 default:
2660 return PSA_ERROR_NOT_SUPPORTED;
2661 }
2662
2663 return PSA_SUCCESS;
2664}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002665#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002666
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002667#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002668int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2669 const unsigned char *dhm_P, size_t P_len,
2670 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002671{
Janos Follath865b3eb2019-12-16 11:46:15 +00002672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 mbedtls_mpi_free(&conf->dhm_P);
2675 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2678 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2679 mbedtls_mpi_free(&conf->dhm_P);
2680 mbedtls_mpi_free(&conf->dhm_G);
2681 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002682 }
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002685}
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002688{
Janos Follath865b3eb2019-12-16 11:46:15 +00002689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 mbedtls_mpi_free(&conf->dhm_P);
2692 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2695 &conf->dhm_P)) != 0 ||
2696 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2697 &conf->dhm_G)) != 0) {
2698 mbedtls_mpi_free(&conf->dhm_P);
2699 mbedtls_mpi_free(&conf->dhm_G);
2700 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002701 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002704}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002705#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002706
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002707#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2708/*
2709 * Set the minimum length for Diffie-Hellman parameters
2710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002711void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2712 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002713{
2714 conf->dhm_min_bitlen = bitlen;
2715}
2716#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2717
Ronald Crone68ab4f2022-10-05 12:46:29 +02002718#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002719#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002720/*
2721 * Set allowed/preferred hashes for handshake signatures
2722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2724 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002725{
2726 conf->sig_hashes = hashes;
2727}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002728#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002729
Jerry Yuf017ee42022-01-12 15:49:48 +08002730/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002731void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2732 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002733{
Jerry Yuf017ee42022-01-12 15:49:48 +08002734#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2735 conf->sig_hashes = NULL;
2736#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2737 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002738}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002739#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002740
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002741#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002742#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002743/*
2744 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002745 *
2746 * mbedtls_ssl_setup() takes the provided list
2747 * and translates it to a list of IANA TLS group identifiers,
2748 * stored in ssl->handshake->group_list.
2749 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002750 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002751void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2752 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002753{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002754 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002755 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002756}
Brett Warrene0edc842021-08-17 09:53:13 +01002757#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002758#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002759
Brett Warrene0edc842021-08-17 09:53:13 +01002760/*
2761 * Set the allowed groups
2762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2764 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002765{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002766#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002767 conf->curve_list = NULL;
2768#endif
2769 conf->group_list = group_list;
2770}
2771
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002772#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002773int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002774{
Hanno Becker947194e2017-04-07 13:25:49 +01002775 /* Initialize to suppress unnecessary compiler warning */
2776 size_t hostname_len = 0;
2777
2778 /* Check if new hostname is valid before
2779 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (hostname != NULL) {
2781 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2784 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2785 }
Hanno Becker947194e2017-04-07 13:25:49 +01002786 }
2787
2788 /* Now it's clear that we will overwrite the old hostname,
2789 * so we can free it safely */
2790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002792 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002793 }
2794
2795 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002796
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002798 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 } else {
2800 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2801 if (ssl->hostname == NULL) {
2802 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2803 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002806
Hanno Becker947194e2017-04-07 13:25:49 +01002807 ssl->hostname[hostname_len] = '\0';
2808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002811}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002812#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002814#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002815void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2816 int (*f_sni)(void *, mbedtls_ssl_context *,
2817 const unsigned char *, size_t),
2818 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002819{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002820 conf->f_sni = f_sni;
2821 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002822}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002826int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002827{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002828 size_t cur_len, tot_len;
2829 const char **p;
2830
2831 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002832 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2833 * MUST NOT be truncated."
2834 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002835 */
2836 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 for (p = protos; *p != NULL; p++) {
2838 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002839 tot_len += cur_len;
2840
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if ((cur_len == 0) ||
2842 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2843 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2844 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2845 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002846 }
2847
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002848 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002851}
2852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002854{
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002858
Johan Pascalb62bb512015-12-03 21:56:45 +01002859#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002860void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2861 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002862{
2863 conf->dtls_srtp_mki_support = support_mki_value;
2864}
2865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2867 unsigned char *mki_value,
2868 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002869{
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2871 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002872 }
Ron Eldor591f1622018-01-22 12:30:04 +02002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2875 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002876 }
Ron Eldor591f1622018-01-22 12:30:04 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002879 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002881}
2882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2884 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002885{
Johan Pascal253d0262020-09-22 13:04:45 +02002886 const mbedtls_ssl_srtp_profile *p;
2887 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002888
Johan Pascal253d0262020-09-22 13:04:45 +02002889 /* check the profiles list: all entry must be valid,
2890 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2892 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2893 p++) {
2894 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002895 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002897 /* unsupported value, stop parsing and set the size to an error value */
2898 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002899 }
2900 }
2901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2903 conf->dtls_srtp_profile_list = NULL;
2904 conf->dtls_srtp_profile_list_len = 0;
2905 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002906 }
2907
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002908 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002909 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002910
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002912}
2913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2915 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002916{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002917 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2918 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002920 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002922 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2924 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002925 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002926}
Johan Pascalb62bb512015-12-03 21:56:45 +01002927#endif /* MBEDTLS_SSL_DTLS_SRTP */
2928
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302929#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002930void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002931{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002932 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002933}
2934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002936{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002937 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002938}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302939#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002940
Janos Follath088ce432017-04-10 12:42:31 +01002941#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002942void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2943 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002944{
2945 conf->cert_req_ca_list = cert_req_ca_list;
2946}
2947#endif
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002950void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002951{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002952 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002953}
2954#endif
2955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002956#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002957void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002958{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002959 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002960}
2961#endif
2962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002964int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002965{
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2967 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002969 }
2970
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002971 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002979 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002980}
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002983void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002984{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002985 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002986}
2987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002990 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002991}
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2994 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002995{
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003001#if defined(MBEDTLS_SSL_CLI_C)
Ronald Crond67f8012024-08-28 07:45:57 +02003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003004{
Ronald Crond67f8012024-08-28 07:45:57 +02003005 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_MASK;
3006 conf->session_tickets |= (use_tickets != 0) <<
3007 MBEDTLS_SSL_SESSION_TICKETS_TLS1_2_BIT;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003008}
Ronald Crond67f8012024-08-28 07:45:57 +02003009
Ronald Cronbedddd72024-08-27 14:18:50 +02003010#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron9f44c882024-08-28 16:44:10 +02003011void mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
3012 mbedtls_ssl_config *conf, int signal_new_session_tickets)
Ronald Cronbedddd72024-08-27 14:18:50 +02003013{
Ronald Crond67f8012024-08-28 07:45:57 +02003014 conf->session_tickets &= ~MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_MASK;
Ronald Cron9f44c882024-08-28 16:44:10 +02003015 conf->session_tickets |= (signal_new_session_tickets != 0) <<
Ronald Crond67f8012024-08-28 07:45:57 +02003016 MBEDTLS_SSL_SESSION_TICKETS_TLS1_3_BIT;
3017}
Ronald Cronbedddd72024-08-27 14:18:50 +02003018#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3019#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker606b4ba2013-08-14 16:52:14 +02003020
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003021#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003022
Jerry Yud0766ec2022-09-22 10:46:57 +08003023#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003024void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3025 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003026{
Jerry Yud0766ec2022-09-22 10:46:57 +08003027 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003028}
3029#endif
3030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3032 mbedtls_ssl_ticket_write_t *f_ticket_write,
3033 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3034 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003035{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003036 conf->f_ticket_write = f_ticket_write;
3037 conf->f_ticket_parse = f_ticket_parse;
3038 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003039}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3044 mbedtls_ssl_export_keys_t *f_export_keys,
3045 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003046{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003047 ssl->f_export_keys = f_export_keys;
3048 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003049}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003050
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003051#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003052void mbedtls_ssl_conf_async_private_cb(
3053 mbedtls_ssl_config *conf,
3054 mbedtls_ssl_async_sign_t *f_async_sign,
3055 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3056 mbedtls_ssl_async_resume_t *f_async_resume,
3057 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003059{
3060 conf->f_async_sign_start = f_async_sign;
3061 conf->f_async_decrypt_start = f_async_decrypt;
3062 conf->f_async_resume = f_async_resume;
3063 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003064 conf->p_async_config_data = async_config_data;
3065}
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003068{
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003070}
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003073{
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (ssl->handshake == NULL) {
3075 return NULL;
3076 } else {
3077 return ssl->handshake->user_async_ctx;
3078 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003079}
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3082 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003083{
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003085 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003087}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003088#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003089
Paul Bakker5121ce52009-01-03 21:22:43 +00003090/*
3091 * SSL get accessors
3092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003093uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003094{
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 if (ssl->session != NULL) {
3096 return ssl->session->verify_result;
3097 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (ssl->session_negotiate != NULL) {
3100 return ssl->session_negotiate->verify_result;
3101 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003107{
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl == NULL || ssl->session == NULL) {
3109 return 0;
3110 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003113}
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003116{
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (ssl == NULL || ssl->session == NULL) {
3118 return NULL;
3119 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003120
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003122}
3123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3128 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003129 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003131 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003133 }
3134 }
3135#endif
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003138 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003140 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003142 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003144 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003145}
3146
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003147#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3148
3149size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3150{
3151 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3152 size_t record_size_limit = max_len;
3153
3154 if (ssl->session != NULL &&
3155 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3156 ssl->session->record_size_limit < max_len) {
3157 record_size_limit = ssl->session->record_size_limit;
3158 }
3159
3160 // TODO: this is currently untested
3161 /* During a handshake, use the value being negotiated */
3162 if (ssl->session_negotiate != NULL &&
3163 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3164 ssl->session_negotiate->record_size_limit < max_len) {
3165 record_size_limit = ssl->session_negotiate->record_size_limit;
3166 }
3167
3168 return record_size_limit;
3169}
3170#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3171
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003172#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003173size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003174{
David Horstmann95d516f2021-05-04 18:36:56 +01003175 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003176 size_t read_mfl;
3177
Jerry Yuddda0502022-12-01 19:43:12 +08003178#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003179 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3181 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3182 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003183 }
Jerry Yuddda0502022-12-01 19:43:12 +08003184#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003185
3186 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if (ssl->session_out != NULL) {
3188 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3189 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003190 max_len = read_mfl;
3191 }
3192 }
3193
Jerry Yuddda0502022-12-01 19:43:12 +08003194 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 if (ssl->session_negotiate != NULL) {
3196 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3197 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003198 max_len = read_mfl;
3199 }
3200 }
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003203}
3204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003206{
3207 size_t max_len;
3208
3209 /*
3210 * Assume mfl_code is correct since it was checked when set
3211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003213
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003214 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 if (ssl->session_out != NULL &&
3216 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3217 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003218 }
3219
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003220 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if (ssl->session_negotiate != NULL &&
3222 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3223 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003224 }
3225
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003227}
3228#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3229
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003231size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003232{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003233 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3235 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3236 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3237 return 0;
3238 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003239
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3241 return ssl->mtu;
3242 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 if (ssl->mtu == 0) {
3245 return ssl->handshake->mtu;
3246 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 return ssl->mtu < ssl->handshake->mtu ?
3249 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003250}
3251#endif /* MBEDTLS_SSL_PROTO_DTLS */
3252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003254{
3255 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3256
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003257#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003258 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003259 !defined(MBEDTLS_SSL_PROTO_DTLS)
3260 (void) ssl;
3261#endif
3262
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003263#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003267 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003269#endif
3270
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003271#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3272 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3273
3274 if (max_len > record_size_limit) {
3275 max_len = record_size_limit;
3276 }
3277#endif
3278
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003279 if (ssl->transform_out != NULL &&
3280 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003281 /*
3282 * In TLS 1.3 case, when records are protected, `max_len` as computed
3283 * above is the maximum length of the TLSInnerPlaintext structure that
3284 * along the plaintext payload contains the inner content type (one byte)
3285 * and some zero padding. Given the algorithm used for padding
3286 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3287 * the plaintext payload. Round down to a multiple of
3288 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3289 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003290 */
3291 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3292 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3293 }
3294
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3297 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3298 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003299 const size_t overhead = (size_t) ret;
3300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (ret < 0) {
3302 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003303 }
3304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (mtu <= overhead) {
3306 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3308 }
3309
3310 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003311 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003314#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003315
Hanno Becker0defedb2018-08-10 12:35:02 +01003316#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003317 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3318 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003319 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320#endif
3321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003323}
3324
Gilles Peskine449bd832023-01-11 14:50:10 +01003325int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003326{
3327 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3328
3329#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3330 (void) ssl;
3331#endif
3332
3333#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003337 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003339#endif
3340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003342}
3343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003345const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003346{
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (ssl == NULL || ssl->session == NULL) {
3348 return NULL;
3349 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003350
Hanno Beckere6824572019-02-07 13:18:46 +00003351#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003353#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003355#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003360int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3361 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003362{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003363 int ret;
3364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003366 dst == NULL ||
3367 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3369 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003370 }
3371
Hanno Beckere810bbc2021-05-14 16:01:05 +01003372 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3373 * idempotent: Each session can only be exported once.
3374 *
3375 * (This is in preparation for TLS 1.3 support where we will
3376 * need the ability to export multiple sessions (aka tickets),
3377 * which will be achieved by calling mbedtls_ssl_get_session()
3378 * multiple times until it fails.)
3379 *
3380 * Check whether we have already exported the current session,
3381 * and fail if so.
3382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 if (ssl->session->exported == 1) {
3384 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3385 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3388 if (ret != 0) {
3389 return ret;
3390 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003391
3392 /* Remember that we've exported the session. */
3393 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003397
David Horstmanncb01b362024-02-29 17:31:46 +00003398#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3399
3400/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003401 *
David Horstmanncb01b362024-02-29 17:31:46 +00003402 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003403 */
3404static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3405 unsigned char *buf,
3406 size_t buf_len)
3407{
3408 unsigned char *p = buf;
3409 size_t used = 0;
3410
3411#if defined(MBEDTLS_HAVE_TIME)
3412 uint64_t start;
3413#endif
3414#if defined(MBEDTLS_X509_CRT_PARSE_C)
3415#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3416 size_t cert_len;
3417#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3418#endif /* MBEDTLS_X509_CRT_PARSE_C */
3419
3420 /*
3421 * Time
3422 */
3423#if defined(MBEDTLS_HAVE_TIME)
3424 used += 8;
3425
3426 if (used <= buf_len) {
3427 start = (uint64_t) session->start;
3428
3429 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3430 p += 8;
3431 }
3432#endif /* MBEDTLS_HAVE_TIME */
3433
3434 /*
3435 * Basic mandatory fields
3436 */
3437 used += 1 /* id_len */
3438 + sizeof(session->id)
3439 + sizeof(session->master)
3440 + 4; /* verify_result */
3441
3442 if (used <= buf_len) {
3443 *p++ = MBEDTLS_BYTE_0(session->id_len);
3444 memcpy(p, session->id, 32);
3445 p += 32;
3446
3447 memcpy(p, session->master, 48);
3448 p += 48;
3449
3450 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3451 p += 4;
3452 }
3453
3454 /*
3455 * Peer's end-entity certificate
3456 */
3457#if defined(MBEDTLS_X509_CRT_PARSE_C)
3458#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3459 if (session->peer_cert == NULL) {
3460 cert_len = 0;
3461 } else {
3462 cert_len = session->peer_cert->raw.len;
3463 }
3464
3465 used += 3 + cert_len;
3466
3467 if (used <= buf_len) {
3468 *p++ = MBEDTLS_BYTE_2(cert_len);
3469 *p++ = MBEDTLS_BYTE_1(cert_len);
3470 *p++ = MBEDTLS_BYTE_0(cert_len);
3471
3472 if (session->peer_cert != NULL) {
3473 memcpy(p, session->peer_cert->raw.p, cert_len);
3474 p += cert_len;
3475 }
3476 }
3477#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3478 if (session->peer_cert_digest != NULL) {
3479 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3480 if (used <= buf_len) {
3481 *p++ = (unsigned char) session->peer_cert_digest_type;
3482 *p++ = (unsigned char) session->peer_cert_digest_len;
3483 memcpy(p, session->peer_cert_digest,
3484 session->peer_cert_digest_len);
3485 p += session->peer_cert_digest_len;
3486 }
3487 } else {
3488 used += 2;
3489 if (used <= buf_len) {
3490 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3491 *p++ = 0;
3492 }
3493 }
3494#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3495#endif /* MBEDTLS_X509_CRT_PARSE_C */
3496
3497 /*
3498 * Session ticket if any, plus associated data
3499 */
3500#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3501#if defined(MBEDTLS_SSL_CLI_C)
3502 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3503 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3504
3505 if (used <= buf_len) {
3506 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3507 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3508 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3509
3510 if (session->ticket != NULL) {
3511 memcpy(p, session->ticket, session->ticket_len);
3512 p += session->ticket_len;
3513 }
3514
3515 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3516 p += 4;
3517 }
3518 }
3519#endif /* MBEDTLS_SSL_CLI_C */
3520#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3521 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3522 used += 8;
3523
3524 if (used <= buf_len) {
3525 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3526 p += 8;
3527 }
3528 }
3529#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3530#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3531
3532 /*
3533 * Misc extension-related info
3534 */
3535#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3536 used += 1;
3537
3538 if (used <= buf_len) {
3539 *p++ = session->mfl_code;
3540 }
3541#endif
3542
3543#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3544 used += 1;
3545
3546 if (used <= buf_len) {
3547 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3548 }
3549#endif
3550
3551 return used;
3552}
3553
3554MBEDTLS_CHECK_RETURN_CRITICAL
3555static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3556 const unsigned char *buf,
3557 size_t len)
3558{
3559#if defined(MBEDTLS_HAVE_TIME)
3560 uint64_t start;
3561#endif
3562#if defined(MBEDTLS_X509_CRT_PARSE_C)
3563#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3564 size_t cert_len;
3565#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3566#endif /* MBEDTLS_X509_CRT_PARSE_C */
3567
3568 const unsigned char *p = buf;
3569 const unsigned char * const end = buf + len;
3570
3571 /*
3572 * Time
3573 */
3574#if defined(MBEDTLS_HAVE_TIME)
3575 if (8 > (size_t) (end - p)) {
3576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3577 }
3578
3579 start = MBEDTLS_GET_UINT64_BE(p, 0);
3580 p += 8;
3581
3582 session->start = (time_t) start;
3583#endif /* MBEDTLS_HAVE_TIME */
3584
3585 /*
3586 * Basic mandatory fields
3587 */
3588 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3590 }
3591
3592 session->id_len = *p++;
3593 memcpy(session->id, p, 32);
3594 p += 32;
3595
3596 memcpy(session->master, p, 48);
3597 p += 48;
3598
3599 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3600 p += 4;
3601
3602 /* Immediately clear invalid pointer values that have been read, in case
3603 * we exit early before we replaced them with valid ones. */
3604#if defined(MBEDTLS_X509_CRT_PARSE_C)
3605#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3606 session->peer_cert = NULL;
3607#else
3608 session->peer_cert_digest = NULL;
3609#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3610#endif /* MBEDTLS_X509_CRT_PARSE_C */
3611#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3612 session->ticket = NULL;
3613#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3614
3615 /*
3616 * Peer certificate
3617 */
3618#if defined(MBEDTLS_X509_CRT_PARSE_C)
3619#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3620 /* Deserialize CRT from the end of the ticket. */
3621 if (3 > (size_t) (end - p)) {
3622 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3623 }
3624
3625 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3626 p += 3;
3627
3628 if (cert_len != 0) {
3629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3630
3631 if (cert_len > (size_t) (end - p)) {
3632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3633 }
3634
3635 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3636
3637 if (session->peer_cert == NULL) {
3638 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3639 }
3640
3641 mbedtls_x509_crt_init(session->peer_cert);
3642
3643 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3644 p, cert_len)) != 0) {
3645 mbedtls_x509_crt_free(session->peer_cert);
3646 mbedtls_free(session->peer_cert);
3647 session->peer_cert = NULL;
3648 return ret;
3649 }
3650
3651 p += cert_len;
3652 }
3653#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3654 /* Deserialize CRT digest from the end of the ticket. */
3655 if (2 > (size_t) (end - p)) {
3656 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3657 }
3658
3659 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3660 session->peer_cert_digest_len = (size_t) *p++;
3661
3662 if (session->peer_cert_digest_len != 0) {
3663 const mbedtls_md_info_t *md_info =
3664 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3665 if (md_info == NULL) {
3666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3667 }
3668 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3670 }
3671
3672 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3674 }
3675
3676 session->peer_cert_digest =
3677 mbedtls_calloc(1, session->peer_cert_digest_len);
3678 if (session->peer_cert_digest == NULL) {
3679 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3680 }
3681
3682 memcpy(session->peer_cert_digest, p,
3683 session->peer_cert_digest_len);
3684 p += session->peer_cert_digest_len;
3685 }
3686#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3687#endif /* MBEDTLS_X509_CRT_PARSE_C */
3688
3689 /*
3690 * Session ticket and associated data
3691 */
3692#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3693#if defined(MBEDTLS_SSL_CLI_C)
3694 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3695 if (3 > (size_t) (end - p)) {
3696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3697 }
3698
3699 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3700 p += 3;
3701
3702 if (session->ticket_len != 0) {
3703 if (session->ticket_len > (size_t) (end - p)) {
3704 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3705 }
3706
3707 session->ticket = mbedtls_calloc(1, session->ticket_len);
3708 if (session->ticket == NULL) {
3709 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3710 }
3711
3712 memcpy(session->ticket, p, session->ticket_len);
3713 p += session->ticket_len;
3714 }
3715
3716 if (4 > (size_t) (end - p)) {
3717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3718 }
3719
3720 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3721 p += 4;
3722 }
3723#endif /* MBEDTLS_SSL_CLI_C */
3724#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3725 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3726 if (8 > (size_t) (end - p)) {
3727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3728 }
3729 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3730 p += 8;
3731 }
3732#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3733#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3734
3735 /*
3736 * Misc extension-related info
3737 */
3738#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3739 if (1 > (size_t) (end - p)) {
3740 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3741 }
3742
3743 session->mfl_code = *p++;
3744#endif
3745
3746#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3747 if (1 > (size_t) (end - p)) {
3748 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3749 }
3750
3751 session->encrypt_then_mac = *p++;
3752#endif
3753
3754 /* Done, should have consumed entire buffer */
3755 if (p != end) {
3756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3757 }
3758
3759 return 0;
3760}
3761
3762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3763
3764#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3765/* Serialization of TLS 1.3 sessions:
3766 *
David Horstmanncb01b362024-02-29 17:31:46 +00003767 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003768 */
3769#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3770MBEDTLS_CHECK_RETURN_CRITICAL
3771static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3772 unsigned char *buf,
3773 size_t buf_len,
3774 size_t *olen)
3775{
3776 unsigned char *p = buf;
3777#if defined(MBEDTLS_SSL_CLI_C) && \
3778 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3779 size_t hostname_len = (session->hostname == NULL) ?
3780 0 : strlen(session->hostname) + 1;
3781#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003782
3783#if defined(MBEDTLS_SSL_SRV_C) && \
3784 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003785 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003786 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003787#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003788 size_t needed = 4 /* ticket_age_add */
3789 + 1 /* ticket_flags */
3790 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003791
David Horstmanne59f9702024-02-29 16:08:57 +00003792 *olen = 0;
3793
3794 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3796 }
3797 needed += session->resumption_key_len; /* resumption_key */
3798
3799#if defined(MBEDTLS_SSL_EARLY_DATA)
3800 needed += 4; /* max_early_data_size */
3801#endif
3802#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3803 needed += 2; /* record_size_limit */
3804#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3805
3806#if defined(MBEDTLS_HAVE_TIME)
3807 needed += 8; /* ticket_creation_time or ticket_reception_time */
3808#endif
3809
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003810#if defined(MBEDTLS_SSL_SRV_C)
3811 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3812#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003813 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003814 + alpn_len; /* alpn */
3815#endif
3816 }
3817#endif /* MBEDTLS_SSL_SRV_C */
3818
David Horstmanne59f9702024-02-29 16:08:57 +00003819#if defined(MBEDTLS_SSL_CLI_C)
3820 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3821#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3822 needed += 2 /* hostname_len */
3823 + hostname_len; /* hostname */
3824#endif
3825
3826 needed += 4 /* ticket_lifetime */
3827 + 2; /* ticket_len */
3828
3829 /* Check size_t overflow */
3830 if (session->ticket_len > SIZE_MAX - needed) {
3831 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3832 }
3833
3834 needed += session->ticket_len; /* ticket */
3835 }
3836#endif /* MBEDTLS_SSL_CLI_C */
3837
3838 *olen = needed;
3839 if (needed > buf_len) {
3840 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3841 }
3842
3843 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3844 p[4] = session->ticket_flags;
3845
3846 /* save resumption_key */
3847 p[5] = session->resumption_key_len;
3848 p += 6;
3849 memcpy(p, session->resumption_key, session->resumption_key_len);
3850 p += session->resumption_key_len;
3851
3852#if defined(MBEDTLS_SSL_EARLY_DATA)
3853 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3854 p += 4;
3855#endif
3856#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3857 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3858 p += 2;
3859#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3860
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003861#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003862 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003863#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003864 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3865 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003866#endif /* MBEDTLS_HAVE_TIME */
3867
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003868#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003869 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3870 p += 2;
3871
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003872 if (alpn_len > 0) {
3873 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003874 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003875 p += alpn_len;
3876 }
3877#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3878 }
3879#endif /* MBEDTLS_SSL_SRV_C */
3880
David Horstmanne59f9702024-02-29 16:08:57 +00003881#if defined(MBEDTLS_SSL_CLI_C)
3882 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3883#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3884 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3885 p += 2;
3886 if (hostname_len > 0) {
3887 /* save host name */
3888 memcpy(p, session->hostname, hostname_len);
3889 p += hostname_len;
3890 }
3891#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3892
3893#if defined(MBEDTLS_HAVE_TIME)
3894 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3895 p += 8;
3896#endif
3897 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3898 p += 4;
3899
3900 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3901 p += 2;
3902
3903 if (session->ticket != NULL && session->ticket_len > 0) {
3904 memcpy(p, session->ticket, session->ticket_len);
3905 p += session->ticket_len;
3906 }
3907 }
3908#endif /* MBEDTLS_SSL_CLI_C */
3909 return 0;
3910}
3911
3912MBEDTLS_CHECK_RETURN_CRITICAL
3913static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3914 const unsigned char *buf,
3915 size_t len)
3916{
3917 const unsigned char *p = buf;
3918 const unsigned char *end = buf + len;
3919
3920 if (end - p < 6) {
3921 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3922 }
3923 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3924 session->ticket_flags = p[4];
3925
3926 /* load resumption_key */
3927 session->resumption_key_len = p[5];
3928 p += 6;
3929
3930 if (end - p < session->resumption_key_len) {
3931 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3932 }
3933
3934 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3935 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3936 }
3937 memcpy(session->resumption_key, p, session->resumption_key_len);
3938 p += session->resumption_key_len;
3939
3940#if defined(MBEDTLS_SSL_EARLY_DATA)
3941 if (end - p < 4) {
3942 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3943 }
3944 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3945 p += 4;
3946#endif
3947#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3948 if (end - p < 2) {
3949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3950 }
3951 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3952 p += 2;
3953#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3954
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003955#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003956 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003957#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003958 if (end - p < 8) {
3959 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3960 }
3961 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3962 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003963#endif /* MBEDTLS_HAVE_TIME */
3964
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003965#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003966 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003967
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003968 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3970 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003971
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003972 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3973 p += 2;
3974
3975 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3977 }
3978
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003979 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003980 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3981 if (ret != 0) {
3982 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003983 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003984 p += alpn_len;
3985 }
3986#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3987 }
3988#endif /* MBEDTLS_SSL_SRV_C */
3989
David Horstmanne59f9702024-02-29 16:08:57 +00003990#if defined(MBEDTLS_SSL_CLI_C)
3991 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3992#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3993 size_t hostname_len;
3994 /* load host name */
3995 if (end - p < 2) {
3996 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3997 }
3998 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3999 p += 2;
4000
4001 if (end - p < (long int) hostname_len) {
4002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4003 }
4004 if (hostname_len > 0) {
4005 session->hostname = mbedtls_calloc(1, hostname_len);
4006 if (session->hostname == NULL) {
4007 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4008 }
4009 memcpy(session->hostname, p, hostname_len);
4010 p += hostname_len;
4011 }
4012#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4013
4014#if defined(MBEDTLS_HAVE_TIME)
4015 if (end - p < 8) {
4016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4017 }
4018 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4019 p += 8;
4020#endif
4021 if (end - p < 4) {
4022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4023 }
4024 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4025 p += 4;
4026
4027 if (end - p < 2) {
4028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4029 }
4030 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4031 p += 2;
4032
4033 if (end - p < (long int) session->ticket_len) {
4034 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4035 }
4036 if (session->ticket_len > 0) {
4037 session->ticket = mbedtls_calloc(1, session->ticket_len);
4038 if (session->ticket == NULL) {
4039 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4040 }
4041 memcpy(session->ticket, p, session->ticket_len);
4042 p += session->ticket_len;
4043 }
4044 }
4045#endif /* MBEDTLS_SSL_CLI_C */
4046
4047 return 0;
4048
4049}
4050#else /* MBEDTLS_SSL_SESSION_TICKETS */
4051MBEDTLS_CHECK_RETURN_CRITICAL
4052static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4053 unsigned char *buf,
4054 size_t buf_len,
4055 size_t *olen)
4056{
4057 ((void) session);
4058 ((void) buf);
4059 ((void) buf_len);
4060 *olen = 0;
4061 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4062}
4063
4064static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritius93b2c322023-01-24 17:48:29 +01004065 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004066 size_t buf_len)
4067{
4068 ((void) session);
4069 ((void) buf);
4070 ((void) buf_len);
4071 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4072}
4073#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4074#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4075
Paul Bakker5121ce52009-01-03 21:22:43 +00004076/*
Hanno Beckera835da52019-05-16 12:39:07 +01004077 * Define ticket header determining Mbed TLS version
4078 * and structure of the ticket.
4079 */
4080
Hanno Becker94ef3b32019-05-16 12:50:45 +01004081/*
Hanno Becker50b59662019-05-28 14:30:45 +01004082 * Define bitflag determining compile-time settings influencing
4083 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004084 */
4085
Hanno Becker50b59662019-05-28 14:30:45 +01004086#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004087#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004088#else
Hanno Becker3e088662019-05-29 11:10:18 +01004089#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004090#endif /* MBEDTLS_HAVE_TIME */
4091
4092#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004093#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004094#else
Hanno Becker3e088662019-05-29 11:10:18 +01004095#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004096#endif /* MBEDTLS_X509_CRT_PARSE_C */
4097
David Horstmann5c5a32f2024-02-13 17:53:35 +00004098#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004099#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004100#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004101#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004102#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4103
Hanno Becker94ef3b32019-05-16 12:50:45 +01004104#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004105#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004106#else
Hanno Becker3e088662019-05-29 11:10:18 +01004107#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004108#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4109
4110#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004111#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004112#else
Hanno Becker3e088662019-05-29 11:10:18 +01004113#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004114#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4115
Hanno Becker94ef3b32019-05-16 12:50:45 +01004116#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004117#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004118#else
Hanno Becker3e088662019-05-29 11:10:18 +01004119#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004120#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4121
Hanno Becker94ef3b32019-05-16 12:50:45 +01004122#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4123#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4124#else
4125#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4126#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4127
David Horstmann92b258b2024-02-13 17:23:34 +00004128#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4129#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4130#else
4131#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4132#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4133
4134#if defined(MBEDTLS_SSL_EARLY_DATA)
4135#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4136#else
4137#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4138#endif /* MBEDTLS_SSL_EARLY_DATA */
4139
4140#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4141#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4142#else
4143#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4144#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4145
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004146#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4147 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004148#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4149#else
4150#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4151#endif /* MBEDTLS_SSL_ALPN */
4152
Hanno Becker3e088662019-05-29 11:10:18 +01004153#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4154#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4155#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4156#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004157#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4158#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004159#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004160#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4161#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4162#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004163#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004164
Hanno Becker50b59662019-05-28 14:30:45 +01004165#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 ((uint16_t) ( \
4167 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4168 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4169 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4170 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4171 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4172 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004173 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004174 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4175 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004176 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4177 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4178 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4179 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004180 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004181 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004182 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004183
Chien Wong4e9683e2023-12-28 17:07:43 +08004184static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004185 MBEDTLS_VERSION_MAJOR,
4186 MBEDTLS_VERSION_MINOR,
4187 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4189 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004190};
Hanno Beckera835da52019-05-16 12:39:07 +01004191
4192/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004193 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004194 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004195 *
David Horstmanncb01b362024-02-29 17:31:46 +00004196 * TLS 1.2 session:
4197 *
4198 * struct {
4199 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4200 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4201 * uint32 ticket_lifetime;
4202 * #endif
4203 * } ClientOnlyData;
4204 *
4205 * struct {
4206 * #if defined(MBEDTLS_HAVE_TIME)
4207 * uint64 start_time;
4208 * #endif
4209 * uint8 session_id_len; // at most 32
4210 * opaque session_id[32];
4211 * opaque master[48]; // fixed length in the standard
4212 * uint32 verify_result;
4213 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4214 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4215 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004216 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004217 * opaque peer_cert_digest<0..2^8-1>
4218 * #endif
4219 * select (endpoint) {
4220 * case client: ClientOnlyData;
4221 * case server: uint64 ticket_creation_time;
4222 * };
4223 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4224 * uint8 mfl_code; // up to 255 according to standard
4225 * #endif
4226 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4227 * uint8 encrypt_then_mac; // 0 or 1
4228 * #endif
4229 * } serialized_session_tls12;
4230 *
4231 *
4232 * TLS 1.3 Session:
4233 *
4234 * struct {
4235 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4236 * opaque hostname<0..2^16-1>;
4237 * #endif
4238 * #if defined(MBEDTLS_HAVE_TIME)
4239 * uint64 ticket_reception_time;
4240 * #endif
4241 * uint32 ticket_lifetime;
4242 * opaque ticket<1..2^16-1>;
4243 * } ClientOnlyData;
4244 *
4245 * struct {
4246 * uint32 ticket_age_add;
4247 * uint8 ticket_flags;
4248 * opaque resumption_key<0..255>;
4249 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4250 * uint32 max_early_data_size;
4251 * #endif
4252 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4253 * uint16 record_size_limit;
4254 * #endif
4255 * select ( endpoint ) {
4256 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004257 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004258 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004259 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004260 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004261 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004262 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004263 * #endif
4264 * };
4265 * } serialized_session_tls13;
4266 *
4267 *
4268 * SSL session:
4269 *
4270 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004271 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004272 * opaque mbedtls_version[3]; // library version: major, minor, patch
4273 * opaque session_format[2]; // library-version specific 16-bit field
4274 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004275 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004276 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004277 * Note: When updating the format, remember to keep
4278 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004279 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004280 * // In this version, `session_format` determines
4281 * // the setting of those compile-time
4282 * // configuration options which influence
4283 * // the structure of mbedtls_ssl_session.
4284 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004285 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004286 * // - TLS 1.2 (0x0303)
4287 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004288 * uint8_t endpoint;
4289 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004290 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004291 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004292 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004293 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004294 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004295 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004296 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004297 *
4298 * };
4299 *
4300 * } serialized_session;
4301 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004302 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004303
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004305static int ssl_session_save(const mbedtls_ssl_session *session,
4306 unsigned char omit_header,
4307 unsigned char *buf,
4308 size_t buf_len,
4309 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004310{
4311 unsigned char *p = buf;
4312 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004313 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004314#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4315 size_t out_len;
4316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4317#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (session == NULL) {
4319 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4320 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004321
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004323 /*
4324 * Add Mbed TLS version identifier
4325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (used <= buf_len) {
4329 memcpy(p, ssl_serialized_session_header,
4330 sizeof(ssl_serialized_session_header));
4331 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004332 }
4333 }
4334
4335 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004336 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004337 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004338 used += 1 /* TLS version */
4339 + 1 /* endpoint */
4340 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 if (used <= buf_len) {
4342 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004343 *p++ = session->endpoint;
4344 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4345 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004346 }
4347
4348 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004349 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004351#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004352 case MBEDTLS_SSL_VERSION_TLS1_2:
4353 used += ssl_tls12_session_save(session, p, remaining_len);
4354 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004355#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4356
Jerry Yu251a12e2022-07-13 15:15:48 +08004357#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 case MBEDTLS_SSL_VERSION_TLS1_3:
4359 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4360 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4361 return ret;
4362 }
4363 used += out_len;
4364 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004365#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 default:
4368 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004369 }
4370
4371 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (used > buf_len) {
4373 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4374 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004375
Gilles Peskine449bd832023-01-11 14:50:10 +01004376 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004377}
4378
4379/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004380 * Public wrapper for ssl_session_save()
4381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4383 unsigned char *buf,
4384 size_t buf_len,
4385 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004386{
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004388}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004389
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004390/*
4391 * Deserialize session, see mbedtls_ssl_session_save() for format.
4392 *
4393 * This internal version is wrapped by a public function that cleans up in
4394 * case of error, and has an extra option omit_header.
4395 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004397static int ssl_session_load(mbedtls_ssl_session *session,
4398 unsigned char omit_header,
4399 const unsigned char *buf,
4400 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004401{
4402 const unsigned char *p = buf;
4403 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004404 size_t remaining_len;
4405
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 if (session == NULL) {
4408 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4409 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004412 /*
4413 * Check Mbed TLS version identifier
4414 */
4415
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004418 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004419
4420 if (memcmp(p, ssl_serialized_session_header,
4421 sizeof(ssl_serialized_session_header)) != 0) {
4422 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4423 }
4424 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004425 }
4426
4427 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004428 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004429 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004430 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4432 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004433 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004434 session->endpoint = *p++;
4435 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4436 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004437
4438 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004439 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 case MBEDTLS_SSL_VERSION_TLS1_2:
4443 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004444#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4445
Jerry Yu438ddd82022-07-07 06:55:50 +00004446#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 case MBEDTLS_SSL_VERSION_TLS1_3:
4448 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004449#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 default:
4452 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004453 }
4454}
4455
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004456/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004457 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004459int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4460 const unsigned char *buf,
4461 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004462{
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 if (ret != 0) {
4466 mbedtls_ssl_session_free(session);
4467 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004470}
4471
4472/*
Paul Bakker1961b702013-01-25 14:49:24 +01004473 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004474 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004475MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004476static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004477{
4478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4479
Ronald Cron66dbf912022-02-02 15:33:46 +01004480 /*
4481 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004482 * that were written into the output buffer by the previous handshake step,
4483 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004484 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4485 * We proceed to the next handshake step only when all data from the
4486 * previous one have been sent to the peer, thus we make sure that this is
4487 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4488 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4489 * we have to wait before to go ahead.
4490 * In the case of TLS 1.3, handshake step handlers do not send data to the
4491 * peer. Data are only sent here and through
4492 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004493 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4496 return ret;
4497 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004498
4499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4501 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4502 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4503 return ret;
4504 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004505 }
4506#endif /* MBEDTLS_SSL_PROTO_DTLS */
4507
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004509}
4510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004512{
Hanno Becker41934dd2021-08-07 19:13:43 +01004513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004516 ssl->conf == NULL ||
4517 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4519 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004520 }
4521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 ret = ssl_prepare_handshake_step(ssl);
4523 if (ret != 0) {
4524 return ret;
4525 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004526
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 ret = mbedtls_ssl_handle_pending_alert(ssl);
4528 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004529 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 }
Jerry Yue7047812021-09-13 19:26:39 +08004531
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004532 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4533 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4534 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4538 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004539 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004542 case MBEDTLS_SSL_HELLO_REQUEST:
4543 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004544 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004545 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004546
Ronald Cron9f0fba32022-02-10 16:45:15 +01004547 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004549 break;
4550
4551 default:
4552#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4554 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4555 } else {
4556 ret = mbedtls_ssl_handshake_client_step(ssl);
4557 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004558#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004560#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004562#endif
4563 }
Jerry Yub9930e72021-08-06 17:11:51 +08004564 }
Ronald Cron6291b232023-03-08 15:51:25 +01004565#endif /* MBEDTLS_SSL_CLI_C */
4566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004569#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4570 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004572 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 ret = mbedtls_ssl_handshake_server_step(ssl);
4574 }
Ronald Cron6291b232023-03-08 15:51:25 +01004575#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4576 ret = mbedtls_ssl_handshake_server_step(ssl);
4577#else
4578 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004579#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004580 }
4581#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004584 /* handshake_step return error. And it is same
4585 * with alert_reason.
4586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if (ssl->send_alert) {
4588 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004589 goto cleanup;
4590 }
4591 }
4592
4593cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004595}
4596
4597/*
4598 * Perform the SSL handshake
4599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004601{
4602 int ret = 0;
4603
Hanno Beckera817ea42020-10-20 15:20:23 +01004604 /* Sanity checks */
4605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 if (ssl == NULL || ssl->conf == NULL) {
4607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4608 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004609
Hanno Beckera817ea42020-10-20 15:20:23 +01004610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4612 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4613 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4614 "mbedtls_ssl_set_timer_cb() for DTLS"));
4615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004616 }
4617#endif /* MBEDTLS_SSL_PROTO_DTLS */
4618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004620
Hanno Beckera817ea42020-10-20 15:20:23 +01004621 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4623 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004624
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004626 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 }
Paul Bakker1961b702013-01-25 14:49:24 +01004628 }
4629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004633}
4634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635#if defined(MBEDTLS_SSL_RENEGOTIATION)
4636#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004637/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004638 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004639 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004640MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004641static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004642{
Janos Follath865b3eb2019-12-16 11:46:15 +00004643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004644
Gilles Peskine449bd832023-01-11 14:50:10 +01004645 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004646
4647 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4649 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4652 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4653 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004654 }
4655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004661
4662/*
4663 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 * - any side: calling mbedtls_ssl_renegotiate(),
4665 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4666 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004667 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004668 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004670 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004671int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004672{
Janos Follath865b3eb2019-12-16 11:46:15 +00004673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004676
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 if ((ret = ssl_handshake_init(ssl)) != 0) {
4678 return ret;
4679 }
Paul Bakker48916f92012-09-16 19:57:18 +00004680
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004681 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4682 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4685 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4686 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004687 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004689 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004691 }
4692#endif
4693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4695 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004696
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4698 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4699 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004700 }
4701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004705}
4706
4707/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004708 * Renegotiate current connection on client,
4709 * or request renegotiation on server
4710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004711int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004714
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (ssl == NULL || ssl->conf == NULL) {
4716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4717 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004719#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004720 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004721 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4722 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4724 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004727
4728 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 if (ssl->out_left != 0) {
4730 return mbedtls_ssl_flush_output(ssl);
4731 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004735#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004737#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004738 /*
4739 * On client, either start the renegotiation process or,
4740 * if already in progress, continue the handshake
4741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4743 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004746
4747 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4748 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4749 return ret;
4750 }
4751 } else {
4752 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4753 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4754 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004755 }
4756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004762
Gilles Peskine449bd832023-01-11 14:50:10 +01004763void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004764{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004765 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4766
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004768 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770
Valerio Setti6f0441d2023-07-03 12:11:36 +02004771#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004772#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (ssl->handshake->group_list_heap_allocated) {
4774 mbedtls_free((void *) handshake->group_list);
4775 }
Brett Warrene0edc842021-08-17 09:53:13 +01004776 handshake->group_list = NULL;
4777#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004778#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004779
Ronald Crone68ab4f2022-10-05 12:46:29 +02004780#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004781#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (ssl->handshake->sig_algs_heap_allocated) {
4783 mbedtls_free((void *) handshake->sig_algs);
4784 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004785 handshake->sig_algs = NULL;
4786#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004787#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (ssl->handshake->certificate_request_context) {
4789 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004790 }
4791#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004792#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004793
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004794#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4796 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004797 handshake->async_in_progress = 0;
4798 }
4799#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4800
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004801#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004802#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004804#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004805 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004806#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004807#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004808#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004809#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004811#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004812 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004813#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004814#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004818#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004819#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004820 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004822#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004823
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004824#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004825#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004827 /*
4828 * Opaque keys are not stored in the handshake's data and it's the user
4829 * responsibility to destroy them. Clear ones, instead, are created by
4830 * the TLS library and should be destroyed at the same level
4831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4833 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004834 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004835 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4836#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004838#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004839#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004841 handshake->ecjpake_cache = NULL;
4842 handshake->ecjpake_cache_len = 0;
4843#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004844#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004845
Valerio Setti7aeec542023-07-05 18:57:21 +02004846#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004847 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004848 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004849 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004851#endif
4852
Ronald Cron73fe8df2022-10-05 14:31:43 +02004853#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004854#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004856 /* The maintenance of the external PSK key slot is the
4857 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 if (ssl->handshake->psk_opaque_is_internal) {
4859 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004860 ssl->handshake->psk_opaque_is_internal = 0;
4861 }
4862 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4863 }
Neil Armstronge952a302022-05-03 10:22:14 +02004864#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004866 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004867 }
Neil Armstronge952a302022-05-03 10:22:14 +02004868#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004869#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4872 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004873 /*
4874 * Free only the linked list wrapper, not the keys themselves
4875 * since the belong to the SNI callback
4876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004878#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004879
Gilles Peskineeccd8882020-03-10 12:19:08 +01004880#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4882 if (handshake->ecrs_peer_cert != NULL) {
4883 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4884 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004885 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004886#endif
4887
Hanno Becker75173122019-02-06 16:18:31 +00004888#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4889 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004891#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4892
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004893#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4895 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004896#endif /* MBEDTLS_SSL_CLI_C &&
4897 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004898
4899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 mbedtls_ssl_flight_free(handshake->flight);
4901 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004902#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004903
Valerio Settiea59c432023-07-25 11:14:03 +02004904#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004905 if (handshake->xxdh_psa_privkey_is_external == 0) {
4906 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 }
Valerio Settiea59c432023-07-25 11:14:03 +02004908#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004909
Ronald Cron6f135e12021-12-08 16:57:54 +01004910#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 mbedtls_ssl_transform_free(handshake->transform_handshake);
4912 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004913#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4915 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004916#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004917#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004918
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004919
4920#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4921 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4922 * processes datagrams and the fact that a datagram is allowed to have
4923 * several records in it, it is possible that the I/O buffers are not
4924 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4926 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004927#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004928
Jerry Yuba9c7272021-10-30 11:54:10 +08004929 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 mbedtls_platform_zeroize(handshake,
4931 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004932}
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004935{
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004937 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004942#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004943
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004944#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004945#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4946 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004948#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004950#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004951
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004952#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4953 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004954 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004955#endif
4956
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004958}
4959
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004960#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004961
4962#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4963#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4964#else
4965#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4966#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4967
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004968#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004969
4970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4971#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4972#else
4973#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4974#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4975
4976#if defined(MBEDTLS_SSL_ALPN)
4977#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4978#else
4979#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4980#endif /* MBEDTLS_SSL_ALPN */
4981
4982#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4983#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4984#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4985#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4986
4987#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 ((uint32_t) ( \
4989 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4990 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4991 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4992 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4993 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4994 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4995 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4996 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004997
Chien Wong4e9683e2023-12-28 17:07:43 +08004998static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004999 MBEDTLS_VERSION_MAJOR,
5000 MBEDTLS_VERSION_MINOR,
5001 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005002 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5003 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5004 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5005 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5006 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005007};
5008
Paul Bakker5121ce52009-01-03 21:22:43 +00005009/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005010 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005011 *
5012 * The format of the serialized data is:
5013 * (in the presentation language of TLS, RFC 8446 section 3)
5014 *
5015 * // header
5016 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005017 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005018 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005019 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005020 * Note: When updating the format, remember to keep these
5021 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005022 *
5023 * // session sub-structure
5024 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5025 * // transform sub-structure
5026 * uint8 random[64]; // ServerHello.random+ClientHello.random
5027 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5028 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5029 * // fields from ssl_context
5030 * uint32 badmac_seen; // DTLS: number of records with failing MAC
5031 * uint64 in_window_top; // DTLS: last validated record seq_num
5032 * uint64 in_window; // DTLS: bitmask for replay protection
5033 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5034 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5035 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5036 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5037 *
5038 * Note that many fields of the ssl_context or sub-structures are not
5039 * serialized, as they fall in one of the following categories:
5040 *
5041 * 1. forced value (eg in_left must be 0)
5042 * 2. pointer to dynamically-allocated memory (eg session, transform)
5043 * 3. value can be re-derived from other data (eg session keys from MS)
5044 * 4. value was temporary (eg content of input buffer)
5045 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005046 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5048 unsigned char *buf,
5049 size_t buf_len,
5050 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005051{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005052 unsigned char *p = buf;
5053 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005054 size_t session_len;
5055 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005056
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005057 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005058 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5059 * this function's documentation.
5060 *
5061 * These are due to assumptions/limitations in the implementation. Some of
5062 * them are likely to stay (no handshake in progress) some might go away
5063 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005064 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005065 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5067 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005069 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (ssl->handshake != NULL) {
5071 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5072 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005073 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005074 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (ssl->transform == NULL || ssl->session == NULL) {
5076 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5077 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005078 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005079 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (mbedtls_ssl_check_pending(ssl) != 0) {
5081 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005083 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 if (ssl->out_left != 0) {
5085 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005087 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005088 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5090 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005092 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005093 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5095 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005097 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005098 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5100 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5101 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005102 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005103 /* Renegotiation must not be enabled */
5104#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5106 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005108 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005109#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005110
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005111 /*
5112 * Version and format identifier
5113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005115
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if (used <= buf_len) {
5117 memcpy(p, ssl_serialized_context_header,
5118 sizeof(ssl_serialized_context_header));
5119 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005120 }
5121
5122 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005123 * Session (length + data)
5124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5126 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5127 return ret;
5128 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005129
5130 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 if (used <= buf_len) {
5132 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005133 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005134
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 ret = ssl_session_save(ssl->session, 1,
5136 p, session_len, &session_len);
5137 if (ret != 0) {
5138 return ret;
5139 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005140
5141 p += session_len;
5142 }
5143
5144 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005145 * Transform
5146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 used += sizeof(ssl->transform->randbytes);
5148 if (used <= buf_len) {
5149 memcpy(p, ssl->transform->randbytes,
5150 sizeof(ssl->transform->randbytes));
5151 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005152 }
5153
5154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005155 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005157 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005159 p += ssl->transform->in_cid_len;
5160
5161 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005163 p += ssl->transform->out_cid_len;
5164 }
5165#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5166
5167 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005168 * Saved fields from top-level ssl_context structure
5169 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005170 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (used <= buf_len) {
5172 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005173 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005174 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005175
5176#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5177 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 if (used <= buf_len) {
5179 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005180 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005181
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005183 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005184 }
5185#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5186
5187#if defined(MBEDTLS_SSL_PROTO_DTLS)
5188 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005190 *p++ = ssl->disable_datagram_packing;
5191 }
5192#endif /* MBEDTLS_SSL_PROTO_DTLS */
5193
Jerry Yuae0b2e22021-10-08 15:21:19 +08005194 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 if (used <= buf_len) {
5196 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005197 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005198 }
5199
5200#if defined(MBEDTLS_SSL_PROTO_DTLS)
5201 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (used <= buf_len) {
5203 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005204 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005205 }
5206#endif /* MBEDTLS_SSL_PROTO_DTLS */
5207
5208#if defined(MBEDTLS_SSL_ALPN)
5209 {
5210 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005212 : 0;
5213
5214 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005216 *p++ = alpn_len;
5217
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (ssl->alpn_chosen != NULL) {
5219 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005220 p += alpn_len;
5221 }
5222 }
5223 }
5224#endif /* MBEDTLS_SSL_ALPN */
5225
5226 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005227 * Done
5228 */
5229 *olen = used;
5230
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 if (used > buf_len) {
5232 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5233 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005238}
5239
5240/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005241 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005242 *
5243 * This internal version is wrapped by a public function that cleans up in
5244 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005245 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005246MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005247static int ssl_context_load(mbedtls_ssl_context *ssl,
5248 const unsigned char *buf,
5249 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005250{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005251 const unsigned char *p = buf;
5252 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005253 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005255#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005256 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005257#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005258
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005259 /*
5260 * The context should have been freshly setup or reset.
5261 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005262 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005263 * renegotiating, or if the user mistakenly loaded a session first.)
5264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5266 ssl->session != NULL) {
5267 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005268 }
5269
5270 /*
5271 * We can't check that the config matches the initial one, but we can at
5272 * least check it matches the requirements for serializing.
5273 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005274 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005275#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005276 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005277#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005278 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5279 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5280 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5281 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005286
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005287 /*
5288 * Check version identifier
5289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5291 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005292 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005293
5294 if (memcmp(p, ssl_serialized_context_header,
5295 sizeof(ssl_serialized_context_header)) != 0) {
5296 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5297 }
5298 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005299
5300 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005301 * Session
5302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if ((size_t) (end - p) < 4) {
5304 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5305 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005306
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005307 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005308 p += 4;
5309
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005310 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005311 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005312 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005313 ssl->session_in = ssl->session;
5314 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005315 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005316
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 if ((size_t) (end - p) < session_len) {
5318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5319 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005320
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 ret = ssl_session_load(ssl->session, 1, p, session_len);
5322 if (ret != 0) {
5323 mbedtls_ssl_session_free(ssl->session);
5324 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005325 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005326
5327 p += session_len;
5328
5329 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005330 * Transform
5331 */
5332
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005333 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005334 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005335#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005336 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005337 ssl->transform_in = ssl->transform;
5338 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005339 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005340#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005341
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005342#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5344 if (prf_func == NULL) {
5345 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5346 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005347
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005348 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5350 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5351 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005352
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 ret = ssl_tls12_populate_transform(ssl->transform,
5354 ssl->session->ciphersuite,
5355 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005356#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005358#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 prf_func,
5360 p, /* currently pointing to randbytes */
5361 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5362 ssl->conf->endpoint,
5363 ssl);
5364 if (ret != 0) {
5365 return ret;
5366 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005367#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005369
5370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5371 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 if ((size_t) (end - p) < 1) {
5373 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5374 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005375
5376 ssl->transform->in_cid_len = *p++;
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5379 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5380 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005381
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005383 p += ssl->transform->in_cid_len;
5384
5385 ssl->transform->out_cid_len = *p++;
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5388 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5389 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005390
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005392 p += ssl->transform->out_cid_len;
5393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5394
5395 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005396 * Saved fields from top-level ssl_context structure
5397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 if ((size_t) (end - p) < 4) {
5399 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5400 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005401
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005402 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005403 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005404
5405#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 if ((size_t) (end - p) < 16) {
5407 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5408 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005409
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005410 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005411 p += 8;
5412
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005413 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005414 p += 8;
5415#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5416
5417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 if ((size_t) (end - p) < 1) {
5419 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5420 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005421
5422 ssl->disable_datagram_packing = *p++;
5423#endif /* MBEDTLS_SSL_PROTO_DTLS */
5424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5426 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5427 }
5428 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5429 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005430
5431#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 if ((size_t) (end - p) < 2) {
5433 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5434 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005435
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005436 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005437 p += 2;
5438#endif /* MBEDTLS_SSL_PROTO_DTLS */
5439
5440#if defined(MBEDTLS_SSL_ALPN)
5441 {
5442 uint8_t alpn_len;
5443 const char **cur;
5444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 if ((size_t) (end - p) < 1) {
5446 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5447 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005448
5449 alpn_len = *p++;
5450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005452 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5454 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005455 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005456 ssl->alpn_chosen = *cur;
5457 break;
5458 }
5459 }
5460 }
5461
5462 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5464 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5465 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005466
5467 p += alpn_len;
5468 }
5469#endif /* MBEDTLS_SSL_ALPN */
5470
5471 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005472 * Forced fields from top-level ssl_context structure
5473 *
5474 * Most of them already set to the correct value by mbedtls_ssl_init() and
5475 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5476 */
5477 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005478 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005479
Hanno Becker361b10d2019-08-30 10:42:49 +01005480 /* Adjust pointers for header fields of outgoing records to
5481 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005483
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005484#if defined(MBEDTLS_SSL_PROTO_DTLS)
5485 ssl->in_epoch = 1;
5486#endif
5487
5488 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5489 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005490 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5491 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 if (ssl->handshake != NULL) {
5493 mbedtls_ssl_handshake_free(ssl);
5494 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005495 ssl->handshake = NULL;
5496 }
5497
5498 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005499 * Done - should have consumed entire buffer
5500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 if (p != end) {
5502 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5503 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005506}
5507
5508/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005509 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005511int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5512 const unsigned char *buf,
5513 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005514{
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 if (ret != 0) {
5518 mbedtls_ssl_free(context);
5519 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005522}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005523#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005524
5525/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 * Free an SSL context
5527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005528void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005529{
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005531 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005533
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005537#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5538 size_t out_buf_len = ssl->out_buf_len;
5539#else
5540 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5541#endif
5542
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005543 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005544 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005545 }
5546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005548#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5549 size_t in_buf_len = ssl->in_buf_len;
5550#else
5551 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5552#endif
5553
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005554 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005555 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 }
5557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 if (ssl->transform) {
5559 mbedtls_ssl_transform_free(ssl->transform);
5560 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005561 }
5562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 if (ssl->handshake) {
5564 mbedtls_ssl_handshake_free(ssl);
5565 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005566
5567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5569 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005570#endif
5571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 mbedtls_ssl_session_free(ssl->session_negotiate);
5573 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005574 }
5575
Ronald Cron6f135e12021-12-08 16:57:54 +01005576#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 mbedtls_ssl_transform_free(ssl->transform_application);
5578 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005579#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 if (ssl->session) {
5582 mbedtls_ssl_session_free(ssl->session);
5583 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005584 }
5585
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005586#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005588 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005591
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005592#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005594#endif
5595
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005597
Paul Bakker86f04f42013-02-14 11:20:09 +01005598 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005600}
5601
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005602/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005603 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005605void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005606{
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005608}
5609
Gilles Peskineae270bf2021-06-02 00:05:29 +02005610/* The selection should be the same as mbedtls_x509_crt_profile_default in
5611 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005612 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005613 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005614 * about this list.
5615 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005616static const uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005617#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01005618 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005619#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005620#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005621 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005622#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005623#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005624 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005625#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005626#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01005627 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005628#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005629#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005630 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005631#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005632#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005633 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005634#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005635#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005636 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005637#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005638#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005639 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005640#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005641#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005642 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5643 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5644 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5645 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5646 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5647#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005648 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005649};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005650
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005651static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005652 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5653 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5654 0
5655};
5656
Ronald Crone68ab4f2022-10-05 12:46:29 +02005657#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005658
Jerry Yu909df7b2022-01-22 11:56:27 +08005659/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005660 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005661 * rules SHOULD be upheld.
5662 * - No duplicate entries.
5663 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005664 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005665 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005666 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005667static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005668
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005669#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005670 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005671 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005672 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005673 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5674#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005675
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005676#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005677 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005678 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005679 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005680 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5681#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005682
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005683#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005684 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005685 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005686 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005687 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5688#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005689
Yanray Wang1136fad2023-11-22 16:54:31 +08005690#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005691 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005692#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005693
Yanray Wang1136fad2023-11-22 16:54:31 +08005694#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005695 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005696#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005697
Yanray Wang1136fad2023-11-22 16:54:31 +08005698#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005699 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005700#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005701
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005702#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005703 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005704#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005705
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005706#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005707 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005708#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005709
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005710#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005711 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005712#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005713
Gabor Mezei15b95a62022-05-09 16:37:58 +02005714 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005715};
5716
5717/* NOTICE: see above */
5718#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5719static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005720
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005721#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005722#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005724#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005725#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5726 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005727#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005728#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005730#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005731#endif /* MBEDTLS_MD_CAN_SHA512 */
5732
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005733#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005734#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005736#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005737#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5738 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005739#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005740#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005742#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005743#endif /* MBEDTLS_MD_CAN_SHA384 */
5744
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005745#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005746#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005748#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005749#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5750 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005751#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005752#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005754#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005755#endif /* MBEDTLS_MD_CAN_SHA256 */
5756
Gabor Mezei15b95a62022-05-09 16:37:58 +02005757 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005758};
5759#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005760
Jerry Yu909df7b2022-01-22 11:56:27 +08005761/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005762static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005763
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005764#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005765 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005766 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005767 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005768 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5769#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005770
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005771#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005772 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005773 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005774 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005775 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5776#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005777
Gabor Mezei15b95a62022-05-09 16:37:58 +02005778 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005779};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005780
Jerry Yu909df7b2022-01-22 11:56:27 +08005781/* NOTICE: see above */
5782#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5783static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005784
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005785#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005786#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005788#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005789#endif /* MBEDTLS_MD_CAN_SHA256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005790
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005791#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005792#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005794#endif
Yanray Wang1136fad2023-11-22 16:54:31 +08005795#endif /* MBEDTLS_MD_CAN_SHA384 */
5796
Gabor Mezei15b95a62022-05-09 16:37:58 +02005797 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005798};
Jerry Yu909df7b2022-01-22 11:56:27 +08005799#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5800
Ronald Crone68ab4f2022-10-05 12:46:29 +02005801#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005802
Chien Wong4e9683e2023-12-28 17:07:43 +08005803static const uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005804#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005805 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005806#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005807#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005808 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005809#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005810 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005811};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005812
Ronald Crone68ab4f2022-10-05 12:46:29 +02005813#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005814/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005815 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005816MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005817static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005818{
5819 size_t i, j;
5820 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005821
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5823 for (j = 0; j < i; j++) {
5824 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005825 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 }
5827 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5828 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5829 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005830 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005831 }
5832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005834}
5835
Ronald Crone68ab4f2022-10-05 12:46:29 +02005836#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005837
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005838/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005839 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005841int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5842 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005843{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005844#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005846#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005847
Ronald Crone68ab4f2022-10-05 12:46:29 +02005848#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5850 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5851 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005852 }
5853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5855 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5856 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005857 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005858
5859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5861 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5862 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005863 }
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5866 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5867 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005868 }
5869#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005870#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005871
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005872 /* Use the functions here so that they are covered in tests,
5873 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 mbedtls_ssl_conf_endpoint(conf, endpoint);
5875 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005876
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005877 /*
5878 * Things that are common to all presets
5879 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005880#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005882 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5883#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Crond67f8012024-08-28 07:45:57 +02005884 mbedtls_ssl_conf_session_tickets(conf, MBEDTLS_SSL_SESSION_TICKETS_ENABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005885#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cronc46edd42024-08-28 16:54:42 +02005886 /* Contrary to TLS 1.2 tickets, TLS 1.3 NewSessionTicket message
5887 * handling is disabled by default in Mbed TLS 3.6.x for backward
5888 * compatibility with client applications developed using Mbed TLS 3.5
5889 * or earlier with the default configuration.
5890 *
5891 * Up to Mbed TLS 3.5, in the default configuration TLS 1.3 was
5892 * disabled, and a Mbed TLS client with the default configuration would
5893 * establish a TLS 1.2 connection with a TLS 1.2 and TLS 1.3 capable
5894 * server.
5895 *
5896 * Starting with Mbed TLS 3.6.0, TLS 1.3 is enabled by default, and thus
5897 * an Mbed TLS client with the default configuration establishes a
5898 * TLS 1.3 connection with a TLS 1.2 and TLS 1.3 capable server. If
5899 * following the handshake the TLS 1.3 server sends NewSessionTicket
5900 * messages and the Mbed TLS client processes them, this results in
5901 * Mbed TLS high level APIs (mbedtls_ssl_read(),
5902 * mbedtls_ssl_handshake(), ...) to eventually return an
5903 * #MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET non fatal error code
5904 * (see the documentation of mbedtls_ssl_read() for more information on
5905 * that error code). Applications unaware of that TLS 1.3 specific non
5906 * fatal error code are then failing.
5907 */
Ronald Cron9f44c882024-08-28 16:44:10 +02005908 mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(
5909 conf, MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_DISABLED);
Ronald Cronbedddd72024-08-27 14:18:50 +02005910#endif
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005911#endif
5912 }
5913#endif
5914
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5916 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5917#endif
5918
5919#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5920 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5921#endif
5922
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005923#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005924 conf->f_cookie_write = ssl_cookie_write_dummy;
5925 conf->f_cookie_check = ssl_cookie_check_dummy;
5926#endif
5927
5928#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5929 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5930#endif
5931
Janos Follath088ce432017-04-10 12:42:31 +01005932#if defined(MBEDTLS_SSL_SRV_C)
5933 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005934 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005935#endif
5936
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005937#if defined(MBEDTLS_SSL_PROTO_DTLS)
5938 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5939 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5940#endif
5941
5942#if defined(MBEDTLS_SSL_RENEGOTIATION)
5943 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 memset(conf->renego_period, 0x00, 2);
5945 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005946#endif
5947
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005948#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005950 const unsigned char dhm_p[] =
5951 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5952 const unsigned char dhm_g[] =
5953 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5956 dhm_p, sizeof(dhm_p),
5957 dhm_g, sizeof(dhm_g))) != 0) {
5958 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005959 }
5960 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005961#endif
5962
Ronald Cron6f135e12021-12-08 16:57:54 +01005963#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005964
5965#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005966 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005967#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005968 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005969#endif
5970#endif /* MBEDTLS_SSL_EARLY_DATA */
5971
Jerry Yud0766ec2022-09-22 10:46:57 +08005972#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005973 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005975#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005976 /*
5977 * Allow all TLS 1.3 key exchange modes by default.
5978 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005979 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005980#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005984 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005985 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005986#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005988#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005990#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005991 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5992 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005993#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5994 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5995 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5996#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5997 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5998 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5999#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00006001#endif
6002 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04006003
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006004 /*
6005 * Preset-specific defaults
6006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006008 /*
6009 * NSA Suite B
6010 */
6011 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006012
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006013 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006014
6015#if defined(MBEDTLS_X509_CRT_PARSE_C)
6016 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006017#endif
6018
Ronald Crone68ab4f2022-10-05 12:46:29 +02006019#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006022 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006024#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006026#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006027
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006028#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006029 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006030#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006031 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006032 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006033
6034 /*
6035 * Default
6036 */
6037 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006038
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006039 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006040
6041#if defined(MBEDTLS_X509_CRT_PARSE_C)
6042 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6043#endif
6044
Ronald Crone68ab4f2022-10-05 12:46:29 +02006045#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006046#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006048 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006050#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006052#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006053
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006054#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006055 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006056#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006057 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006058
6059#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6060 conf->dhm_min_bitlen = 1024;
6061#endif
6062 }
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006065}
6066
6067/*
6068 * Free mbedtls_ssl_config
6069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006070void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006071{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006072 if (conf == NULL) {
6073 return;
6074 }
6075
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006076#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 mbedtls_mpi_free(&conf->dhm_P);
6078 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006079#endif
6080
Ronald Cron73fe8df2022-10-05 14:31:43 +02006081#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006084 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6085 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006086#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006088 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006089 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006090 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006091 }
6092
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006094 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006095 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006096 conf->psk_identity_len = 0;
6097 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006098#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006099
6100#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006102#endif
6103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006105}
6106
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006107#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006108 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006109/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006112unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6116 return MBEDTLS_SSL_SIG_RSA;
6117 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006118#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006119#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6121 return MBEDTLS_SSL_SIG_ECDSA;
6122 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006123#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006125}
6126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006128{
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006130 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006132 case MBEDTLS_PK_ECDSA:
6133 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006135 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006137 }
6138}
6139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006141{
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143#if defined(MBEDTLS_RSA_C)
6144 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006146#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006147#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006150#endif
6151 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006153 }
6154}
Valerio Settie9646ec2023-08-02 20:02:28 +02006155#endif /* MBEDTLS_PK_C &&
6156 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006157
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006158/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006159 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006162{
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006164#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006167#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006168#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006169 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006171#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006172#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006175#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006176#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006179#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006180#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006183#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006184#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006185 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006187#endif
6188 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006190 }
6191}
6192
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006193/*
6194 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006196unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006197{
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006199#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006200 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006202#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006203#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006204 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006206#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006207#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006208 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006210#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006211#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006212 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006214#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006215#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006216 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006218#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006219#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006220 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006222#endif
6223 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006225 }
6226}
6227
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006228/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006229 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006230 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006232int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006233{
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if (group_list == NULL) {
6237 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006238 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 for (; *group_list != 0; group_list++) {
6241 if (*group_list == tls_id) {
6242 return 0;
6243 }
6244 }
6245
6246 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006247}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006248
Valerio Setti49e69072023-06-27 17:27:51 +02006249#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006250/*
6251 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006253int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006254{
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006258 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006262}
Valerio Setti49e69072023-06-27 17:27:51 +02006263#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01006264
Valerio Setti18c9fed2022-12-30 17:44:24 +01006265static const struct {
6266 uint16_t tls_id;
6267 mbedtls_ecp_group_id ecp_group_id;
6268 psa_ecc_family_t psa_family;
6269 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006270} tls_id_match_table[] =
6271{
Valerio Settidb6b4db2023-09-01 09:20:51 +02006272#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006273 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006274#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006275#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006276 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006277#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006278#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006279 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006280#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006281#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006282 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006283#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006284#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006285 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006286#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006287#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006288 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006289#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006290#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006291 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006292#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006293#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006294 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006295#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006296#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006297 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006298#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006299#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006300 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006301#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006302#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02006303 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006304#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006305#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02006306 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006307#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02006308#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006309 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006310#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006311 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006312};
6313
Gilles Peskine449bd832023-01-11 14:50:10 +01006314int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006315 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006317{
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6319 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006320 if (type != NULL) {
6321 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 }
6323 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006324 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006326 return PSA_SUCCESS;
6327 }
6328 }
6329
6330 return PSA_ERROR_NOT_SUPPORTED;
6331}
6332
Gilles Peskine449bd832023-01-11 14:50:10 +01006333mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006334{
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6336 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006337 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006339 }
6340
6341 return MBEDTLS_ECP_DP_NONE;
6342}
6343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006345{
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6347 i++) {
6348 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006349 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006351 }
6352
6353 return 0;
6354}
6355
Valerio Setti67419f02023-01-04 16:12:42 +01006356#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006357static const struct {
6358 uint16_t tls_id;
6359 const char *name;
6360} tls_id_curve_name_table[] =
6361{
Valerio Setti54e23792023-07-07 10:49:27 +02006362 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6363 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6364 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6365 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6366 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6367 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6368 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6369 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6370 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6371 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6372 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6373 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6374 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006375 { 0, NULL },
6376};
6377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006379{
Valerio Settiacd32c02023-06-29 18:06:29 +02006380 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6381 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6382 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006384 }
6385
6386 return NULL;
6387}
Valerio Setti67419f02023-01-04 16:12:42 +01006388#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006389
Jerry Yu148165c2021-09-24 23:20:59 +08006390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006391int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6392 const mbedtls_md_type_t md,
6393 unsigned char *dst,
6394 size_t dst_len,
6395 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006396{
Ronald Cronf6893e12022-01-07 22:09:01 +01006397 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6398 psa_hash_operation_t *hash_operation_to_clone;
6399 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6400
Jerry Yu148165c2021-09-24 23:20:59 +08006401 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006402
Gilles Peskine449bd832023-01-11 14:50:10 +01006403 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006404#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 case MBEDTLS_MD_SHA384:
6406 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6407 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006408#endif
6409
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006410#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 case MBEDTLS_MD_SHA256:
6412 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6413 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006414#endif
6415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 default:
6417 goto exit;
6418 }
6419
6420 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6421 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006422 goto exit;
6423 }
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6426 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006427 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006429
6430exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006431#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6432 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006433 (void) ssl;
6434#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006435 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006436}
6437#else /* MBEDTLS_USE_PSA_CRYPTO */
6438
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006439#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006440MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006441static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6442 unsigned char *dst,
6443 size_t dst_len,
6444 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006445{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006446 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006447 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (dst_len < 48) {
6450 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6451 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006452
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006453 mbedtls_md_init(&sha384);
6454 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006455 if (ret != 0) {
6456 goto exit;
6457 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006458 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006459 if (ret != 0) {
6460 goto exit;
6461 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006462
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006463 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006464 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006465 goto exit;
6466 }
6467
6468 *olen = 48;
6469
6470exit:
6471
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006472 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006474}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006475#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006476
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006477#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006479static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6480 unsigned char *dst,
6481 size_t dst_len,
6482 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006483{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006484 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006485 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 if (dst_len < 32) {
6488 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6489 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006490
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006491 mbedtls_md_init(&sha256);
6492 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6493 if (ret != 0) {
6494 goto exit;
6495 }
6496 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6497 if (ret != 0) {
6498 goto exit;
6499 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006500
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006501 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6502 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006503 goto exit;
6504 }
6505
6506 *olen = 32;
6507
6508exit:
6509
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006510 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006512}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006513#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6516 const mbedtls_md_type_t md,
6517 unsigned char *dst,
6518 size_t dst_len,
6519 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006520{
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006522
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006523#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 case MBEDTLS_MD_SHA384:
6525 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006526#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006527
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006528#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 case MBEDTLS_MD_SHA256:
6530 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006531#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006532
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006534#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6535 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 (void) ssl;
6537 (void) dst;
6538 (void) dst_len;
6539 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006540#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006542 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006544}
XiaokangQian647719a2021-12-07 09:16:29 +00006545
Jerry Yu148165c2021-09-24 23:20:59 +08006546#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006547
Ronald Crone68ab4f2022-10-05 12:46:29 +02006548#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006549/* mbedtls_ssl_parse_sig_alg_ext()
6550 *
6551 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6552 * value (TLS 1.3 RFC8446):
6553 * enum {
6554 * ....
6555 * ecdsa_secp256r1_sha256( 0x0403 ),
6556 * ecdsa_secp384r1_sha384( 0x0503 ),
6557 * ecdsa_secp521r1_sha512( 0x0603 ),
6558 * ....
6559 * } SignatureScheme;
6560 *
6561 * struct {
6562 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6563 * } SignatureSchemeList;
6564 *
6565 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6566 * value (TLS 1.2 RFC5246):
6567 * enum {
6568 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6569 * sha512(6), (255)
6570 * } HashAlgorithm;
6571 *
6572 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6573 * SignatureAlgorithm;
6574 *
6575 * struct {
6576 * HashAlgorithm hash;
6577 * SignatureAlgorithm signature;
6578 * } SignatureAndHashAlgorithm;
6579 *
6580 * SignatureAndHashAlgorithm
6581 * supported_signature_algorithms<2..2^16-2>;
6582 *
6583 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6584 * generalization of the TLS 1.2 signature algorithm extension.
6585 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6586 * `SignatureScheme` field of TLS 1.3
6587 *
6588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006589int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6590 const unsigned char *buf,
6591 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006592{
6593 const unsigned char *p = buf;
6594 size_t supported_sig_algs_len = 0;
6595 const unsigned char *supported_sig_algs_end;
6596 uint16_t sig_alg;
6597 uint32_t common_idx = 0;
6598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6600 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006601 p += 2;
6602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 memset(ssl->handshake->received_sig_algs, 0,
6604 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006607 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 while (p < supported_sig_algs_end) {
6609 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6610 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006611 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006612 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6613 sig_alg,
6614 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006615#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6617 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6618 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006619 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006620 }
6621#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6624 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006625
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006627 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6628 common_idx += 1;
6629 }
6630 }
6631 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 if (p != end) {
6633 MBEDTLS_SSL_DEBUG_MSG(1,
6634 ("Signature algorithms extension length misaligned"));
6635 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6636 MBEDTLS_ERR_SSL_DECODE_ERROR);
6637 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006638 }
6639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 if (common_idx == 0) {
6641 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6642 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6643 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6644 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006645 }
6646
Gabor Mezei15b95a62022-05-09 16:37:58 +02006647 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006649}
6650
Ronald Crone68ab4f2022-10-05 12:46:29 +02006651#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006652
Jerry Yudc7bd172022-02-17 13:44:15 +08006653#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6654
6655#if defined(MBEDTLS_USE_PSA_CRYPTO)
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6658 mbedtls_svc_key_id_t key,
6659 psa_algorithm_t alg,
6660 const unsigned char *raw_psk, size_t raw_psk_length,
6661 const unsigned char *seed, size_t seed_length,
6662 const unsigned char *label, size_t label_length,
6663 const unsigned char *other_secret,
6664 size_t other_secret_length,
6665 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006666{
6667 psa_status_t status;
6668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 status = psa_key_derivation_setup(derivation, alg);
6670 if (status != PSA_SUCCESS) {
6671 return status;
6672 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6675 status = psa_key_derivation_input_bytes(derivation,
6676 PSA_KEY_DERIVATION_INPUT_SEED,
6677 seed, seed_length);
6678 if (status != PSA_SUCCESS) {
6679 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006680 }
6681
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 if (other_secret != NULL) {
6683 status = psa_key_derivation_input_bytes(derivation,
6684 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6685 other_secret, other_secret_length);
6686 if (status != PSA_SUCCESS) {
6687 return status;
6688 }
6689 }
6690
6691 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006692 status = psa_key_derivation_input_bytes(
6693 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 raw_psk, raw_psk_length);
6695 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006696 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (status != PSA_SUCCESS) {
6700 return status;
6701 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006702
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 status = psa_key_derivation_input_bytes(derivation,
6704 PSA_KEY_DERIVATION_INPUT_LABEL,
6705 label, label_length);
6706 if (status != PSA_SUCCESS) {
6707 return status;
6708 }
6709 } else {
6710 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006711 }
6712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 status = psa_key_derivation_set_capacity(derivation, capacity);
6714 if (status != PSA_SUCCESS) {
6715 return status;
6716 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006719}
6720
Andrzej Kurek57d10632022-10-24 10:32:01 -04006721#if defined(PSA_WANT_ALG_SHA_384) || \
6722 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006723MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006724static int tls_prf_generic(mbedtls_md_type_t md_type,
6725 const unsigned char *secret, size_t slen,
6726 const char *label,
6727 const unsigned char *random, size_t rlen,
6728 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006729{
6730 psa_status_t status;
6731 psa_algorithm_t alg;
6732 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6733 psa_key_derivation_operation_t derivation =
6734 PSA_KEY_DERIVATION_OPERATION_INIT;
6735
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006737 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006739 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006741
6742 /* Normally a "secret" should be long enough to be impossible to
6743 * find by brute force, and in particular should not be empty. But
6744 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6745 * and for this use case it makes sense to have a 0-length "secret".
6746 * Since the key API doesn't allow importing a key of length 0,
6747 * keep master_key=0, which setup_psa_key_derivation() understands
6748 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006750 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6752 psa_set_key_algorithm(&key_attributes, alg);
6753 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6756 if (status != PSA_SUCCESS) {
6757 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6758 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006759 }
6760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 status = setup_psa_key_derivation(&derivation,
6762 master_key, alg,
6763 NULL, 0,
6764 random, rlen,
6765 (unsigned char const *) label,
6766 (size_t) strlen(label),
6767 NULL, 0,
6768 dlen);
6769 if (status != PSA_SUCCESS) {
6770 psa_key_derivation_abort(&derivation);
6771 psa_destroy_key(master_key);
6772 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006773 }
6774
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6776 if (status != PSA_SUCCESS) {
6777 psa_key_derivation_abort(&derivation);
6778 psa_destroy_key(master_key);
6779 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006780 }
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 status = psa_key_derivation_abort(&derivation);
6783 if (status != PSA_SUCCESS) {
6784 psa_destroy_key(master_key);
6785 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006786 }
6787
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 if (!mbedtls_svc_key_id_is_null(master_key)) {
6789 status = psa_destroy_key(master_key);
6790 }
6791 if (status != PSA_SUCCESS) {
6792 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6793 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006796}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006797#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006798#else /* MBEDTLS_USE_PSA_CRYPTO */
6799
Andrzej Kurek57d10632022-10-24 10:32:01 -04006800#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006801 (defined(MBEDTLS_MD_CAN_SHA256) || \
6802 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006803MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006804static int tls_prf_generic(mbedtls_md_type_t md_type,
6805 const unsigned char *secret, size_t slen,
6806 const char *label,
6807 const unsigned char *random, size_t rlen,
6808 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006809{
6810 size_t nb;
6811 size_t i, j, k, md_len;
6812 unsigned char *tmp;
6813 size_t tmp_len = 0;
6814 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6815 const mbedtls_md_info_t *md_info;
6816 mbedtls_md_context_t md_ctx;
6817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6822 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6823 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 tmp_len = md_len + strlen(label) + rlen;
6828 tmp = mbedtls_calloc(1, tmp_len);
6829 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006830 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6831 goto exit;
6832 }
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 nb = strlen(label);
6835 memcpy(tmp + md_len, label, nb);
6836 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006837 nb += rlen;
6838
6839 /*
6840 * Compute P_<hash>(secret, label + random)[0..dlen]
6841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006843 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6847 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006848 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 }
6850 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6851 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 }
6854 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6855 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006856 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 for (i = 0; i < dlen; i += md_len) {
6860 ret = mbedtls_md_hmac_reset(&md_ctx);
6861 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006862 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 }
6864 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6865 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006866 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 }
6868 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6869 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006870 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 ret = mbedtls_md_hmac_reset(&md_ctx);
6874 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006875 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 }
6877 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6878 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006879 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 }
6881 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6882 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006883 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006884 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006885
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006887
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006889 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006891 }
6892
6893exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (tmp != NULL) {
6897 mbedtls_platform_zeroize(tmp, tmp_len);
6898 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006905}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006906#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006907#endif /* MBEDTLS_USE_PSA_CRYPTO */
6908
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006909#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006911static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6912 const char *label,
6913 const unsigned char *random, size_t rlen,
6914 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006915{
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6917 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006918}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006919#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006920
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006921#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006922MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006923static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6924 const char *label,
6925 const unsigned char *random, size_t rlen,
6926 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006927{
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6929 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006930}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006931#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006932
Jerry Yuf009d862022-02-17 14:01:37 +08006933/*
6934 * Set appropriate PRF function and other SSL / TLS1.2 functions
6935 *
6936 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006937 * - hash associated with the ciphersuite (only used by TLS 1.2)
6938 *
6939 * Outputs:
6940 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6941 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006942MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006943static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6944 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006945{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006946#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006948 handshake->tls_prf = tls_prf_sha384;
6949 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6950 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006952#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006953#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006954 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006955 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006956 handshake->tls_prf = tls_prf_sha256;
6957 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6958 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6959 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006960#else
Jerry Yuf009d862022-02-17 14:01:37 +08006961 {
Jerry Yuf009d862022-02-17 14:01:37 +08006962 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006963 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006965 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006966#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006967
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006969}
Jerry Yud6ab2352022-02-17 14:03:43 +08006970
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006971/*
6972 * Compute master secret if needed
6973 *
6974 * Parameters:
6975 * [in/out] handshake
6976 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6977 * (PSA-PSK) ciphersuite_info, psk_opaque
6978 * [out] premaster (cleared)
6979 * [out] master
6980 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6981 * debug: conf->f_dbg, conf->p_dbg
6982 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006983 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006984 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006986static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6987 unsigned char *master,
6988 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006989{
6990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6991
6992 /* cf. RFC 5246, Section 8.1:
6993 * "The master secret is always exactly 48 bytes in length." */
6994 size_t const master_secret_len = 48;
6995
6996#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6997 unsigned char session_hash[48];
6998#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6999
7000 /* The label for the KDF used for key expansion.
7001 * This is either "master secret" or "extended master secret"
7002 * depending on whether the Extended Master Secret extension
7003 * is used. */
7004 char const *lbl = "master secret";
7005
Przemek Stekielae4ed302022-04-05 17:15:55 +02007006 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007007 * - If the Extended Master Secret extension is not used,
7008 * this is ClientHello.Random + ServerHello.Random
7009 * (see Sect. 8.1 in RFC 5246).
7010 * - If the Extended Master Secret extension is used,
7011 * this is the transcript of the handshake so far.
7012 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02007013 unsigned char const *seed = handshake->randbytes;
7014 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007015
7016#if !defined(MBEDTLS_DEBUG_C) && \
7017 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
7018 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007020 ssl = NULL; /* make sure we don't use it except for those cases */
7021 (void) ssl;
7022#endif
7023
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (handshake->resume != 0) {
7025 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
7026 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007027 }
7028
7029#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007031 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007032 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007033 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7034 if (ret != 0) {
7035 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7036 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007037
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7039 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007040 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007041#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007042
Przemek Stekiel99114f32022-04-22 11:20:09 +02007043#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007044 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007046 /* Perform PSK-to-MS expansion in a single step. */
7047 psa_status_t status;
7048 psa_algorithm_t alg;
7049 mbedtls_svc_key_id_t psk;
7050 psa_key_derivation_operation_t derivation =
7051 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007052 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007059 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007061 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007063
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007064 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007066
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007068 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007069 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007070 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007071 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007072 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007073 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007074 other_secret_len = 48;
7075 other_secret = handshake->premaster + 2;
7076 break;
7077 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007078 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007079 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7080 other_secret = handshake->premaster + 2;
7081 break;
7082 default:
7083 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007084 }
7085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 status = setup_psa_key_derivation(&derivation, psk, alg,
7087 ssl->conf->psk, ssl->conf->psk_len,
7088 seed, seed_len,
7089 (unsigned char const *) lbl,
7090 (size_t) strlen(lbl),
7091 other_secret, other_secret_len,
7092 master_secret_len);
7093 if (status != PSA_SUCCESS) {
7094 psa_key_derivation_abort(&derivation);
7095 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007096 }
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 status = psa_key_derivation_output_bytes(&derivation,
7099 master,
7100 master_secret_len);
7101 if (status != PSA_SUCCESS) {
7102 psa_key_derivation_abort(&derivation);
7103 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007104 }
7105
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 status = psa_key_derivation_abort(&derivation);
7107 if (status != PSA_SUCCESS) {
7108 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7109 }
7110 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007111#endif
7112 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007113#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7115 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007116 psa_status_t status;
7117 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7118 psa_key_derivation_operation_t derivation =
7119 PSA_KEY_DERIVATION_OPERATION_INIT;
7120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007122
7123 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 status = psa_key_derivation_setup(&derivation, alg);
7126 if (status != PSA_SUCCESS) {
7127 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007128 }
7129
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 status = psa_key_derivation_set_capacity(&derivation,
7131 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7132 if (status != PSA_SUCCESS) {
7133 psa_key_derivation_abort(&derivation);
7134 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007135 }
7136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7138 &derivation);
7139 if (status != PSA_SUCCESS) {
7140 psa_key_derivation_abort(&derivation);
7141 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007142 }
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 status = psa_key_derivation_output_bytes(&derivation,
7145 handshake->premaster,
7146 handshake->pmslen);
7147 if (status != PSA_SUCCESS) {
7148 psa_key_derivation_abort(&derivation);
7149 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7150 }
7151
7152 status = psa_key_derivation_abort(&derivation);
7153 if (status != PSA_SUCCESS) {
7154 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007155 }
7156 }
7157#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7159 lbl, seed, seed_len,
7160 master,
7161 master_secret_len);
7162 if (ret != 0) {
7163 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7164 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007165 }
7166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7168 handshake->premaster,
7169 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 mbedtls_platform_zeroize(handshake->premaster,
7172 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007173 }
7174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007176}
7177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007179{
7180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7181 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7182 ssl->handshake->ciphersuite_info;
7183
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007185
7186 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007188 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 if (ret != 0) {
7190 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7191 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007192 }
7193
7194 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 ret = ssl_compute_master(ssl->handshake,
7196 ssl->session_negotiate->master,
7197 ssl);
7198 if (ret != 0) {
7199 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7200 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007201 }
7202
7203 /* Swap the client and server random values:
7204 * - MS derivation wanted client+server (RFC 5246 8.1)
7205 * - key derivation wants server+client (RFC 5246 6.3) */
7206 {
7207 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 memcpy(tmp, ssl->handshake->randbytes, 64);
7209 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7210 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7211 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007212 }
7213
7214 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7216 ssl->session_negotiate->ciphersuite,
7217 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007218#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007220#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 ssl->handshake->tls_prf,
7222 ssl->handshake->randbytes,
7223 ssl->tls_version,
7224 ssl->conf->endpoint,
7225 ssl);
7226 if (ret != 0) {
7227 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7228 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007229 }
7230
7231 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7233 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007234
Gilles Peskine449bd832023-01-11 14:50:10 +01007235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007236
Gilles Peskine449bd832023-01-11 14:50:10 +01007237 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007238}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007239
Gilles Peskine449bd832023-01-11 14:50:10 +01007240int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007241{
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007243#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007244 case MBEDTLS_SSL_HASH_SHA384:
7245 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7246 break;
7247#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007248#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007249 case MBEDTLS_SSL_HASH_SHA256:
7250 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7251 break;
7252#endif
7253 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007255 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007256#if !defined(MBEDTLS_MD_CAN_SHA384) && \
7257 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007258 (void) ssl;
7259#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007261}
7262
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007263#if defined(MBEDTLS_USE_PSA_CRYPTO)
7264static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7265 const psa_hash_operation_t *hs_op,
7266 size_t buffer_size,
7267 unsigned char *hash,
7268 size_t *hlen)
7269{
7270 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007271 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007272
7273#if !defined(MBEDTLS_DEBUG_C)
7274 (void) ssl;
7275#endif
7276 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007277 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007278 if (status != PSA_SUCCESS) {
7279 goto exit;
7280 }
7281
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007282 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007283 if (status != PSA_SUCCESS) {
7284 goto exit;
7285 }
7286
7287 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7288 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7289
7290exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007291 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007292 return mbedtls_md_error_from_psa(status);
7293}
7294#else
7295static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7296 const mbedtls_md_context_t *hs_ctx,
7297 unsigned char *hash,
7298 size_t *hlen)
7299{
7300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007301 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007302
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007303 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007304
7305#if !defined(MBEDTLS_DEBUG_C)
7306 (void) ssl;
7307#endif
7308 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7309
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007310 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007311 if (ret != 0) {
7312 goto exit;
7313 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007314 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007315 if (ret != 0) {
7316 goto exit;
7317 }
7318
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007319 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007320 if (ret != 0) {
7321 goto exit;
7322 }
7323
7324 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7325
7326 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7327 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7328
7329exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007330 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007331 return ret;
7332}
7333#endif /* MBEDTLS_USE_PSA_CRYPTO */
7334
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007335#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007336int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7337 unsigned char *hash,
7338 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007339{
7340#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007341 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7342 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007343#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007344 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7345 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007346#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007347}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007348#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007349
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007350#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007351int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7352 unsigned char *hash,
7353 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007354{
7355#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007356 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7357 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007358#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007359 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7360 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007361#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007362}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007363#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007364
Neil Armstrong80f6f322022-05-03 17:56:38 +02007365#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7366 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007367int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007368{
7369 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007371 const unsigned char *psk = NULL;
7372 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007376 /*
7377 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007378 * checked before calling this function.
7379 *
7380 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007381 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7384 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7385 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007386 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007387 }
7388
7389 /*
7390 * PMS = struct {
7391 * opaque other_secret<0..2^16-1>;
7392 * opaque psk<0..2^16-1>;
7393 * };
7394 * with "other_secret" depending on the particular key exchange
7395 */
7396#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7398 if (end - p < 2) {
7399 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7400 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007401
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007403 p += 2;
7404
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if (end < p || (size_t) (end - p) < psk_len) {
7406 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7407 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007408
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007410 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007412#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
7413#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007415 /*
7416 * other_secret already set by the ClientKeyExchange message,
7417 * and is 48 bytes long
7418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 if (end - p < 2) {
7420 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7421 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007422
7423 *p++ = 0;
7424 *p++ = 48;
7425 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007427#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
7428#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7431 size_t len;
7432
7433 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007435 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7437 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7438 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007439 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007441 p += 2 + len;
7442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7444 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007445#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007446#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7449 size_t zlen;
7450
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007452 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7454 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7455 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007456 }
7457
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007459 p += 2 + zlen;
7460
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7462 MBEDTLS_DEBUG_ECDH_Z);
7463 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007464#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007465 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7467 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007468 }
7469
7470 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 if (end - p < 2) {
7472 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7473 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007474
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007476 p += 2;
7477
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (end < p || (size_t) (end - p) < psk_len) {
7479 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7480 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007483 p += psk_len;
7484
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007485 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007486
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007488}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007489#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007490
7491#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007492MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007493static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007494
7495#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007496int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007497{
7498 /* If renegotiation is not enforced, retransmit until we would reach max
7499 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007501 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7502 unsigned char doublings = 1;
7503
Gilles Peskine449bd832023-01-11 14:50:10 +01007504 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007505 ++doublings;
7506 ratio >>= 1;
7507 }
7508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 if (++ssl->renego_records_seen > doublings) {
7510 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7511 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007512 }
7513 }
7514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007516}
7517#endif
7518#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007519
Jerry Yud9526692022-02-17 14:23:47 +08007520/*
7521 * Handshake functions
7522 */
7523#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7524/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007525int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007526{
7527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7528 ssl->handshake->ciphersuite_info;
7529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7533 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007534 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007536 }
7537
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7539 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007540}
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007543{
7544 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7545 ssl->handshake->ciphersuite_info;
7546
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7550 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007551 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007553 }
7554
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7556 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007557}
7558
7559#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7560/* Some certificate support -> implement write and parse */
7561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007563{
7564 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7565 size_t i, n;
7566 const mbedtls_x509_crt *crt;
7567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7568 ssl->handshake->ciphersuite_info;
7569
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007571
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7573 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007574 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007576 }
7577
7578#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7580 if (ssl->handshake->client_auth == 0) {
7581 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007582 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007584 }
7585 }
7586#endif /* MBEDTLS_SSL_CLI_C */
7587#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7589 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007590 /* Should never happen because we shouldn't have picked the
7591 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007593 }
7594 }
7595#endif
7596
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007598
7599 /*
7600 * 0 . 0 handshake type
7601 * 1 . 3 handshake length
7602 * 4 . 6 length of all certs
7603 * 7 . 9 length of cert. 1
7604 * 10 . n-1 peer certificate
7605 * n . n+2 length of cert. 2
7606 * n+3 . ... upper level cert, etc.
7607 */
7608 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007612 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7614 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7615 " > %" MBEDTLS_PRINTF_SIZET,
7616 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7617 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007618 }
7619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7621 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7622 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007623
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007625 i += n; crt = crt->next;
7626 }
7627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7629 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7630 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007631
7632 ssl->out_msglen = i;
7633 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7634 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7635
7636 ssl->state++;
7637
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7639 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7640 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007641 }
7642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007646}
7647
7648#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7649
7650#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007651MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007652static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7653 unsigned char *crt_buf,
7654 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007655{
7656 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7657
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 if (peer_crt == NULL) {
7659 return -1;
7660 }
Jerry Yud9526692022-02-17 14:23:47 +08007661
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 if (peer_crt->raw.len != crt_buf_len) {
7663 return -1;
7664 }
Jerry Yud9526692022-02-17 14:23:47 +08007665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007667}
7668#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007669MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007670static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7671 unsigned char *crt_buf,
7672 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007673{
7674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7675 unsigned char const * const peer_cert_digest =
7676 ssl->session->peer_cert_digest;
7677 mbedtls_md_type_t const peer_cert_digest_type =
7678 ssl->session->peer_cert_digest_type;
7679 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007681 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7682 size_t digest_len;
7683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 if (peer_cert_digest == NULL || digest_info == NULL) {
7685 return -1;
7686 }
Jerry Yud9526692022-02-17 14:23:47 +08007687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 digest_len = mbedtls_md_get_size(digest_info);
7689 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7690 return -1;
7691 }
Jerry Yud9526692022-02-17 14:23:47 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7694 if (ret != 0) {
7695 return -1;
7696 }
Jerry Yud9526692022-02-17 14:23:47 +08007697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007699}
7700#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7701#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7702
7703/*
7704 * Once the certificate message is read, parse it into a cert chain and
7705 * perform basic checks, but leave actual verification to the caller
7706 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007708static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7709 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007710{
7711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7712#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007714#endif
7715 size_t i, n;
7716 uint8_t alert;
7717
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7719 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7720 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7721 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7722 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007723 }
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7726 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7727 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7728 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007729 }
7730
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7732 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7733 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7734 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7735 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007736 }
7737
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007739
7740 /*
7741 * Same message structure as in mbedtls_ssl_write_certificate()
7742 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007743 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007744
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 if (ssl->in_msg[i] != 0 ||
7746 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7747 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7748 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7749 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7750 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007751 }
7752
7753 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7754 i += 3;
7755
7756 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007757 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007758 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 if (i + 3 > ssl->in_hslen) {
7760 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7761 mbedtls_ssl_send_alert_message(ssl,
7762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7763 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7764 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007765 }
7766 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7767 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 if (ssl->in_msg[i] != 0) {
7769 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7770 mbedtls_ssl_send_alert_message(ssl,
7771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7772 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7773 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007774 }
7775
7776 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007777 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007778 i += 3;
7779
Gilles Peskine449bd832023-01-11 14:50:10 +01007780 if (n < 128 || i + n > ssl->in_hslen) {
7781 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7782 mbedtls_ssl_send_alert_message(ssl,
7783 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7784 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7785 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007786 }
7787
7788 /* Check if we're handling the first CRT in the chain. */
7789#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007791 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007793 /* During client-side renegotiation, check that the server's
7794 * end-CRTs hasn't changed compared to the initial handshake,
7795 * mitigating the triple handshake attack. On success, reuse
7796 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007797 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7798 if (ssl_check_peer_crt_unchanged(ssl,
7799 &ssl->in_msg[i],
7800 n) != 0) {
7801 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7802 mbedtls_ssl_send_alert_message(ssl,
7803 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7804 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7805 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007806 }
7807
7808 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007810 }
7811#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7812
7813 /* Parse the next certificate in the chain. */
7814#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007816#else
7817 /* If we don't need to store the CRT chain permanently, parse
7818 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007820#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007822 case 0: /*ok*/
7823 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7824 /* Ignore certificate with an unknown algorithm: maybe a
7825 prior certificate was already trusted. */
7826 break;
7827
7828 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7829 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7830 goto crt_parse_der_failed;
7831
7832 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7833 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7834 goto crt_parse_der_failed;
7835
7836 default:
7837 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007838crt_parse_der_failed:
7839 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7840 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7841 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007842 }
7843
7844 i += n;
7845 }
7846
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7848 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007849}
7850
7851#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007853static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007854{
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7856 return -1;
7857 }
Jerry Yud9526692022-02-17 14:23:47 +08007858
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007860 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7861 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7863 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7864 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007867}
7868#endif /* MBEDTLS_SSL_SRV_C */
7869
7870/* Check if a certificate message is expected.
7871 * Return either
7872 * - SSL_CERTIFICATE_EXPECTED, or
7873 * - SSL_CERTIFICATE_SKIP
7874 * indicating whether a Certificate message is expected or not.
7875 */
7876#define SSL_CERTIFICATE_EXPECTED 0
7877#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007878MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007879static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7880 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007881{
7882 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7883 ssl->handshake->ciphersuite_info;
7884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7886 return SSL_CERTIFICATE_SKIP;
7887 }
Jerry Yud9526692022-02-17 14:23:47 +08007888
7889#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7891 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7892 return SSL_CERTIFICATE_SKIP;
7893 }
Jerry Yud9526692022-02-17 14:23:47 +08007894
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007896 ssl->session_negotiate->verify_result =
7897 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007899 }
7900 }
7901#else
7902 ((void) authmode);
7903#endif /* MBEDTLS_SSL_SRV_C */
7904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007906}
7907
Jerry Yud9526692022-02-17 14:23:47 +08007908#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007909MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007910static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7911 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007912{
7913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7914 /* Remember digest of the peer's end-CRT. */
7915 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7917 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7918 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7919 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7920 mbedtls_ssl_send_alert_message(ssl,
7921 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7922 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007925 }
7926
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 ret = mbedtls_md(mbedtls_md_info_from_type(
7928 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7929 start, len,
7930 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007931
7932 ssl->session_negotiate->peer_cert_digest_type =
7933 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7934 ssl->session_negotiate->peer_cert_digest_len =
7935 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7936
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007938}
7939
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007940MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007941static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7942 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007943{
7944 unsigned char *end = start + len;
7945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7946
7947 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7949 ret = mbedtls_pk_parse_subpubkey(&start, end,
7950 &ssl->handshake->peer_pubkey);
7951 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007952 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007954 }
7955
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007957}
7958#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7959
Gilles Peskine449bd832023-01-11 14:50:10 +01007960int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007961{
7962 int ret = 0;
7963 int crt_expected;
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +02007964 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007965#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7966 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7967 ? ssl->handshake->sni_authmode
7968 : ssl->conf->authmode;
7969#else
7970 const int authmode = ssl->conf->authmode;
7971#endif
7972 void *rs_ctx = NULL;
7973 mbedtls_x509_crt *chain = NULL;
7974
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007976
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7978 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7979 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007980 goto exit;
7981 }
7982
7983#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007984 if (ssl->handshake->ecrs_enabled &&
7985 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007986 chain = ssl->handshake->ecrs_peer_cert;
7987 ssl->handshake->ecrs_peer_cert = NULL;
7988 goto crt_verify;
7989 }
7990#endif
7991
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007993 /* mbedtls_ssl_read_record may have sent an alert already. We
7994 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007996 goto exit;
7997 }
7998
7999#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008001 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
8002
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08008004 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 }
Jerry Yud9526692022-02-17 14:23:47 +08008006
8007 goto exit;
8008 }
8009#endif /* MBEDTLS_SSL_SRV_C */
8010
8011 /* Clear existing peer CRT structure in case we tried to
8012 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08008014
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
8016 if (chain == NULL) {
8017 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
8018 sizeof(mbedtls_x509_crt)));
8019 mbedtls_ssl_send_alert_message(ssl,
8020 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8021 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08008022
8023 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
8024 goto exit;
8025 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008027
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 ret = ssl_parse_certificate_chain(ssl, chain);
8029 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008030 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 }
Jerry Yud9526692022-02-17 14:23:47 +08008032
8033#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008035 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 }
Jerry Yud9526692022-02-17 14:23:47 +08008037
8038crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08008040 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 }
Jerry Yud9526692022-02-17 14:23:47 +08008042#endif
8043
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +02008044 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
8045 ssl->handshake->ciphersuite_info,
8046 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008048 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 }
Jerry Yud9526692022-02-17 14:23:47 +08008050
8051#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8052 {
8053 unsigned char *crt_start, *pk_start;
8054 size_t crt_len, pk_len;
8055
8056 /* We parse the CRT chain without copying, so
8057 * these pointers point into the input buffer,
8058 * and are hence still valid after freeing the
8059 * CRT chain. */
8060
8061 crt_start = chain->raw.p;
8062 crt_len = chain->raw.len;
8063
8064 pk_start = chain->pk_raw.p;
8065 pk_len = chain->pk_raw.len;
8066
8067 /* Free the CRT structures before computing
8068 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008069 mbedtls_x509_crt_free(chain);
8070 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008071 chain = NULL;
8072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
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
Gilles Peskine449bd832023-01-11 14:50:10 +01008078 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8079 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008080 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 }
Jerry Yud9526692022-02-17 14:23:47 +08008082 }
8083#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8084 /* Pass ownership to session structure. */
8085 ssl->session_negotiate->peer_cert = chain;
8086 chain = NULL;
8087#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8088
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008090
8091exit:
8092
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008094 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 }
Jerry Yud9526692022-02-17 14:23:47 +08008096
8097#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008099 ssl->handshake->ecrs_peer_cert = chain;
8100 chain = NULL;
8101 }
8102#endif
8103
Gilles Peskine449bd832023-01-11 14:50:10 +01008104 if (chain != NULL) {
8105 mbedtls_x509_crt_free(chain);
8106 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008107 }
8108
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008110}
8111#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8112
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008113static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8114 unsigned char *padbuf, size_t hlen,
8115 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008116{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008117 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008118 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008119#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008120 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008121 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008122 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008123 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008124#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008126 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008127 mbedtls_md_context_t cloned_ctx;
8128 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008129#endif
8130
8131 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008133 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008135
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008137 ? "client finished"
8138 : "server finished";
8139
8140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008141 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008142
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008143 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008145 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008146 }
8147
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008148 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008150 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008151 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008152 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008153#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008154 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008155
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008156 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008157 if (ret != 0) {
8158 goto exit;
8159 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008160 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008161 if (ret != 0) {
8162 goto exit;
8163 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008164
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008165 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008166 if (ret != 0) {
8167 goto exit;
8168 }
8169#endif /* MBEDTLS_USE_PSA_CRYPTO */
8170
8171 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8172
Jerry Yu615bd6f2022-02-17 14:25:15 +08008173 /*
8174 * TLSv1.2:
8175 * hash = PRF( master, finished_label,
8176 * Hash( handshake ) )[0.11]
8177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008179 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008182
Paul Elliottecb95be2023-08-11 16:41:04 +01008183 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008184
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008185 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008186
8187exit:
8188#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008189 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008190 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008191#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008192 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008193 return ret;
8194#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008195}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008196
8197#if defined(MBEDTLS_MD_CAN_SHA256)
8198static int ssl_calc_finished_tls_sha256(
8199 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8200{
8201 unsigned char padbuf[32];
8202 return ssl_calc_finished_tls_generic(ssl,
8203#if defined(MBEDTLS_USE_PSA_CRYPTO)
8204 &ssl->handshake->fin_sha256_psa,
8205#else
8206 &ssl->handshake->fin_sha256,
8207#endif
8208 padbuf, sizeof(padbuf),
8209 buf, from);
8210}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008211#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008212
Jerry Yub7ba49e2022-02-17 14:25:53 +08008213
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008214#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008215static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008216 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008217{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008218 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008219 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008221 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008222#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008223 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008224#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008225 padbuf, sizeof(padbuf),
8226 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008227}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008228#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008229
Gilles Peskine449bd832023-01-11 14:50:10 +01008230void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008231{
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008233
8234 /*
8235 * Free our handshake params
8236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 mbedtls_ssl_handshake_free(ssl);
8238 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008239 ssl->handshake = NULL;
8240
8241 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008242 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 if (ssl->transform) {
8245 mbedtls_ssl_transform_free(ssl->transform);
8246 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008247 }
8248 ssl->transform = ssl->transform_negotiate;
8249 ssl->transform_negotiate = NULL;
8250
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008252}
8253
Gilles Peskine449bd832023-01-11 14:50:10 +01008254void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008255{
8256 int resume = ssl->handshake->resume;
8257
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008259
8260#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008262 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8263 ssl->renego_records_seen = 0;
8264 }
8265#endif
8266
8267 /*
8268 * Free the previous session and switch in the current one
8269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008271#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8272 /* RFC 7366 3.1: keep the EtM state */
8273 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008275#endif
8276
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 mbedtls_ssl_session_free(ssl->session);
8278 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008279 }
8280 ssl->session = ssl->session_negotiate;
8281 ssl->session_negotiate = NULL;
8282
8283 /*
8284 * Add cache entry
8285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008287 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 resume == 0) {
8289 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8290 ssl->session->id,
8291 ssl->session->id_len,
8292 ssl->session) != 0) {
8293 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8294 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008295 }
8296
8297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8299 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008300 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008302
8303 /* Keep last flight around in case we need to resend it:
8304 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8306 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008307#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008309
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008310 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008311
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008313}
8314
Gilles Peskine449bd832023-01-11 14:50:10 +01008315int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008316{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008317 int ret;
8318 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008319
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008321
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008323
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008324 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8325 if (ret != 0) {
8326 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8327 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008328
8329 /*
8330 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8331 * may define some other value. Currently (early 2016), no defined
8332 * ciphersuite does this (and this is unlikely to change as activity has
8333 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8334 */
8335 hash_len = 12;
8336
8337#if defined(MBEDTLS_SSL_RENEGOTIATION)
8338 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008340#endif
8341
8342 ssl->out_msglen = 4 + hash_len;
8343 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8344 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8345
8346 /*
8347 * In case of session resuming, invert the client and server
8348 * ChangeCipherSpec messages order.
8349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008351#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008353 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008355#endif
8356#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008358 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008360#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008362 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008364
8365 /*
8366 * Switch to our negotiated transform and session parameters for outbound
8367 * data.
8368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008370
8371#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008373 unsigned char i;
8374
8375 /* Remember current epoch settings for resending */
8376 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008377 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8378 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008379
8380 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008382
8383
8384 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 for (i = 2; i > 0; i--) {
8386 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008387 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 }
8389 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008390
8391 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 if (i == 0) {
8393 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8394 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008395 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008399
8400 ssl->transform_out = ssl->transform_negotiate;
8401 ssl->session_out = ssl->session_negotiate;
8402
8403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8405 mbedtls_ssl_send_flight_completed(ssl);
8406 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008407#endif
8408
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8410 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8411 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008412 }
8413
8414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8416 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8417 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8418 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008419 }
8420#endif
8421
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008423
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008425}
8426
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008427#define SSL_MAX_HASH_LEN 12
8428
Gilles Peskine449bd832023-01-11 14:50:10 +01008429int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008430{
8431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8432 unsigned int hash_len = 12;
8433 unsigned char buf[SSL_MAX_HASH_LEN];
8434
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008436
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008437 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8438 if (ret != 0) {
8439 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8440 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008441
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8443 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008444 goto exit;
8445 }
8446
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8448 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8449 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8450 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008451 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8452 goto exit;
8453 }
8454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8456 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8457 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008458 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8459 goto exit;
8460 }
8461
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8463 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8464 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8465 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008466 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8467 goto exit;
8468 }
8469
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8471 buf, hash_len) != 0) {
8472 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8473 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8474 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008475 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8476 goto exit;
8477 }
8478
8479#if defined(MBEDTLS_SSL_RENEGOTIATION)
8480 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008482#endif
8483
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008485#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008487 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008489#endif
8490#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008492 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008494#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008496 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008498
8499#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8501 mbedtls_ssl_recv_flight_completed(ssl);
8502 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008503#endif
8504
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008506
8507exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 mbedtls_platform_zeroize(buf, hash_len);
8509 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008510}
8511
Jerry Yu392112c2022-02-17 14:34:10 +08008512#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8513/*
8514 * Helper to get TLS 1.2 PRF from ciphersuite
8515 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008517static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008518{
Jerry Yu392112c2022-02-17 14:34:10 +08008519 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008521#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8523 return tls_prf_sha384;
8524 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008525#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008526#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008527 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8529 return tls_prf_sha256;
8530 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008531 }
8532#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008533#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8534 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008535 (void) ciphersuite_info;
8536#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008537
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008539}
8540#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008541
Gilles Peskine449bd832023-01-11 14:50:10 +01008542static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008543{
8544 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008545#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 if (tls_prf == tls_prf_sha384) {
8547 return MBEDTLS_SSL_TLS_PRF_SHA384;
8548 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008549#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008550#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008551 if (tls_prf == tls_prf_sha256) {
8552 return MBEDTLS_SSL_TLS_PRF_SHA256;
8553 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008554#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008556}
8557
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008558/*
8559 * Populate a transform structure with session keys and all the other
8560 * necessary information.
8561 *
8562 * Parameters:
8563 * - [in/out]: transform: structure to populate
8564 * [in] must be just initialised with mbedtls_ssl_transform_init()
8565 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8566 * - [in] ciphersuite
8567 * - [in] master
8568 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008569 * - [in] tls_prf: pointer to PRF to use for key derivation
8570 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008571 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008572 * - [in] endpoint: client or server
8573 * - [in] ssl: used for:
8574 * - ssl->conf->{f,p}_export_keys
8575 * [in] optionally used for:
8576 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8577 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008578MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008579static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8580 int ciphersuite,
8581 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008582#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008584#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 ssl_tls_prf_t tls_prf,
8586 const unsigned char randbytes[64],
8587 mbedtls_ssl_protocol_version tls_version,
8588 unsigned endpoint,
8589 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008590{
8591 int ret = 0;
8592 unsigned char keyblk[256];
8593 unsigned char *key1;
8594 unsigned char *key2;
8595 unsigned char *mac_enc;
8596 unsigned char *mac_dec;
8597 size_t mac_key_len = 0;
8598 size_t iv_copy_len;
8599 size_t keylen;
8600 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008601 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008602#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008603 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008604 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008605#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008606
8607#if defined(MBEDTLS_USE_PSA_CRYPTO)
8608 psa_key_type_t key_type;
8609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8610 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008611 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008612 size_t key_bits;
8613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8614#endif
8615
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008616 /*
8617 * Some data just needs copying into the structure
8618 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008619#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008620 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008621#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008622 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008623
8624#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008626#endif
8627
8628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008630 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8631 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008633 }
8634#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8635
8636 /*
8637 * Get various info structures
8638 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8640 if (ciphersuite_info == NULL) {
8641 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8642 ciphersuite));
8643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008644 }
8645
Neil Armstrongab555e02022-04-04 11:07:59 +02008646 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008647#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008649#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008651
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008653 transform->taglen =
8654 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008656
8657#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008658 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 transform->taglen,
8660 &alg,
8661 &key_type,
8662 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008663 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008665 goto end;
8666 }
8667#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008668 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 if (cipher_info == NULL) {
8670 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8671 ciphersuite_info->cipher));
8672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008673 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008674#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008675
Neil Armstronge4512952022-03-08 09:08:22 +01008676#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008677 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008679 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 (unsigned) ciphersuite_info->mac));
8681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008682 }
8683#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008684 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 if (md_info == NULL) {
8686 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8687 (unsigned) ciphersuite_info->mac));
8688 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008689 }
Neil Armstronge4512952022-03-08 09:08:22 +01008690#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008691
8692#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8693 /* Copy own and peer's CID if the use of the CID
8694 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8696 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008697
8698 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8700 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8701 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008702
8703 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8705 ssl->handshake->peer_cid_len);
8706 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8707 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008708 }
8709#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8710
8711 /*
8712 * Compute key block using the PRF
8713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8715 if (ret != 0) {
8716 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8717 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008718 }
8719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8721 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8722 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8723 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8724 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008725
8726 /*
8727 * Determine the appropriate key, IV and MAC length.
8728 */
8729
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008730#if defined(MBEDTLS_USE_PSA_CRYPTO)
8731 keylen = PSA_BITS_TO_BYTES(key_bits);
8732#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008733 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008734#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008735
Valerio Settie5707042023-10-11 11:54:42 +02008736#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008738 size_t explicit_ivlen;
8739
8740 transform->maclen = 0;
8741 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008742
8743 /* All modes haves 96-bit IVs, but the length of the static parts vary
8744 * with mode and version:
8745 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8746 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8747 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8748 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8749 * sequence number).
8750 */
8751 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008752
8753 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008754#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008756#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8758 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008759#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008762 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008764 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766
8767 /* Minimum length of encrypted record */
8768 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8769 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 } else
Valerio Settie5707042023-10-11 11:54:42 +02008771#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008772#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008774 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008776#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008778#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008779 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008780#endif /* MBEDTLS_USE_PSA_CRYPTO */
8781
8782#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008783 /* Get MAC length */
8784 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8785#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008786 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8788 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8789 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008790 goto end;
8791 }
8792
8793 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008795#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008796 transform->maclen = mac_key_len;
8797
8798 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008799#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008801#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008802 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008803#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008804
8805 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008807 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008809 /*
8810 * GenericBlockCipher:
8811 * 1. if EtM is in use: one block plus MAC
8812 * otherwise: * first multiple of blocklen greater than maclen
8813 * 2. IV
8814 */
8815#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008817 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 + block_size;
8819 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008820#endif
8821 {
8822 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 + block_size
8824 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008825 }
8826
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008828 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 } else {
8830 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008831 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8832 goto end;
8833 }
8834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008836#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8837 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008840 }
8841
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8843 (unsigned) keylen,
8844 (unsigned) transform->minlen,
8845 (unsigned) transform->ivlen,
8846 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008847
8848 /*
8849 * Finally setup the cipher contexts, IVs and MAC secrets.
8850 */
8851#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008853 key1 = keyblk + mac_key_len * 2;
8854 key2 = keyblk + mac_key_len * 2 + keylen;
8855
8856 mac_enc = keyblk;
8857 mac_dec = keyblk + mac_key_len;
8858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 iv_copy_len = (transform->fixed_ivlen) ?
8860 transform->fixed_ivlen : transform->ivlen;
8861 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8862 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8863 iv_copy_len);
8864 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008865#endif /* MBEDTLS_SSL_CLI_C */
8866#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008868 key1 = keyblk + mac_key_len * 2 + keylen;
8869 key2 = keyblk + mac_key_len * 2;
8870
8871 mac_enc = keyblk + mac_key_len;
8872 mac_dec = keyblk;
8873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 iv_copy_len = (transform->fixed_ivlen) ?
8875 transform->fixed_ivlen : transform->ivlen;
8876 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8877 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8878 iv_copy_len);
8879 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008880#endif /* MBEDTLS_SSL_SRV_C */
8881 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008883 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8884 goto end;
8885 }
8886
Paul Elliott4fb19552023-10-18 12:15:30 +01008887 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 ssl->f_export_keys(ssl->p_export_keys,
8889 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8890 master, 48,
8891 randbytes + 32,
8892 randbytes,
8893 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008894 }
8895
8896#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008897 transform->psa_alg = alg;
8898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8900 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8901 psa_set_key_algorithm(&attributes, alg);
8902 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 if ((status = psa_import_key(&attributes,
8905 key1,
8906 PSA_BITS_TO_BYTES(key_bits),
8907 &transform->psa_key_enc)) != PSA_SUCCESS) {
8908 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008909 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008911 goto end;
8912 }
8913
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008915
Gilles Peskine449bd832023-01-11 14:50:10 +01008916 if ((status = psa_import_key(&attributes,
8917 key2,
8918 PSA_BITS_TO_BYTES(key_bits),
8919 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008920 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008922 goto end;
8923 }
8924 }
8925#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8927 cipher_info)) != 0) {
8928 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008929 goto end;
8930 }
8931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8933 cipher_info)) != 0) {
8934 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008935 goto end;
8936 }
8937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8939 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8940 MBEDTLS_ENCRYPT)) != 0) {
8941 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008942 goto end;
8943 }
8944
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8946 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8947 MBEDTLS_DECRYPT)) != 0) {
8948 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008949 goto end;
8950 }
8951
8952#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8954 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8955 MBEDTLS_PADDING_NONE)) != 0) {
8956 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008957 goto end;
8958 }
8959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8961 MBEDTLS_PADDING_NONE)) != 0) {
8962 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008963 goto end;
8964 }
8965 }
8966#endif /* MBEDTLS_CIPHER_MODE_CBC */
8967#endif /* MBEDTLS_USE_PSA_CRYPTO */
8968
Neil Armstrong29c0c042022-03-17 17:47:28 +01008969#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8970 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8971 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008973#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008975
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8977 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8978 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 if ((status = psa_import_key(&attributes,
8981 mac_enc, mac_key_len,
8982 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008983 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008985 goto end;
8986 }
8987
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8989 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008990#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008992#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008994 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8996 PSA_KEY_USAGE_VERIFY_HASH);
8997 } else {
8998 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8999 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if ((status = psa_import_key(&attributes,
9002 mac_dec, mac_key_len,
9003 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009004 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01009006 goto end;
9007 }
9008#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
9010 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009011 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 }
9013 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
9014 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01009015 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01009017#endif /* MBEDTLS_USE_PSA_CRYPTO */
9018 }
9019#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
9020
9021 ((void) mac_dec);
9022 ((void) mac_enc);
9023
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009024end:
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
9026 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08009027}
9028
Valerio Settia08b1a42022-11-17 15:10:02 +01009029#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
9030 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01009031int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 psa_pake_operation_t *pake_ctx,
9033 const unsigned char *buf,
9034 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009035{
9036 psa_status_t status;
9037 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01009038 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009039 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9040 * At round two perform a single cycle
9041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 for (; remaining_steps > 0; remaining_steps--) {
9045 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01009046 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009048 /* Length is stored at the first byte */
9049 size_t length = buf[input_offset];
9050 input_offset += 1;
9051
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009053 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9054 }
9055
Gilles Peskine449bd832023-01-11 14:50:10 +01009056 status = psa_pake_input(pake_ctx, step,
9057 buf + input_offset, length);
9058 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009059 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009060 }
9061
9062 input_offset += length;
9063 }
9064 }
9065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009067 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009069
Gilles Peskine449bd832023-01-11 14:50:10 +01009070 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009071}
9072
Valerio Setti6b3dab02022-11-17 17:14:54 +01009073int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 psa_pake_operation_t *pake_ctx,
9075 unsigned char *buf,
9076 size_t len, size_t *olen,
9077 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009078{
9079 psa_status_t status;
9080 size_t output_offset = 0;
9081 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009082 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009083 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9084 * At round two perform a single cycle
9085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009087
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 for (; remaining_steps > 0; remaining_steps--) {
9089 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9090 step <= PSA_PAKE_STEP_ZK_PROOF;
9091 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009092 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009093 * For each step, prepend 1 byte with the length of the data as
9094 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 status = psa_pake_output(pake_ctx, step,
9097 buf + output_offset + 1,
9098 len - output_offset - 1,
9099 &output_len);
9100 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009101 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009102 }
9103
Valerio Setti99d88c12022-11-22 16:03:43 +01009104 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009105
9106 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009107 }
9108 }
9109
9110 *olen = output_offset;
9111
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009113}
Valerio Settia08b1a42022-11-17 15:10:02 +01009114#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9115
Jerry Yuee40f9d2022-02-17 14:55:16 +08009116#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009117int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9118 unsigned char *hash, size_t *hashlen,
9119 unsigned char *data, size_t data_len,
9120 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009121{
9122 psa_status_t status;
9123 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009124 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 if ((status = psa_hash_setup(&hash_operation,
9129 hash_alg)) != PSA_SUCCESS) {
9130 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009131 goto exit;
9132 }
9133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9135 64)) != PSA_SUCCESS) {
9136 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009137 goto exit;
9138 }
9139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if ((status = psa_hash_update(&hash_operation,
9141 data, data_len)) != PSA_SUCCESS) {
9142 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009143 goto exit;
9144 }
9145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9147 hashlen)) != PSA_SUCCESS) {
9148 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9149 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009150 }
9151
9152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 if (status != PSA_SUCCESS) {
9154 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9155 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9156 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009157 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009159 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9160 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009162 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009164 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009166 }
9167 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009169}
9170
9171#else
9172
Gilles Peskine449bd832023-01-11 14:50:10 +01009173int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9174 unsigned char *hash, size_t *hashlen,
9175 unsigned char *data, size_t data_len,
9176 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009177{
9178 int ret = 0;
9179 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9181 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009186
9187 /*
9188 * digitally-signed struct {
9189 * opaque client_random[32];
9190 * opaque server_random[32];
9191 * ServerDHParams params;
9192 * };
9193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9195 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009196 goto exit;
9197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9199 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009200 goto exit;
9201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9203 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009204 goto exit;
9205 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9207 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009208 goto exit;
9209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9211 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009212 goto exit;
9213 }
9214
9215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009216 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009217
Gilles Peskine449bd832023-01-11 14:50:10 +01009218 if (ret != 0) {
9219 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9220 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9221 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009222
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009224}
9225#endif /* MBEDTLS_USE_PSA_CRYPTO */
9226
Jerry Yud9d91da2022-02-17 14:57:06 +08009227#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9228
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009229/* Find the preferred hash for a given signature algorithm. */
9230unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 mbedtls_ssl_context *ssl,
9232 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009233{
Gabor Mezei078e8032022-04-27 21:17:56 +02009234 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009235 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009236
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9238 return MBEDTLS_SSL_HASH_NONE;
9239 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009240
Gilles Peskine449bd832023-01-11 14:50:10 +01009241 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009242 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9244 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009245 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9247 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009248
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009249 mbedtls_md_type_t md_alg =
9250 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9251 if (md_alg == MBEDTLS_MD_NONE) {
9252 continue;
9253 }
9254
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009256#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009258 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009259 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009260
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9262 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9263 PSA_ALG_ECDSA(psa_hash_alg),
9264 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009265 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009267
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9269 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9270 PSA_ALG_RSA_PKCS1V15_SIGN(
9271 psa_hash_alg),
9272 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009273 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009274 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009275 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009276#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009279 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009280 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009283}
9284
9285#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9286
Jerry Yudc7bd172022-02-17 13:44:15 +08009287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9288
XiaokangQian75d40ef2022-04-20 11:05:24 +00009289int mbedtls_ssl_validate_ciphersuite(
9290 const mbedtls_ssl_context *ssl,
9291 const mbedtls_ssl_ciphersuite_t *suite_info,
9292 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009293 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009294{
9295 (void) ssl;
9296
Gilles Peskine449bd832023-01-11 14:50:10 +01009297 if (suite_info == NULL) {
9298 return -1;
9299 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009300
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 if ((suite_info->min_tls_version > max_tls_version) ||
9302 (suite_info->max_tls_version < min_tls_version)) {
9303 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009304 }
9305
XiaokangQian060d8672022-04-21 09:24:56 +00009306#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009307#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9310 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009311#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9313 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009314#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009315 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009317 }
9318#endif
9319
9320 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9321#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9323 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9324 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009325 }
9326#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9327#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9328
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009330}
9331
Ronald Crone68ab4f2022-10-05 12:46:29 +02009332#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009333/*
9334 * Function for writing a signature algorithm extension.
9335 *
9336 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9337 * value (TLS 1.3 RFC8446):
9338 * enum {
9339 * ....
9340 * ecdsa_secp256r1_sha256( 0x0403 ),
9341 * ecdsa_secp384r1_sha384( 0x0503 ),
9342 * ecdsa_secp521r1_sha512( 0x0603 ),
9343 * ....
9344 * } SignatureScheme;
9345 *
9346 * struct {
9347 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9348 * } SignatureSchemeList;
9349 *
9350 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9351 * value (TLS 1.2 RFC5246):
9352 * enum {
9353 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9354 * sha512(6), (255)
9355 * } HashAlgorithm;
9356 *
9357 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9358 * SignatureAlgorithm;
9359 *
9360 * struct {
9361 * HashAlgorithm hash;
9362 * SignatureAlgorithm signature;
9363 * } SignatureAndHashAlgorithm;
9364 *
9365 * SignatureAndHashAlgorithm
9366 * supported_signature_algorithms<2..2^16-2>;
9367 *
9368 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9369 * generalization of the TLS 1.2 signature algorithm extension.
9370 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9371 * `SignatureScheme` field of TLS 1.3
9372 *
9373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009374int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9375 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009376{
9377 unsigned char *p = buf;
9378 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9379 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9380
9381 *out_len = 0;
9382
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009384
9385 /* Check if we have space for header and length field:
9386 * - extension_type (2 bytes)
9387 * - extension_data_length (2 bytes)
9388 * - supported_signature_algorithms_length (2 bytes)
9389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009391 p += 6;
9392
9393 /*
9394 * Write supported_signature_algorithms
9395 */
9396 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9398 if (sig_alg == NULL) {
9399 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9400 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009401
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9403 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9404 *sig_alg,
9405 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9406 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009407 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 }
9409 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9410 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009411 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9413 *sig_alg,
9414 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009415 }
9416
9417 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009418 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 if (supported_sig_alg_len == 0) {
9420 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9421 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009422 }
9423
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9425 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9426 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009427
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009428 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009429
9430#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009432#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009433
Gilles Peskine449bd832023-01-11 14:50:10 +01009434 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009435}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009436#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009437
XiaokangQian40a35232022-05-07 09:02:40 +00009438#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009439/*
9440 * mbedtls_ssl_parse_server_name_ext
9441 *
9442 * Structure of server_name extension:
9443 *
9444 * enum {
9445 * host_name(0), (255)
9446 * } NameType;
9447 * opaque HostName<1..2^16-1>;
9448 *
9449 * struct {
9450 * NameType name_type;
9451 * select (name_type) {
9452 * case host_name: HostName;
9453 * } name;
9454 * } ServerName;
9455 * struct {
9456 * ServerName server_name_list<1..2^16-1>
9457 * } ServerNameList;
9458 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009459MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009460int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9461 const unsigned char *buf,
9462 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009463{
9464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9465 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009466 size_t server_name_list_len, hostname_len;
9467 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009468
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9472 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009473 p += 2;
9474
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009476 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 while (p < server_name_list_end) {
9478 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9479 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9480 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9481 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009482
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009484 /* sni_name is intended to be used only during the parsing of the
9485 * ClientHello message (it is reset to NULL before the end of
9486 * the message parsing). Thus it is ok to just point to the
9487 * reception buffer and not make a copy of it.
9488 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009489 ssl->handshake->sni_name = p + 3;
9490 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 if (ssl->conf->f_sni == NULL) {
9492 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009493 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9495 ssl, p + 3, hostname_len);
9496 if (ret != 0) {
9497 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9498 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9499 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9500 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9501 }
9502 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009503 }
9504
9505 p += hostname_len + 3;
9506 }
9507
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009509}
9510#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9511
XiaokangQianacb39922022-06-17 10:18:48 +00009512#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009513MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009514int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9515 const unsigned char *buf,
9516 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009517{
9518 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009519 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009520 const unsigned char *protocol_name_list;
9521 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009522 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009523
9524 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 if (ssl->conf->alpn_list == NULL) {
9526 return 0;
9527 }
XiaokangQianacb39922022-06-17 10:18:48 +00009528
9529 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009530 * RFC7301, section 3.1
9531 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009532 *
XiaokangQianc7403452022-06-23 03:24:12 +00009533 * struct {
9534 * ProtocolName protocol_name_list<2..2^16-1>
9535 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009536 */
9537
XiaokangQianc7403452022-06-23 03:24:12 +00009538 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009539 * protocol_name_list_len 2 bytes
9540 * protocol_name_len 1 bytes
9541 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009543 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009544
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009546 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009548 protocol_name_list = p;
9549 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009550
9551 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009553 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9555 protocol_name_len);
9556 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009557 MBEDTLS_SSL_PEND_FATAL_ALERT(
9558 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9560 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009561 }
9562
9563 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009564 }
9565
9566 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9568 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009569 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009571 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 if (protocol_name_len == alpn_len &&
9573 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009574 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009575 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009576 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009577
9578 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009579 }
9580 }
9581
XiaokangQian95d5f542022-06-24 02:29:26 +00009582 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009583 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9585 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9586 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009587}
9588
Gilles Peskine449bd832023-01-11 14:50:10 +01009589int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9590 unsigned char *buf,
9591 unsigned char *end,
9592 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009593{
9594 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009595 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009596 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009597
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 if (ssl->alpn_chosen == NULL) {
9599 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009600 }
9601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 protocol_name_len = strlen(ssl->alpn_chosen);
9603 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009604
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009606 /*
9607 * 0 . 1 ext identifier
9608 * 2 . 3 ext length
9609 * 4 . 5 protocol list length
9610 * 6 . 6 protocol name length
9611 * 7 . 7+n protocol name
9612 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009614
XiaokangQian95d5f542022-06-24 02:29:26 +00009615 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009616
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9618 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009619 /* Note: the length of the chosen protocol has been checked to be less
9620 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9621 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009625
9626#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009628#endif
9629
Gilles Peskine449bd832023-01-11 14:50:10 +01009630 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009631}
9632#endif /* MBEDTLS_SSL_ALPN */
9633
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009634#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009635 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009636 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009637 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009638int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9639 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009640{
9641 /* Initialize to suppress unnecessary compiler warning */
9642 size_t hostname_len = 0;
9643
9644 /* Check if new hostname is valid before
9645 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009646 if (hostname != NULL) {
9647 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009648
Gilles Peskine449bd832023-01-11 14:50:10 +01009649 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9651 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009652 }
9653
9654 /* Now it's clear that we will overwrite the old hostname,
9655 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009656 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009657 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009658 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009659 }
9660
9661 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009663 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 } else {
9665 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9666 if (session->hostname == NULL) {
9667 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9668 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009669
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009671 }
9672
Gilles Peskine449bd832023-01-11 14:50:10 +01009673 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009674}
Xiaokang Qian03409292022-10-12 02:49:52 +00009675#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9676 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009677 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009678 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009679
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009680#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9681 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009682int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9683 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009684{
9685 size_t alpn_len = 0;
9686
9687 if (alpn != NULL) {
9688 alpn_len = strlen(alpn);
9689
9690 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9691 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9692 }
9693 }
9694
9695 if (session->ticket_alpn != NULL) {
9696 mbedtls_zeroize_and_free(session->ticket_alpn,
9697 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009698 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009699 }
9700
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009701 if (alpn != NULL) {
9702 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009703 if (session->ticket_alpn == NULL) {
9704 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9705 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009706 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009707 }
9708
9709 return 0;
9710}
9711#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnardc32a4a22024-08-20 12:14:43 +02009712
9713/*
9714 * The following functions are used by 1.2 and 1.3, client and server.
9715 */
9716#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9717int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9718 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9719 int recv_endpoint,
9720 mbedtls_ssl_protocol_version tls_version,
9721 uint32_t *flags)
9722{
9723 int ret = 0;
9724 unsigned int usage = 0;
9725 const char *ext_oid;
9726 size_t ext_len;
9727
9728 /*
9729 * keyUsage
9730 */
9731
9732 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9733 * to check what a compliant client will think while choosing which cert
9734 * to send to the client. */
9735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9736 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9737 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9738 /* TLS 1.2 server part of the key exchange */
9739 switch (ciphersuite->key_exchange) {
9740 case MBEDTLS_KEY_EXCHANGE_RSA:
9741 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
9742 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9743 break;
9744
9745 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9746 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9747 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9748 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9749 break;
9750
9751 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9752 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9753 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9754 break;
9755
9756 /* Don't use default: we want warnings when adding new values */
9757 case MBEDTLS_KEY_EXCHANGE_NONE:
9758 case MBEDTLS_KEY_EXCHANGE_PSK:
9759 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9760 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9761 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9762 usage = 0;
9763 }
9764 } else
9765#endif
9766 {
9767 /* This is either TLS 1.3 authentication, which always uses signatures,
9768 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9769 * options we implement, both using signatures. */
9770 (void) tls_version;
9771 (void) ciphersuite;
9772 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9773 }
9774
9775 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9776 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9777 ret = -1;
9778 }
9779
9780 /*
9781 * extKeyUsage
9782 */
9783
9784 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9785 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9786 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9787 } else {
9788 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9789 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9790 }
9791
9792 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9793 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9794 ret = -1;
9795 }
9796
9797 return ret;
9798}
9799
9800int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9801 int authmode,
9802 mbedtls_x509_crt *chain,
9803 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9804 void *rs_ctx)
9805{
9806 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9807 return 0;
9808 }
9809
9810 /*
9811 * Primary check: use the appropriate X.509 verification function
9812 */
9813 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9814 void *p_vrfy;
9815 if (ssl->f_vrfy != NULL) {
9816 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9817 f_vrfy = ssl->f_vrfy;
9818 p_vrfy = ssl->p_vrfy;
9819 } else {
9820 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9821 f_vrfy = ssl->conf->f_vrfy;
9822 p_vrfy = ssl->conf->p_vrfy;
9823 }
9824
9825 int ret = 0;
9826 int have_ca_chain_or_callback = 0;
9827#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9828 if (ssl->conf->f_ca_cb != NULL) {
9829 ((void) rs_ctx);
9830 have_ca_chain_or_callback = 1;
9831
9832 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9833 ret = mbedtls_x509_crt_verify_with_ca_cb(
9834 chain,
9835 ssl->conf->f_ca_cb,
9836 ssl->conf->p_ca_cb,
9837 ssl->conf->cert_profile,
9838 ssl->hostname,
9839 &ssl->session_negotiate->verify_result,
9840 f_vrfy, p_vrfy);
9841 } else
9842#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9843 {
9844 mbedtls_x509_crt *ca_chain;
9845 mbedtls_x509_crl *ca_crl;
9846#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9847 if (ssl->handshake->sni_ca_chain != NULL) {
9848 ca_chain = ssl->handshake->sni_ca_chain;
9849 ca_crl = ssl->handshake->sni_ca_crl;
9850 } else
9851#endif
9852 {
9853 ca_chain = ssl->conf->ca_chain;
9854 ca_crl = ssl->conf->ca_crl;
9855 }
9856
9857 if (ca_chain != NULL) {
9858 have_ca_chain_or_callback = 1;
9859 }
9860
9861 ret = mbedtls_x509_crt_verify_restartable(
9862 chain,
9863 ca_chain, ca_crl,
9864 ssl->conf->cert_profile,
9865 ssl->hostname,
9866 &ssl->session_negotiate->verify_result,
9867 f_vrfy, p_vrfy, rs_ctx);
9868 }
9869
9870 if (ret != 0) {
9871 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9872 }
9873
9874#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9875 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9876 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9877 }
9878#endif
9879
9880 /*
9881 * Secondary checks: always done, but change 'ret' only if it was 0
9882 */
9883
9884 /* With TLS 1.2 and ECC certs, check that the curve used by the
9885 * certificate is on our list of acceptable curves.
9886 *
9887 * With TLS 1.3 this is not needed because the curve is part of the
9888 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9889 * we validate the signature made with the key associated to this cert.
9890 */
9891#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9892 defined(MBEDTLS_PK_HAVE_ECC_KEYS)
9893 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9894 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9895 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9896 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9897 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9898 if (ret == 0) {
9899 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9900 }
9901 }
9902 }
9903#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_PK_HAVE_ECC_KEYS */
9904
9905 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9906 if (mbedtls_ssl_check_cert_usage(chain,
9907 ciphersuite_info,
9908 ssl->conf->endpoint,
9909 ssl->tls_version,
9910 &ssl->session_negotiate->verify_result) != 0) {
9911 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9912 if (ret == 0) {
9913 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9914 }
9915 }
9916
9917 /* With authmode optional, we want to keep going if the certificate was
9918 * unacceptable, but still fail on other errors (out of memory etc),
9919 * including fatal errors from the f_vrfy callback.
9920 *
9921 * The only acceptable errors are:
9922 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9923 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9924 * Anything else is a fatal error. */
9925 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9926 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9927 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9928 ret = 0;
9929 }
9930
9931 /* Return a specific error as this is a user error: inconsistent
9932 * configuration - can't verify without trust anchors. */
9933 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9934 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9935 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9936 }
9937
9938 if (ret != 0) {
9939 uint8_t alert;
9940
9941 /* The certificate may have been rejected for several reasons.
9942 Pick one and send the corresponding alert. Which alert to send
9943 may be a subject of debate in some cases. */
9944 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9945 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9946 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9947 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9948 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9949 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9950 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9951 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9952 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9953 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9954 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9955 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9956 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9957 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9958 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9959 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9960 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9961 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9962 } else {
9963 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9964 }
9965 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9966 alert);
9967 }
9968
9969#if defined(MBEDTLS_DEBUG_C)
9970 if (ssl->session_negotiate->verify_result != 0) {
9971 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9972 (unsigned int) ssl->session_negotiate->verify_result));
9973 } else {
9974 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9975 }
9976#endif /* MBEDTLS_DEBUG_C */
9977
9978 return ret;
9979}
9980#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#endif /* MBEDTLS_SSL_TLS_C */