blob: 991b431179acb015d3f2ff94547c09dd9ec14ddf [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +010012#include "ssl_misc.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"
Andrzej Kurek25f27152022-08-17 16:09:31 -040021
Valerio Settib4f50762024-01-17 10:24:52 +010022#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000023#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050024#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010025#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020026#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <string.h>
29
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010031#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020032#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020033#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050034#include "psa/crypto.h"
35#endif
36
Janos Follath23bdca02016-10-07 14:47:14 +010037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020039#endif
40
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050041#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040042/* Define local translating functions to save code size by not using too many
43 * arguments in each translating place. */
44static int local_err_translation(psa_status_t status)
45{
46 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040047 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040048 psa_generic_status_to_mbedtls);
49}
50#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050051#endif
52
Ronald Cronad8c17b2022-06-10 17:18:09 +020053#if defined(MBEDTLS_TEST_HOOKS)
54static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
55
56void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010057 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020058{
59 chk_buf_ptr_fail_args.cur = cur;
60 chk_buf_ptr_fail_args.end = end;
61 chk_buf_ptr_fail_args.need = need;
62}
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020067}
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020070{
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return (chk_buf_ptr_fail_args.cur != args->cur) ||
72 (chk_buf_ptr_fail_args.end != args->end) ||
73 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020074}
75#endif /* MBEDTLS_TEST_HOOKS */
76
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020077#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010078
Hanno Beckera0e20d02019-05-15 14:03:01 +010079#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010080/* Top-level Connection ID API */
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
83 size_t len,
84 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010085{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
87 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
88 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
91 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
92 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010093 }
94
95 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010096 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098}
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
101 int enable,
102 unsigned char const *own_cid,
103 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
107 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100108
Hanno Beckerca092242019-04-25 16:01:49 +0100109 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (enable == MBEDTLS_SSL_CID_DISABLED) {
111 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
112 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100113 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
115 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (own_cid_len != ssl->conf->cid_len) {
118 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
119 (unsigned) own_cid_len,
120 (unsigned) ssl->conf->cid_len));
121 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100125 /* Truncation is not an issue here because
126 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
127 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100130}
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
133 int *enabled,
Sam Berry3504c882024-06-11 14:34:17 +0100134 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000136{
137 *enabled = MBEDTLS_SSL_CID_DISABLED;
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
140 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
141 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000142
143 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
144 * zero as this is indistinguishable from not requesting to use
145 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
147 return 0;
148 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000151 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid != NULL) {
153 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
154 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000155 }
156
157 *enabled = MBEDTLS_SSL_CID_ENABLED;
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000160}
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
163 int *enabled,
164 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
165 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100166{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
170 mbedtls_ssl_is_handshake_over(ssl) == 0) {
171 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100172 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100173
Hanno Beckerc5f24222019-05-03 12:54:52 +0100174 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
175 * were used, but client and server requested the empty CID.
176 * This is indistinguishable from not using the CID extension
177 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (ssl->transform_in->in_cid_len == 0 &&
179 ssl->transform_in->out_cid_len == 0) {
180 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181 }
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100184 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid != NULL) {
186 memcpy(peer_cid, ssl->transform_in->out_cid,
187 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100188 }
189 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100190
191 *enabled = MBEDTLS_SSL_CID_ENABLED;
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100194}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100195#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200200/*
201 * Convert max_fragment_length codes to length.
202 * RFC 6066 says:
203 * enum{
204 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
205 * } MaxFragmentLength;
206 * and we add 0 -> extension unused
207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200209{
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 switch (mfl) {
211 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
212 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
213 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
214 return 512;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
216 return 1024;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
218 return 2048;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
220 return 4096;
221 default:
222 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000223 }
224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
228 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200229{
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_ssl_session_free(dst);
231 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800232#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
233 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000234#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
235 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000236 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800237#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000238#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800239
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000240#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
241 defined(MBEDTLS_SSL_EARLY_DATA)
242 dst->ticket_alpn = NULL;
243#endif
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000246
247#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
252 if (dst->peer_cert == NULL) {
253 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
254 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
259 src->peer_cert->raw.len)) != 0) {
260 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263 }
264 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000265#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000267 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_calloc(1, src->peer_cert_digest_len);
269 if (dst->peer_cert_digest == NULL) {
270 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
271 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
274 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000275 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000276 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 }
278#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200281
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000282#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
283 defined(MBEDTLS_SSL_EARLY_DATA)
284 {
285 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
286 if (ret != 0) {
287 return ret;
288 }
289 }
290#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
291
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200292#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (src->ticket != NULL) {
294 dst->ticket = mbedtls_calloc(1, src->ticket_len);
295 if (dst->ticket == NULL) {
296 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
297 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000301
302#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
303 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
307 if (ret != 0) {
308 return ret;
309 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000310 }
311#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
312 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200313#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316}
317
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500318#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100320static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500321{
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
323 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800324 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500326
327 /* We want to copy len_new bytes when downsizing the buffer, and
328 * len_old bytes when upsizing, so we choose the smaller of two sizes,
329 * to fit one buffer into another. Size checks, ensuring that no data is
330 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 memcpy(resized_buffer, *buffer,
332 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100333 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500334
335 *buffer = resized_buffer;
336 *len_old = len_new;
337
338 return 0;
339}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
342 size_t in_buf_new_len,
343 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200344{
345 int modified = 0;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000346 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
Andrzej Kurek4a063792020-10-21 15:08:44 +0200347 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200349 written_in = ssl->in_msg - ssl->in_buf;
350 iv_offset_in = ssl->in_iv - ssl->in_buf;
351 len_offset_in = ssl->in_len - ssl->in_buf;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000352 hdr_in = ssl->in_hdr - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 ssl->in_buf_len < in_buf_new_len) {
356 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
357 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
358 } else {
359 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
360 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200361 modified = 1;
362 }
363 }
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 written_out = ssl->out_msg - ssl->out_buf;
368 iv_offset_out = ssl->out_iv - ssl->out_buf;
369 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200371 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 ssl->out_buf_len < out_buf_new_len) {
373 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
374 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
375 } else {
376 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
377 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 modified = 1;
379 }
380 }
381 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000384 ssl->in_hdr = ssl->in_buf + hdr_in;
385 mbedtls_ssl_update_in_pointers(ssl);
386 mbedtls_ssl_reset_out_pointers(ssl);
387
Andrzej Kurek4a063792020-10-21 15:08:44 +0200388 /* Fields below might not be properly updated with record
389 * splitting or with CID, so they are manually updated here. */
390 ssl->out_msg = ssl->out_buf + written_out;
391 ssl->out_len = ssl->out_buf + len_offset_out;
392 ssl->out_iv = ssl->out_buf + iv_offset_out;
393
394 ssl->in_msg = ssl->in_buf + written_in;
395 ssl->in_len = ssl->in_buf + len_offset_in;
396 ssl->in_iv = ssl->in_buf + iv_offset_in;
397 }
398}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500399#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
400
Jerry Yudb8c48a2022-01-27 14:54:54 +0800401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800402
403#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
405 const char *label,
406 const unsigned char *random, size_t rlen,
407 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800410
411#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
412
413/* Type for the TLS PRF */
414typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
415 const unsigned char *, size_t,
416 unsigned char *, size_t);
417
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200418MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
420 int ciphersuite,
421 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200422#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200424#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 ssl_tls_prf_t tls_prf,
426 const unsigned char randbytes[64],
427 mbedtls_ssl_protocol_version tls_version,
428 unsigned endpoint,
429 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800430
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100431#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200432MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100433static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300434 const char *label,
435 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100437static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
438static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100439
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100440#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100441
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100442#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100443MBEDTLS_CHECK_RETURN_CRITICAL
444static int tls_prf_sha384(const unsigned char *secret, size_t slen,
445 const char *label,
446 const unsigned char *random, size_t rlen,
447 unsigned char *dstbuf, size_t dlen);
448
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100449static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
450static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100451#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453MBEDTLS_CHECK_RETURN_CRITICAL
454static int ssl_tls12_session_load(mbedtls_ssl_session *session,
455 const unsigned char *buf,
456 size_t len);
457#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
458
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100459static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100460
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100461#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100462static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100463#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100464
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100465#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100466static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100467#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100468
469int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
470 const unsigned char *secret, size_t slen,
471 const char *label,
472 const unsigned char *random, size_t rlen,
473 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300474{
475 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100479#if defined(PSA_WANT_ALG_SHA_384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 case MBEDTLS_SSL_TLS_PRF_SHA384:
481 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 break;
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100483#endif /* PSA_WANT_ALG_SHA_384*/
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100484#if defined(PSA_WANT_ALG_SHA_256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300485 case MBEDTLS_SSL_TLS_PRF_SHA256:
486 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 break;
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100488#endif /* PSA_WANT_ALG_SHA_256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300489#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 default:
491 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300495}
496
Jerry Yuc73c6182022-02-08 20:29:25 +0800497#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800499{
500#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (session->peer_cert != NULL) {
502 mbedtls_x509_crt_free(session->peer_cert);
503 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800504 session->peer_cert = NULL;
505 }
506#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800508 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800510 session->peer_cert_digest = NULL;
511 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
512 session->peer_cert_digest_len = 0;
513 }
514#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
515}
516#endif /* MBEDTLS_X509_CRT_PARSE_C */
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800519{
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800521 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800592
593 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595
596 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800598
Jan Bruckner151f6422023-02-10 12:45:19 +0100599 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
600 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
601
Jerry Yu7a485c12022-10-31 13:08:18 +0800602 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800604
605 }
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800611{
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800613}
614
Jerry Yud25cab02022-10-31 12:48:30 +0800615#if defined(MBEDTLS_DEBUG_C)
616static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800617 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800618 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
619 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
620 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
621 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
622 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
623 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
624 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
625 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
626 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
627 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
628 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
629 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
630 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
631 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
632 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
633 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
634 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
635 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
636 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
637 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
639 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
640 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
641 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
642 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
643 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100644 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
645 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800646};
647
Chien Wong4e9683e2023-12-28 17:07:43 +0800648static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800649 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
650 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
651 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
652 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
653 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
655 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
656 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
657 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
658 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
659 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
660 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
661 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
662 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
663 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
664 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
665 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
666 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
667 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
668 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
669 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
670 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
671 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
672 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
673 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
674 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
675 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100676 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
677 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800678};
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800681{
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return extension_name_table[
683 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800684}
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800687{
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800701 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800703 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800705}
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
708 int level, const char *file, int line,
709 int hs_msg_type, unsigned int extension_type,
710 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800711{
712 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800714 mbedtls_debug_print_msg(
715 ssl, level, file, line,
716 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 ssl_tls13_get_hs_msg_name(hs_msg_type),
718 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800719 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800721 return;
722 }
723
724 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800726 mbedtls_debug_print_msg(
727 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
729 mbedtls_ssl_get_extension_name(extension_type), extension_type,
730 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800731 return;
732 }
733
734 mbedtls_debug_print_msg(
735 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
737 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800738}
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
741 int level, const char *file, int line,
742 int hs_msg_type, uint32_t extensions_mask,
743 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800744{
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 for (unsigned i = 0;
747 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
748 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800749 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800750 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800752 }
753}
754
Pengyu Lvee455c02023-01-12 14:37:24 +0800755#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800756static const char *ticket_flag_name_table[] =
757{
758 [0] = "ALLOW_PSK_RESUMPTION",
759 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
760 [3] = "ALLOW_EARLY_DATA",
761};
762
Pengyu Lv4938a562023-01-16 11:28:49 +0800763void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
764 int level, const char *file, int line,
765 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800766{
767 size_t i;
768
769 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800770 "print ticket_flags (0x%02x)", flags);
771
772 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800773
774 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800775 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800776 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
777 ticket_flag_name_table[i]);
778 }
779 }
780}
781#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
782
Jerry Yud25cab02022-10-31 12:48:30 +0800783#endif /* MBEDTLS_DEBUG_C */
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
786 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800787{
788 ((void) ciphersuite_info);
789
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100790#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800792 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800794#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100795#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800797 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800799#endif
800 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800802 return;
803 }
804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100807 unsigned hs_type,
808 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100809{
810 unsigned char hs_hdr[4];
811
812 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
814 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
815 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
816 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100818 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100819}
820
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100821int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100822 unsigned hs_type,
823 unsigned char const *msg,
824 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100825{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100826 int ret;
827 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100828 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100829 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100830 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100831 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100832}
833
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100834int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800835{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100836#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100837 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100838#if defined(MBEDTLS_USE_PSA_CRYPTO)
839 psa_status_t status;
840#else
841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
842#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100843#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800844 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100845#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100846#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100848 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
849 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200850 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100851 }
852 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
853 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200854 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100855 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800856#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100857 mbedtls_md_free(&ssl->handshake->fin_sha256);
858 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100859 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
860 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
861 0);
862 if (ret != 0) {
863 return ret;
864 }
865 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100866 if (ret != 0) {
867 return ret;
868 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#endif
870#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100871#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800872#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100873 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
874 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200875 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100876 }
877 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
878 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200879 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100880 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800881#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100882 mbedtls_md_free(&ssl->handshake->fin_sha384);
883 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100884 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
885 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
886 if (ret != 0) {
887 return ret;
888 }
889 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100890 if (ret != 0) {
891 return ret;
892 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800893#endif
894#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100895 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800896}
897
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100898static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100899 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800900{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100901#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100902 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100903#if defined(MBEDTLS_USE_PSA_CRYPTO)
904 psa_status_t status;
905#else
906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
907#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100908#else /* SHA-256 or SHA-384 */
909 ((void) ssl);
910 (void) buf;
911 (void) len;
912#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100913#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800914#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100915 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
916 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200917 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100918 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800919#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100920 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100921 if (ret != 0) {
922 return ret;
923 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800924#endif
925#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100926#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
929 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200930 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100931 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800932#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100933 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100934 if (ret != 0) {
935 return ret;
936 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800937#endif
938#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100939 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800940}
941
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100942#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100943static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100944 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800945{
946#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200947 return mbedtls_md_error_from_psa(psa_hash_update(
948 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800949#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100950 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800951#endif
952}
953#endif
954
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100955#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100956static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100957 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800958{
959#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200960 return mbedtls_md_error_from_psa(psa_hash_update(
961 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800962#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100963 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800964#endif
965}
966#endif
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100972#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500973#if defined(MBEDTLS_USE_PSA_CRYPTO)
974 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500975#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100976 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200977#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500978#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100979#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500980#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500981 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500982#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100983 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500985#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200986
987 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200991#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200992#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200993 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200995#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200996#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200997#if defined(MBEDTLS_USE_PSA_CRYPTO)
998 handshake->psa_pake_ctx = psa_pake_operation_init();
999 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1000#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001002#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001003#if defined(MBEDTLS_SSL_CLI_C)
1004 handshake->ecjpake_cache = NULL;
1005 handshake->ecjpake_cache_len = 0;
1006#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001007#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001008
Gilles Peskineeccd8882020-03-10 12:19:08 +01001009#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001011#endif
1012
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001013#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1014 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1015#endif
Hanno Becker75173122019-02-06 16:18:31 +00001016
1017#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1018 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001020#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001021}
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001024{
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001026
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
1028 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1029 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001030#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1032 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001033#endif
1034
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001035#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001036#if defined(MBEDTLS_USE_PSA_CRYPTO)
1037 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1038 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001039#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 mbedtls_md_init(&transform->md_ctx_enc);
1041 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001042#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001043#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001044}
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001047{
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001049}
1050
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001052static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001053{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1055
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001057#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ssl->transform_negotiate) {
1059 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1060 }
Jerry Yu2e199812022-12-01 18:57:19 +08001061#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (ssl->session_negotiate) {
1063 mbedtls_ssl_session_free(ssl->session_negotiate);
1064 }
1065 if (ssl->handshake) {
1066 mbedtls_ssl_handshake_free(ssl);
1067 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001068
Jerry Yu2e199812022-12-01 18:57:19 +08001069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001070 /*
1071 * Either the pointers are now NULL or cleared properly and can be freed.
1072 * Now allocate missing structures.
1073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (ssl->transform_negotiate == NULL) {
1075 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001076 }
Jerry Yu2e199812022-12-01 18:57:19 +08001077#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (ssl->session_negotiate == NULL) {
1080 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001081 }
Paul Bakker48916f92012-09-16 19:57:18 +00001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (ssl->handshake == NULL) {
1084 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001085 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001086#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1087 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1090 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001091#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001092
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001093 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001095#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001096 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001097#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 ssl->session_negotiate == NULL) {
1099 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001102 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001103
1104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001106 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001107#endif
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001110 ssl->session_negotiate = NULL;
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001113 }
1114
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001115#if defined(MBEDTLS_SSL_EARLY_DATA)
1116#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001117 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001118#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001119#if defined(MBEDTLS_SSL_SRV_C)
1120 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1121#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001122 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001123#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001124
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001125 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_ssl_session_init(ssl->session_negotiate);
1127 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001128
Jerry Yu2e199812022-12-01 18:57:19 +08001129#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001131#endif
1132
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001133 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001134 ret = mbedtls_ssl_reset_checksum(ssl);
1135 if (ret != 0) {
1136 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1137 return ret;
1138 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001139
Jerry Yud0766ec2022-09-22 10:46:57 +08001140#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1141 defined(MBEDTLS_SSL_SRV_C) && \
1142 defined(MBEDTLS_SSL_SESSION_TICKETS)
1143 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001145#endif
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001149 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001152 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001154 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001158 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001159#endif
1160
Brett Warrene0edc842021-08-17 09:53:13 +01001161/*
1162 * curve_list is translated to IANA TLS group identifiers here because
1163 * mbedtls_ssl_conf_curves returns void and so can't return
1164 * any error codes.
1165 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001166#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001167#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1168 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001170 size_t length;
1171 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1172
Thomas Daubney850a0792023-05-22 12:05:03 +01001173 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Brett Warrene0edc842021-08-17 09:53:13 +01001175
1176 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1178 if (group_list == NULL) {
1179 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1180 }
Brett Warrene0edc842021-08-17 09:53:13 +01001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001183 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 curve_list[i]);
1185 if (tls_id == 0) {
1186 mbedtls_free(group_list);
1187 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001188 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001189 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001190 }
1191
1192 group_list[length] = 0;
1193
1194 ssl->handshake->group_list = group_list;
1195 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001197 ssl->handshake->group_list = ssl->conf->group_list;
1198 ssl->handshake->group_list_heap_allocated = 0;
1199 }
1200#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001201#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001202
Ronald Crone68ab4f2022-10-05 12:46:29 +02001203#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001204#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001205#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001206 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1207 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1209 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001210 const int *md;
1211 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001212 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001213 uint16_t *p;
1214
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001215 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1216 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1217 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1220 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001223#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001225#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001226
Jerry Yub476a442022-01-21 18:14:45 +08001227#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001229#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1231 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1232 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001233 }
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1236 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1237 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1240 sizeof(uint16_t));
1241 if (ssl->handshake->sig_algs == NULL) {
1242 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1243 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 p = (uint16_t *) ssl->handshake->sig_algs;
1246 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1247 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1248 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001249 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001251#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001253 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001254#endif
1255#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001257 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001258#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001259 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001260 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001261 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001263#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001264 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001265 ssl->handshake->sig_algs_heap_allocated = 0;
1266 }
Jerry Yucc539102022-06-27 16:27:35 +08001267#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001268#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001270}
1271
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001272#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001273/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001275static int ssl_cookie_write_dummy(void *ctx,
1276 unsigned char **p, unsigned char *end,
1277 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001278{
1279 ((void) ctx);
1280 ((void) p);
1281 ((void) end);
1282 ((void) cli_id);
1283 ((void) cli_id_len);
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286}
1287
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001288MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001289static int ssl_cookie_check_dummy(void *ctx,
1290 const unsigned char *cookie, size_t cookie_len,
1291 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001292{
1293 ((void) ctx);
1294 ((void) cookie);
1295 ((void) cookie_len);
1296 ((void) cli_id);
1297 ((void) cli_id_len);
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001300}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001301#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303/*
1304 * Initialize an SSL context
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001307{
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001309}
1310
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001311MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001312static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001313{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001314 const mbedtls_ssl_config *conf = ssl->conf;
1315
Ronald Cron6f135e12021-12-08 16:57:54 +01001316#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1318 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1319 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1320 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001321 }
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1324 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001325 }
1326#endif
1327
1328#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1330 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1331 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001332 }
1333#endif
1334
Ronald Cron6f135e12021-12-08 16:57:54 +01001335#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1337 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1338 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1339 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1343 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001344 }
1345#endif
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1348 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001349}
1350
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001351MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001352static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1353{
1354 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 ret = ssl_conf_version_check(ssl);
1356 if (ret != 0) {
1357 return ret;
1358 }
Jerry Yu60835a82021-08-04 10:13:52 +08001359
Yanray Wangb422cab2023-12-01 16:18:10 +08001360 if (ssl->conf->f_rng == NULL) {
1361 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1362 return MBEDTLS_ERR_SSL_NO_RNG;
1363 }
1364
Jerry Yu60835a82021-08-04 10:13:52 +08001365 /* Space for further checks */
1366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001368}
1369
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001370/*
1371 * Setup an SSL context
1372 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1375 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001376{
Janos Follath865b3eb2019-12-16 11:46:15 +00001377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001378 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1379 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001380
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001381 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if ((ret = ssl_conf_check(ssl)) != 0) {
1384 return ret;
1385 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001386 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001387
Paul Bakker62f2dee2012-09-28 07:31:51 +00001388 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001389 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001390 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001391
1392 /* Set to NULL in case of an error condition */
1393 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001394
Darryl Greenb33cc762019-11-28 14:29:44 +00001395#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1396 ssl->in_buf_len = in_buf_len;
1397#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1399 if (ssl->in_buf == NULL) {
1400 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001401 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001402 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001403 }
1404
Darryl Greenb33cc762019-11-28 14:29:44 +00001405#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1406 ssl->out_buf_len = out_buf_len;
1407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1409 if (ssl->out_buf == NULL) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001411 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001412 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 }
1414
Deomid rojer Ryabkov3dfe75e2025-01-26 10:43:42 +02001415 mbedtls_ssl_reset_in_pointers(ssl);
1416 mbedtls_ssl_reset_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001417
Johan Pascalb62bb512015-12-03 21:56:45 +01001418#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001420#endif
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001423 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001427
1428error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 mbedtls_free(ssl->in_buf);
1430 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001431
1432 ssl->conf = NULL;
1433
Darryl Greenb33cc762019-11-28 14:29:44 +00001434#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1435 ssl->in_buf_len = 0;
1436 ssl->out_buf_len = 0;
1437#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001438 ssl->in_buf = NULL;
1439 ssl->out_buf = NULL;
1440
1441 ssl->in_hdr = NULL;
1442 ssl->in_ctr = NULL;
1443 ssl->in_len = NULL;
1444 ssl->in_iv = NULL;
1445 ssl->in_msg = NULL;
1446
1447 ssl->out_hdr = NULL;
1448 ssl->out_ctr = NULL;
1449 ssl->out_len = NULL;
1450 ssl->out_iv = NULL;
1451 ssl->out_msg = NULL;
1452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001454}
1455
1456/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001457 * Reset an initialized and used SSL context for re-use while retaining
1458 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001459 *
1460 * If partial is non-zero, keep data in the input buffer and client ID.
1461 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1464 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001465{
Darryl Greenb33cc762019-11-28 14:29:44 +00001466#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1467 size_t in_buf_len = ssl->in_buf_len;
1468 size_t out_buf_len = ssl->out_buf_len;
1469#else
1470 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1471 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1472#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Hanno Beckerb0302c42021-08-03 09:39:42 +01001474#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1475 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001476#endif
1477
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001478 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001480
Deomid rojer Ryabkov3dfe75e2025-01-26 10:43:42 +02001481 mbedtls_ssl_reset_in_pointers(ssl);
1482 mbedtls_ssl_reset_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483
1484 /* Reset incoming message parsing */
1485 ssl->in_offt = NULL;
1486 ssl->nb_zero = 0;
1487 ssl->in_msgtype = 0;
1488 ssl->in_msglen = 0;
1489 ssl->in_hslen = 0;
Deomid rojer Ryabkovdd14c0a2025-02-13 13:41:51 +03001490 ssl->in_hsfraglen = 0;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001491 ssl->keep_current_message = 0;
1492 ssl->transform_in = NULL;
1493
1494#if defined(MBEDTLS_SSL_PROTO_DTLS)
1495 ssl->next_record_offset = 0;
1496 ssl->in_epoch = 0;
1497#endif
1498
1499 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001501 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001503 }
1504
Ronald Cronad8c17b2022-06-10 17:18:09 +02001505 ssl->send_alert = 0;
1506
Hanno Beckerb0302c42021-08-03 09:39:42 +01001507 /* Reset outgoing message writing */
1508 ssl->out_msgtype = 0;
1509 ssl->out_msglen = 0;
1510 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 memset(ssl->out_buf, 0, out_buf_len);
1512 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 ssl->transform_out = NULL;
1514
1515#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001517#endif
1518
XiaokangQian2b01dc32022-01-21 02:53:13 +00001519#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (ssl->transform) {
1521 mbedtls_ssl_transform_free(ssl->transform);
1522 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001523 ssl->transform = NULL;
1524 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001525#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1526
XiaokangQian2b01dc32022-01-21 02:53:13 +00001527#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 mbedtls_ssl_transform_free(ssl->transform_application);
1529 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001530 ssl->transform_application = NULL;
1531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001533#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1535 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001536 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001537#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1540 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001541 ssl->handshake->transform_handshake = NULL;
1542 }
1543
XiaokangQian2b01dc32022-01-21 02:53:13 +00001544#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001545}
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001548{
1549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1550
1551 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001552 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001555
1556 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_SSL_RENEGOTIATION)
1558 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001559 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001560
1561 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1563 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001566
Hanno Beckerb0302c42021-08-03 09:39:42 +01001567 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001568 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (ssl->session) {
1570 mbedtls_ssl_session_free(ssl->session);
1571 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001572 ssl->session = NULL;
1573 }
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001576 ssl->alpn_chosen = NULL;
1577#endif
1578
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001579#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001580 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001581#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001583#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (free_cli_id) {
1585 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001586 ssl->cli_id = NULL;
1587 ssl->cli_id_len = 0;
1588 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001589#endif
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if ((ret = ssl_handshake_init(ssl)) != 0) {
1592 return ret;
1593 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001596}
1597
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001598/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001599 * Reset an initialized and used SSL context for re-use while retaining
1600 * all application-set variables, function pointers and data.
1601 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001602int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001603{
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001605}
1606
1607/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 * SSL set accessors
1609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001612 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001613}
1614
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001617 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001618}
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001623 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001624}
1625#endif
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001629 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001630}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1635 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001636{
1637 ssl->disable_datagram_packing = !allow_packing;
1638}
1639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1641 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001642{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001643 conf->hs_timeout_min = min;
1644 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001645}
1646#endif
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651}
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001654void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1655 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1656 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001658 conf->f_vrfy = f_vrfy;
1659 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1664 int (*f_rng)(void *, unsigned char *, size_t),
1665 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001666{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001667 conf->f_rng = f_rng;
1668 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001669}
1670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1672 void (*f_dbg)(void *, int, const char *, int, const char *),
1673 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001675 conf->f_dbg = f_dbg;
1676 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677}
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1680 void *p_bio,
1681 mbedtls_ssl_send_t *f_send,
1682 mbedtls_ssl_recv_t *f_recv,
1683 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001684{
1685 ssl->p_bio = p_bio;
1686 ssl->f_send = f_send;
1687 ssl->f_recv = f_recv;
1688 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001689}
1690
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001692void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001693{
1694 ssl->mtu = mtu;
1695}
1696#endif
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001699{
1700 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001701}
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1704 void *p_timer,
1705 mbedtls_ssl_set_timer_t *f_set_timer,
1706 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001707{
1708 ssl->p_timer = p_timer;
1709 ssl->f_set_timer = f_set_timer;
1710 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001711
1712 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001714}
1715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001717void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1718 void *p_cache,
1719 mbedtls_ssl_cache_get_t *f_get_cache,
1720 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001721{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001722 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001723 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001724 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001729int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001730{
Janos Follath865b3eb2019-12-16 11:46:15 +00001731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001734 session == NULL ||
1735 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1737 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001738 }
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if (ssl->handshake->resume == 1) {
1741 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1742 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001743
Jerry Yu21092062022-10-10 21:21:31 +08001744#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron233fcaa2024-04-04 14:05:21 +02001746#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001747 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001751 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1753 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1754 session->ciphersuite));
1755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001756 }
Ronald Cron233fcaa2024-04-04 14:05:21 +02001757#else
1758 /*
1759 * If session tickets are not enabled, it is not possible to resume a
1760 * TLS 1.3 session, thus do not make any change to the SSL context in
1761 * the first place.
1762 */
1763 return 0;
1764#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001765 }
Jerry Yu21092062022-10-10 21:21:31 +08001766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1769 session)) != 0) {
1770 return ret;
1771 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001772
Paul Bakker0a597072012-09-25 21:55:46 +00001773 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1780 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001781{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001782 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783}
1784
Ronald Cron6f135e12021-12-08 16:57:54 +01001785#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1787 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001788{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001789 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001790}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001791
1792#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001793void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1794 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001795{
1796 conf->early_data_enabled = early_data_enabled;
1797}
Jerry Yucc4e0072022-11-22 17:22:22 +08001798
1799#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001800void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001802{
Jerry Yu39da9852022-12-06 16:58:36 +08001803 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001804}
1805#endif /* MBEDTLS_SSL_SRV_C */
1806
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001807#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001808#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001811void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1812 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001813{
1814 conf->cert_profile = profile;
1815}
1816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001818{
1819 mbedtls_ssl_key_cert *cur = key_cert, *next;
1820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001822 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 cur = next;
1825 }
1826}
1827
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001828/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001830static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1831 mbedtls_x509_crt *cert,
1832 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001833{
niisato8ee24222018-06-25 19:05:48 +09001834 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001837 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001839 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1844 if (new_cert == NULL) {
1845 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1846 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001847
niisato8ee24222018-06-25 19:05:48 +09001848 new_cert->cert = cert;
1849 new_cert->key = key;
1850 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001851
Glenn Strauss36872db2022-01-22 05:06:31 -05001852 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001854 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001856 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001858 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 }
niisato8ee24222018-06-25 19:05:48 +09001860 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001861 }
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001864}
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001867 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001869{
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001871}
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001874 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001876{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001877 conf->ca_chain = ca_chain;
1878 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001879
1880#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1881 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1882 * cannot be used together. */
1883 conf->f_ca_cb = NULL;
1884 conf->p_ca_cb = NULL;
1885#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001886}
Hanno Becker5adaad92019-03-27 16:54:37 +00001887
1888#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001889void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1890 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1891 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001892{
1893 conf->f_ca_cb = f_ca_cb;
1894 conf->p_ca_cb = p_ca_cb;
1895
1896 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1897 * cannot be used together. */
1898 conf->ca_chain = NULL;
1899 conf->ca_crl = NULL;
1900}
1901#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001903
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001904#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001905const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1906 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001907{
1908 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001910}
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1913 mbedtls_x509_crt *own_cert,
1914 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001915{
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1917 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001918}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1921 mbedtls_x509_crt *ca_chain,
1922 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001923{
1924 ssl->handshake->sni_ca_chain = ca_chain;
1925 ssl->handshake->sni_ca_crl = ca_crl;
1926}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001927
Glenn Strauss999ef702022-03-11 01:37:23 -05001928#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001929void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1930 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001931{
1932 ssl->handshake->dn_hints = crt;
1933}
1934#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1937 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001938{
1939 ssl->handshake->sni_authmode = authmode;
1940}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001941#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1942
Hanno Becker8927c832019-04-03 12:52:50 +01001943#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001944void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1945 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1946 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001947{
1948 ssl->f_vrfy = f_vrfy;
1949 ssl->p_vrfy = p_vrfy;
1950}
1951#endif
1952
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001953#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001954
Valerio Setti016f6822022-12-09 14:17:50 +01001955#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001956static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1957static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1958
Valerio Setti016f6822022-12-09 14:17:50 +01001959static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 mbedtls_ssl_context *ssl,
1961 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001962{
1963 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001964 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001965 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001966 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001967 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001968 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1970 psa_pake_cs_set_primitive(&cipher_suite,
1971 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1972 PSA_ECC_FAMILY_SECP_R1,
1973 256));
1974 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1977 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001978 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001982 user = jpake_server_id;
1983 user_len = sizeof(jpake_server_id);
1984 peer = jpake_client_id;
1985 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001987 user = jpake_client_id;
1988 user_len = sizeof(jpake_client_id);
1989 peer = jpake_server_id;
1990 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001992
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001993 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1994 if (status != PSA_SUCCESS) {
1995 return status;
1996 }
1997
1998 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002000 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 }
Valerio Setti016f6822022-12-09 14:17:50 +01002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2004 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002005 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 }
Valerio Setti016f6822022-12-09 14:17:50 +01002007
2008 ssl->handshake->psa_pake_ctx_is_ok = 1;
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002011}
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2014 const unsigned char *pw,
2015 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002016{
2017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2018 psa_status_t status;
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (ssl->handshake == NULL || ssl->conf == NULL) {
2021 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002022 }
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 /* Empty password is not valid */
2025 if ((pw == NULL) || (pw_len == 0)) {
2026 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2027 }
2028
2029 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2030 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2031 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2032
2033 status = psa_import_key(&attributes, pw, pw_len,
2034 &ssl->handshake->psa_pake_password);
2035 if (status != PSA_SUCCESS) {
2036 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2037 }
2038
2039 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2040 ssl->handshake->psa_pake_password);
2041 if (status != PSA_SUCCESS) {
2042 psa_destroy_key(ssl->handshake->psa_pake_password);
2043 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2044 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2045 }
2046
2047 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002048}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2051 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002052{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002053 psa_status_t status;
2054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (ssl->handshake == NULL || ssl->conf == NULL) {
2056 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002057 }
2058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (mbedtls_svc_key_id_is_null(pwd)) {
2060 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2061 }
2062
2063 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2064 if (status != PSA_SUCCESS) {
2065 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2066 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2067 }
2068
2069 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002070}
2071#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002072int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2073 const unsigned char *pw,
2074 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002075{
2076 mbedtls_ecjpake_role role;
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (ssl->handshake == NULL || ssl->conf == NULL) {
2079 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2080 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002081
Valerio Settic689ed82022-12-07 14:40:38 +01002082 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if ((pw == NULL) || (pw_len == 0)) {
2084 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2085 }
Valerio Settic689ed82022-12-07 14:40:38 +01002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002088 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002090 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2094 role,
2095 MBEDTLS_MD_SHA256,
2096 MBEDTLS_ECP_DP_SECP256R1,
2097 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002098}
Valerio Setti02c25b52022-11-15 14:08:42 +01002099#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002100#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002101
Ronald Cron73fe8df2022-10-05 14:31:43 +02002102#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002103int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002104{
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (conf->psk_identity == NULL ||
2106 conf->psk_identity_len == 0) {
2107 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002108 }
2109
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002110#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2112 return 1;
2113 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002114#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if (conf->psk != NULL && conf->psk_len != 0) {
2117 return 1;
2118 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002121}
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002124{
2125 /* Remove reference to existing PSK, if any. */
2126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002128 /* The maintenance of the PSK key slot is the
2129 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002130 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002134 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002135 conf->psk = NULL;
2136 conf->psk_len = 0;
2137 }
2138
2139 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (conf->psk_identity != NULL) {
2141 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002142 conf->psk_identity = NULL;
2143 conf->psk_identity_len = 0;
2144 }
2145}
2146
Hanno Becker7390c712018-11-15 13:33:04 +00002147/* This function assumes that PSK identity in the SSL config is unset.
2148 * It checks that the provided identity is well-formed and attempts
2149 * to make a copy of it in the SSL config.
2150 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002151MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002152static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2153 unsigned char const *psk_identity,
2154 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002155{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002156 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002158 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 (psk_identity_len >> 16) != 0 ||
2160 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002162 }
2163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2165 if (conf->psk_identity == NULL) {
2166 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2167 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002168
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002169 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002173}
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2176 const unsigned char *psk, size_t psk_len,
2177 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002178{
Janos Follath865b3eb2019-12-16 11:46:15 +00002179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002180
2181 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2183 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2184 }
Hanno Becker7390c712018-11-15 13:33:04 +00002185
2186 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (psk == NULL) {
2188 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2189 }
2190 if (psk_len == 0) {
2191 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2192 }
2193 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2194 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2195 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2198 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2199 }
Hanno Becker7390c712018-11-15 13:33:04 +00002200 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002202
2203 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2205 if (ret != 0) {
2206 ssl_conf_remove_psk(conf);
2207 }
Hanno Becker7390c712018-11-15 13:33:04 +00002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002210}
2211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002213{
2214#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002216 /* The maintenance of the external PSK key slot is the
2217 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (ssl->handshake->psk_opaque_is_internal) {
2219 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002220 ssl->handshake->psk_opaque_is_internal = 0;
2221 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002222 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002223 }
Neil Armstronge952a302022-05-03 10:22:14 +02002224#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002226 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002228 ssl->handshake->psk_len = 0;
lhuang0454adeab2024-06-10 12:16:29 -07002229 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002230 }
Neil Armstronge952a302022-05-03 10:22:14 +02002231#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002232}
2233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2235 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002236{
Neil Armstrong501c9322022-05-03 09:35:09 +02002237#if defined(MBEDTLS_USE_PSA_CRYPTO)
2238 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002240 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002242#endif /* MBEDTLS_USE_PSA_CRYPTO */
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (psk == NULL || ssl->handshake == NULL) {
2245 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2246 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2249 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2250 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002253
Neil Armstrong501c9322022-05-03 09:35:09 +02002254#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002255#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2257 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2258 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2259 } else {
2260 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2261 }
2262 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002263 }
2264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002265
Jerry Yu568ec252022-07-22 21:27:34 +08002266#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2268 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2269 psa_set_key_usage_flags(&key_attributes,
2270 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002271 }
2272#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 psa_set_key_algorithm(&key_attributes, alg);
2275 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2278 if (status != PSA_SUCCESS) {
2279 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2280 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002281
2282 /* Allow calling psa_destroy_key() on psk remove */
2283 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002285#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2287 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2288 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002289
2290 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002294#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002295}
2296
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002297#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002298int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2299 mbedtls_svc_key_id_t psk,
2300 const unsigned char *psk_identity,
2301 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002302{
Janos Follath865b3eb2019-12-16 11:46:15 +00002303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002304
2305 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2308 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002309
Hanno Becker7390c712018-11-15 13:33:04 +00002310 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (mbedtls_svc_key_id_is_null(psk)) {
2312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2313 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002314 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002315
2316 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2318 psk_identity_len);
2319 if (ret != 0) {
2320 ssl_conf_remove_psk(conf);
2321 }
Hanno Becker7390c712018-11-15 13:33:04 +00002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002324}
2325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2327 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002328{
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if ((mbedtls_svc_key_id_is_null(psk)) ||
2330 (ssl->handshake == NULL)) {
2331 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2332 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002335 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002337}
2338#endif /* MBEDTLS_USE_PSA_CRYPTO */
2339
Jerry Yu8897c072022-08-12 13:56:53 +08002340#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002341void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2342 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2343 size_t),
2344 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002345{
2346 conf->f_psk = f_psk;
2347 conf->p_psk = p_psk;
2348}
Jerry Yu8897c072022-08-12 13:56:53 +08002349#endif /* MBEDTLS_SSL_SRV_C */
2350
Ronald Cron73fe8df2022-10-05 14:31:43 +02002351#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002352
2353#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002354static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002357#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (alg == PSA_ALG_CBC_NO_PADDING) {
2359 return MBEDTLS_SSL_MODE_CBC;
2360 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (PSA_ALG_IS_AEAD(alg)) {
2363 return MBEDTLS_SSL_MODE_AEAD;
2364 }
2365 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002366}
2367
2368#else /* MBEDTLS_USE_PSA_CRYPTO */
2369
2370static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002372{
2373#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (mode == MBEDTLS_MODE_CBC) {
2375 return MBEDTLS_SSL_MODE_CBC;
2376 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002377#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2378
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002379#if defined(MBEDTLS_GCM_C) || \
2380 defined(MBEDTLS_CCM_C) || \
2381 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002383 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 mode == MBEDTLS_MODE_CHACHAPOLY) {
2385 return MBEDTLS_SSL_MODE_AEAD;
2386 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002387#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002390}
Gilles Peskine301711e2022-04-26 16:57:05 +02002391#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002392
Gilles Peskinee108d982022-04-26 16:50:40 +02002393static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2394 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002396{
2397#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2399 base_mode == MBEDTLS_SSL_MODE_CBC) {
2400 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002401 }
2402#else
2403 (void) encrypt_then_mac;
2404#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002406}
2407
Neil Armstrongab555e02022-04-04 11:07:59 +02002408mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002410{
Gilles Peskinee108d982022-04-26 16:50:40 +02002411 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002414#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002416#endif
2417 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002418
Gilles Peskinee108d982022-04-26 16:50:40 +02002419 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002420#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002421 encrypt_then_mac = transform->encrypt_then_mac;
2422#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002424}
2425
Neil Armstrongab555e02022-04-04 11:07:59 +02002426mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002429#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002431{
Gilles Peskinee108d982022-04-26 16:50:40 +02002432 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2433
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002434#if defined(MBEDTLS_USE_PSA_CRYPTO)
2435 psa_status_t status;
2436 psa_algorithm_t alg;
2437 psa_key_type_t type;
2438 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002439 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2440 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (status == PSA_SUCCESS) {
2442 base_mode = mbedtls_ssl_get_base_mode(alg);
2443 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002444#else
2445 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002446 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002448 base_mode =
2449 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002451 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002452#endif /* MBEDTLS_USE_PSA_CRYPTO */
2453
Gilles Peskinee108d982022-04-26 16:50:40 +02002454#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2455 int encrypt_then_mac = 0;
2456#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002458}
2459
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002460#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002461
Ronald Cron8b592d22024-11-12 18:08:25 +01002462const mbedtls_error_pair_t psa_to_ssl_errors[] =
2463{
2464 { PSA_SUCCESS, 0 },
2465 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
2466 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
2467 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
2468 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
2469 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
2470 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
2471};
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2474 size_t taglen,
2475 psa_algorithm_t *alg,
2476 psa_key_type_t *key_type,
2477 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002478{
Elena Uziunaitec2561722024-07-05 11:37:33 +01002479#if !defined(PSA_WANT_ALG_CCM)
Yanray Wang19e4dc82023-11-16 18:02:05 +08002480 (void) taglen;
2481#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 switch (mbedtls_cipher_type) {
Elena Uziunaite74342c72024-07-05 11:31:29 +01002483#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002484 case MBEDTLS_CIPHER_AES_128_CBC:
2485 *alg = PSA_ALG_CBC_NO_PADDING;
2486 *key_type = PSA_KEY_TYPE_AES;
2487 *key_size = 128;
2488 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002489#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002490#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002491 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002493 *key_type = PSA_KEY_TYPE_AES;
2494 *key_size = 128;
2495 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002496#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002497#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002498 case MBEDTLS_CIPHER_AES_128_GCM:
2499 *alg = PSA_ALG_GCM;
2500 *key_type = PSA_KEY_TYPE_AES;
2501 *key_size = 128;
2502 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002503#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002504#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002505 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002507 *key_type = PSA_KEY_TYPE_AES;
2508 *key_size = 192;
2509 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002510#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002511#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002512 case MBEDTLS_CIPHER_AES_192_GCM:
2513 *alg = PSA_ALG_GCM;
2514 *key_type = PSA_KEY_TYPE_AES;
2515 *key_size = 192;
2516 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002517#endif
Elena Uziunaite74342c72024-07-05 11:31:29 +01002518#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002519 case MBEDTLS_CIPHER_AES_256_CBC:
2520 *alg = PSA_ALG_CBC_NO_PADDING;
2521 *key_type = PSA_KEY_TYPE_AES;
2522 *key_size = 256;
2523 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002524#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002525#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002526 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002528 *key_type = PSA_KEY_TYPE_AES;
2529 *key_size = 256;
2530 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002531#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002532#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002533 case MBEDTLS_CIPHER_AES_256_GCM:
2534 *alg = PSA_ALG_GCM;
2535 *key_type = PSA_KEY_TYPE_AES;
2536 *key_size = 256;
2537 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002538#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002539#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002540 case MBEDTLS_CIPHER_ARIA_128_CBC:
2541 *alg = PSA_ALG_CBC_NO_PADDING;
2542 *key_type = PSA_KEY_TYPE_ARIA;
2543 *key_size = 128;
2544 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002545#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002546#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002547 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002549 *key_type = PSA_KEY_TYPE_ARIA;
2550 *key_size = 128;
2551 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002552#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002553#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002554 case MBEDTLS_CIPHER_ARIA_128_GCM:
2555 *alg = PSA_ALG_GCM;
2556 *key_type = PSA_KEY_TYPE_ARIA;
2557 *key_size = 128;
2558 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002559#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002560#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002561 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002563 *key_type = PSA_KEY_TYPE_ARIA;
2564 *key_size = 192;
2565 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002566#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002567#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002568 case MBEDTLS_CIPHER_ARIA_192_GCM:
2569 *alg = PSA_ALG_GCM;
2570 *key_type = PSA_KEY_TYPE_ARIA;
2571 *key_size = 192;
2572 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002573#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002574#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002575 case MBEDTLS_CIPHER_ARIA_256_CBC:
2576 *alg = PSA_ALG_CBC_NO_PADDING;
2577 *key_type = PSA_KEY_TYPE_ARIA;
2578 *key_size = 256;
2579 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002580#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002581#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002582 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002584 *key_type = PSA_KEY_TYPE_ARIA;
2585 *key_size = 256;
2586 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002587#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002588#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002589 case MBEDTLS_CIPHER_ARIA_256_GCM:
2590 *alg = PSA_ALG_GCM;
2591 *key_type = PSA_KEY_TYPE_ARIA;
2592 *key_size = 256;
2593 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002594#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002595#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002596 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2597 *alg = PSA_ALG_CBC_NO_PADDING;
2598 *key_type = PSA_KEY_TYPE_CAMELLIA;
2599 *key_size = 128;
2600 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002601#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002602#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002603 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002605 *key_type = PSA_KEY_TYPE_CAMELLIA;
2606 *key_size = 128;
2607 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002608#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002609#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002610 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2611 *alg = PSA_ALG_GCM;
2612 *key_type = PSA_KEY_TYPE_CAMELLIA;
2613 *key_size = 128;
2614 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002615#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002616#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002617 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002619 *key_type = PSA_KEY_TYPE_CAMELLIA;
2620 *key_size = 192;
2621 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002622#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002623#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002624 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2625 *alg = PSA_ALG_GCM;
2626 *key_type = PSA_KEY_TYPE_CAMELLIA;
2627 *key_size = 192;
2628 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002629#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002630#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002631 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2632 *alg = PSA_ALG_CBC_NO_PADDING;
2633 *key_type = PSA_KEY_TYPE_CAMELLIA;
2634 *key_size = 256;
2635 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002636#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002637#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002638 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002640 *key_type = PSA_KEY_TYPE_CAMELLIA;
2641 *key_size = 256;
2642 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002643#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002644#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002645 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2646 *alg = PSA_ALG_GCM;
2647 *key_type = PSA_KEY_TYPE_CAMELLIA;
2648 *key_size = 256;
2649 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002650#endif
Elena Uziunaite5c70c302024-07-05 11:44:44 +01002651#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002652 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2653 *alg = PSA_ALG_CHACHA20_POLY1305;
2654 *key_type = PSA_KEY_TYPE_CHACHA20;
2655 *key_size = 256;
2656 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002657#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002658 case MBEDTLS_CIPHER_NULL:
2659 *alg = MBEDTLS_SSL_NULL_CIPHER;
2660 *key_type = 0;
2661 *key_size = 0;
2662 break;
2663 default:
2664 return PSA_ERROR_NOT_SUPPORTED;
2665 }
2666
2667 return PSA_SUCCESS;
2668}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002669#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002670
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002671#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002672int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2673 const unsigned char *dhm_P, size_t P_len,
2674 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002675{
Janos Follath865b3eb2019-12-16 11:46:15 +00002676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 mbedtls_mpi_free(&conf->dhm_P);
2679 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2682 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2683 mbedtls_mpi_free(&conf->dhm_P);
2684 mbedtls_mpi_free(&conf->dhm_G);
2685 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002686 }
2687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002689}
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002692{
Janos Follath865b3eb2019-12-16 11:46:15 +00002693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 mbedtls_mpi_free(&conf->dhm_P);
2696 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2699 &conf->dhm_P)) != 0 ||
2700 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2701 &conf->dhm_G)) != 0) {
2702 mbedtls_mpi_free(&conf->dhm_P);
2703 mbedtls_mpi_free(&conf->dhm_G);
2704 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002705 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002708}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002709#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002710
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002711#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2712/*
2713 * Set the minimum length for Diffie-Hellman parameters
2714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002715void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2716 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002717{
2718 conf->dhm_min_bitlen = bitlen;
2719}
2720#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2721
Ronald Crone68ab4f2022-10-05 12:46:29 +02002722#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002723#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002724/*
2725 * Set allowed/preferred hashes for handshake signatures
2726 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2728 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002729{
2730 conf->sig_hashes = hashes;
2731}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002732#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002733
Jerry Yuf017ee42022-01-12 15:49:48 +08002734/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2736 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002737{
Jerry Yuf017ee42022-01-12 15:49:48 +08002738#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2739 conf->sig_hashes = NULL;
2740#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2741 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002742}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002743#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002744
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002745#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002746#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002747/*
2748 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002749 *
2750 * mbedtls_ssl_setup() takes the provided list
2751 * and translates it to a list of IANA TLS group identifiers,
2752 * stored in ssl->handshake->group_list.
2753 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002754 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002755void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2756 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002758 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002759 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002760}
Brett Warrene0edc842021-08-17 09:53:13 +01002761#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002762#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002763
Brett Warrene0edc842021-08-17 09:53:13 +01002764/*
2765 * Set the allowed groups
2766 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2768 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002769{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002770#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002771 conf->curve_list = NULL;
2772#endif
2773 conf->group_list = group_list;
2774}
2775
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002776#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002777int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002778{
Hanno Becker947194e2017-04-07 13:25:49 +01002779 /* Initialize to suppress unnecessary compiler warning */
2780 size_t hostname_len = 0;
2781
2782 /* Check if new hostname is valid before
2783 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 if (hostname != NULL) {
2785 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002786
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2788 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2789 }
Hanno Becker947194e2017-04-07 13:25:49 +01002790 }
2791
2792 /* Now it's clear that we will overwrite the old hostname,
2793 * so we can free it safely */
2794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002796 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002797 }
2798
2799 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002802 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 } else {
2804 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2805 if (ssl->hostname == NULL) {
2806 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2807 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002810
Hanno Becker947194e2017-04-07 13:25:49 +01002811 ssl->hostname[hostname_len] = '\0';
2812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002815}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002816#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002817
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002818#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002819void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2820 int (*f_sni)(void *, mbedtls_ssl_context *,
2821 const unsigned char *, size_t),
2822 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002823{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002824 conf->f_sni = f_sni;
2825 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002830int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002831{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002832 size_t cur_len, tot_len;
2833 const char **p;
2834
2835 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002836 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2837 * MUST NOT be truncated."
2838 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002839 */
2840 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 for (p = protos; *p != NULL; p++) {
2842 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002843 tot_len += cur_len;
2844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if ((cur_len == 0) ||
2846 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2847 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2848 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2849 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002850 }
2851
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002852 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002855}
2856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002858{
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002860}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002862
Johan Pascalb62bb512015-12-03 21:56:45 +01002863#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002864void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2865 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002866{
2867 conf->dtls_srtp_mki_support = support_mki_value;
2868}
2869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2871 unsigned char *mki_value,
2872 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002873{
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2875 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002876 }
Ron Eldor591f1622018-01-22 12:30:04 +02002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2879 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002880 }
Ron Eldor591f1622018-01-22 12:30:04 +02002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002883 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002885}
2886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2888 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002889{
Johan Pascal253d0262020-09-22 13:04:45 +02002890 const mbedtls_ssl_srtp_profile *p;
2891 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002892
Johan Pascal253d0262020-09-22 13:04:45 +02002893 /* check the profiles list: all entry must be valid,
2894 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2896 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2897 p++) {
2898 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002899 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002901 /* unsupported value, stop parsing and set the size to an error value */
2902 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002903 }
2904 }
2905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2907 conf->dtls_srtp_profile_list = NULL;
2908 conf->dtls_srtp_profile_list_len = 0;
2909 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002910 }
2911
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002912 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002913 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002916}
2917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2919 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002920{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002921 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2922 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002924 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002926 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2928 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002929 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002930}
Johan Pascalb62bb512015-12-03 21:56:45 +01002931#endif /* MBEDTLS_SSL_DTLS_SRTP */
2932
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302933#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002934void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002935{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002936 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002937}
2938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002940{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002941 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002942}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302943#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002944
Janos Follath088ce432017-04-10 12:42:31 +01002945#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002946void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2947 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002948{
2949 conf->cert_req_ca_list = cert_req_ca_list;
2950}
2951#endif
2952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002954void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002955{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002956 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002957}
2958#endif
2959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002962{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002963 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002964}
2965#endif
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002968int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002969{
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2971 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2972 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002973 }
2974
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002975 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002982{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002983 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002984}
2985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002989 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002990}
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002994 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002995}
2996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2998 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002999{
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003005#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003006void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003007{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003008 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003009}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003010#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003011
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003012#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003013
Jerry Yud0766ec2022-09-22 10:46:57 +08003014#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003015void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3016 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003017{
Jerry Yud0766ec2022-09-22 10:46:57 +08003018 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003019}
3020#endif
3021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3023 mbedtls_ssl_ticket_write_t *f_ticket_write,
3024 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3025 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003026{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003027 conf->f_ticket_write = f_ticket_write;
3028 conf->f_ticket_parse = f_ticket_parse;
3029 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003030}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3035 mbedtls_ssl_export_keys_t *f_export_keys,
3036 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003037{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003038 ssl->f_export_keys = f_export_keys;
3039 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003040}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003041
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003042#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003043void mbedtls_ssl_conf_async_private_cb(
3044 mbedtls_ssl_config *conf,
3045 mbedtls_ssl_async_sign_t *f_async_sign,
3046 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3047 mbedtls_ssl_async_resume_t *f_async_resume,
3048 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003050{
3051 conf->f_async_sign_start = f_async_sign;
3052 conf->f_async_decrypt_start = f_async_decrypt;
3053 conf->f_async_resume = f_async_resume;
3054 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003055 conf->p_async_config_data = async_config_data;
3056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003059{
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003061}
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003064{
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (ssl->handshake == NULL) {
3066 return NULL;
3067 } else {
3068 return ssl->handshake->user_async_ctx;
3069 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003070}
3071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3073 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003074{
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003076 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003078}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003079#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003080
Paul Bakker5121ce52009-01-03 21:22:43 +00003081/*
3082 * SSL get accessors
3083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003085{
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (ssl->session != NULL) {
3087 return ssl->session->verify_result;
3088 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (ssl->session_negotiate != NULL) {
3091 return ssl->session_negotiate->verify_result;
3092 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003095}
3096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003098{
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (ssl == NULL || ssl->session == NULL) {
3100 return 0;
3101 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003107{
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl == NULL || ssl->session == NULL) {
3109 return NULL;
3110 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003111
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003113}
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3119 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003120 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003122 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003124 }
3125 }
3126#endif
3127
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 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 "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003131 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003133 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003135 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003136}
3137
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003138#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3139
3140size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3141{
3142 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3143 size_t record_size_limit = max_len;
3144
3145 if (ssl->session != NULL &&
3146 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3147 ssl->session->record_size_limit < max_len) {
3148 record_size_limit = ssl->session->record_size_limit;
3149 }
3150
3151 // TODO: this is currently untested
3152 /* During a handshake, use the value being negotiated */
3153 if (ssl->session_negotiate != NULL &&
3154 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3155 ssl->session_negotiate->record_size_limit < max_len) {
3156 record_size_limit = ssl->session_negotiate->record_size_limit;
3157 }
3158
3159 return record_size_limit;
3160}
3161#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3162
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003163#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003164size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003165{
David Horstmann95d516f2021-05-04 18:36:56 +01003166 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003167 size_t read_mfl;
3168
Jerry Yuddda0502022-12-01 19:43:12 +08003169#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003170 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3172 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3173 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003174 }
Jerry Yuddda0502022-12-01 19:43:12 +08003175#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003176
3177 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (ssl->session_out != NULL) {
3179 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3180 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003181 max_len = read_mfl;
3182 }
3183 }
3184
Jerry Yuddda0502022-12-01 19:43:12 +08003185 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if (ssl->session_negotiate != NULL) {
3187 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3188 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003189 max_len = read_mfl;
3190 }
3191 }
3192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003194}
3195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003197{
3198 size_t max_len;
3199
3200 /*
3201 * Assume mfl_code is correct since it was checked when set
3202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003204
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003205 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 if (ssl->session_out != NULL &&
3207 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3208 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003209 }
3210
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003211 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (ssl->session_negotiate != NULL &&
3213 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3214 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003215 }
3216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003218}
3219#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3220
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003221#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003222size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003223{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003224 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3226 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3227 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3228 return 0;
3229 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3232 return ssl->mtu;
3233 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 if (ssl->mtu == 0) {
3236 return ssl->handshake->mtu;
3237 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 return ssl->mtu < ssl->handshake->mtu ?
3240 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003241}
3242#endif /* MBEDTLS_SSL_PROTO_DTLS */
3243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003245{
3246 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3247
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003248#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003249 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003250 !defined(MBEDTLS_SSL_PROTO_DTLS)
3251 (void) ssl;
3252#endif
3253
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003258 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003260#endif
3261
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003262#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3263 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3264
3265 if (max_len > record_size_limit) {
3266 max_len = record_size_limit;
3267 }
3268#endif
3269
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003270 if (ssl->transform_out != NULL &&
3271 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003272 /*
3273 * In TLS 1.3 case, when records are protected, `max_len` as computed
3274 * above is the maximum length of the TLSInnerPlaintext structure that
3275 * along the plaintext payload contains the inner content type (one byte)
3276 * and some zero padding. Given the algorithm used for padding
3277 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3278 * the plaintext payload. Round down to a multiple of
3279 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3280 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003281 */
3282 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3283 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3284 }
3285
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3288 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3289 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003290 const size_t overhead = (size_t) ret;
3291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ret < 0) {
3293 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003294 }
3295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (mtu <= overhead) {
3297 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3298 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3299 }
3300
3301 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003304 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003305#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003306
Hanno Becker0defedb2018-08-10 12:35:02 +01003307#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003308 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3309 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003310 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003311#endif
3312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314}
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003317{
3318 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3319
3320#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3321 (void) ssl;
3322#endif
3323
3324#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003328 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003330#endif
3331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003333}
3334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003336const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003337{
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if (ssl == NULL || ssl->session == NULL) {
3339 return NULL;
3340 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003341
Hanno Beckere6824572019-02-07 13:18:46 +00003342#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003344#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003346#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003351int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3352 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003353{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003354 int ret;
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003357 dst == NULL ||
3358 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3360 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003361 }
3362
Hanno Beckere810bbc2021-05-14 16:01:05 +01003363 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3364 * idempotent: Each session can only be exported once.
3365 *
3366 * (This is in preparation for TLS 1.3 support where we will
3367 * need the ability to export multiple sessions (aka tickets),
3368 * which will be achieved by calling mbedtls_ssl_get_session()
3369 * multiple times until it fails.)
3370 *
3371 * Check whether we have already exported the current session,
3372 * and fail if so.
3373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 if (ssl->session->exported == 1) {
3375 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3376 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3379 if (ret != 0) {
3380 return ret;
3381 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003382
3383 /* Remember that we've exported the session. */
3384 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003388
David Horstmanncb01b362024-02-29 17:31:46 +00003389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3390
3391/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003392 *
David Horstmanncb01b362024-02-29 17:31:46 +00003393 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003394 */
3395static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3396 unsigned char *buf,
3397 size_t buf_len)
3398{
3399 unsigned char *p = buf;
3400 size_t used = 0;
3401
3402#if defined(MBEDTLS_HAVE_TIME)
3403 uint64_t start;
3404#endif
3405#if defined(MBEDTLS_X509_CRT_PARSE_C)
3406#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3407 size_t cert_len;
3408#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3409#endif /* MBEDTLS_X509_CRT_PARSE_C */
3410
3411 /*
3412 * Time
3413 */
3414#if defined(MBEDTLS_HAVE_TIME)
3415 used += 8;
3416
3417 if (used <= buf_len) {
3418 start = (uint64_t) session->start;
3419
3420 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3421 p += 8;
3422 }
3423#endif /* MBEDTLS_HAVE_TIME */
3424
3425 /*
3426 * Basic mandatory fields
3427 */
3428 used += 1 /* id_len */
3429 + sizeof(session->id)
3430 + sizeof(session->master)
3431 + 4; /* verify_result */
3432
3433 if (used <= buf_len) {
3434 *p++ = MBEDTLS_BYTE_0(session->id_len);
3435 memcpy(p, session->id, 32);
3436 p += 32;
3437
3438 memcpy(p, session->master, 48);
3439 p += 48;
3440
3441 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3442 p += 4;
3443 }
3444
3445 /*
3446 * Peer's end-entity certificate
3447 */
3448#if defined(MBEDTLS_X509_CRT_PARSE_C)
3449#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3450 if (session->peer_cert == NULL) {
3451 cert_len = 0;
3452 } else {
3453 cert_len = session->peer_cert->raw.len;
3454 }
3455
3456 used += 3 + cert_len;
3457
3458 if (used <= buf_len) {
3459 *p++ = MBEDTLS_BYTE_2(cert_len);
3460 *p++ = MBEDTLS_BYTE_1(cert_len);
3461 *p++ = MBEDTLS_BYTE_0(cert_len);
3462
3463 if (session->peer_cert != NULL) {
3464 memcpy(p, session->peer_cert->raw.p, cert_len);
3465 p += cert_len;
3466 }
3467 }
3468#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3469 if (session->peer_cert_digest != NULL) {
3470 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3471 if (used <= buf_len) {
3472 *p++ = (unsigned char) session->peer_cert_digest_type;
3473 *p++ = (unsigned char) session->peer_cert_digest_len;
3474 memcpy(p, session->peer_cert_digest,
3475 session->peer_cert_digest_len);
3476 p += session->peer_cert_digest_len;
3477 }
3478 } else {
3479 used += 2;
3480 if (used <= buf_len) {
3481 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3482 *p++ = 0;
3483 }
3484 }
3485#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3486#endif /* MBEDTLS_X509_CRT_PARSE_C */
3487
3488 /*
3489 * Session ticket if any, plus associated data
3490 */
3491#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3492#if defined(MBEDTLS_SSL_CLI_C)
3493 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3494 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3495
3496 if (used <= buf_len) {
3497 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3498 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3499 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3500
3501 if (session->ticket != NULL) {
3502 memcpy(p, session->ticket, session->ticket_len);
3503 p += session->ticket_len;
3504 }
3505
3506 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3507 p += 4;
3508 }
3509 }
3510#endif /* MBEDTLS_SSL_CLI_C */
3511#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3512 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3513 used += 8;
3514
3515 if (used <= buf_len) {
3516 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3517 p += 8;
3518 }
3519 }
3520#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3521#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3522
3523 /*
3524 * Misc extension-related info
3525 */
3526#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3527 used += 1;
3528
3529 if (used <= buf_len) {
3530 *p++ = session->mfl_code;
3531 }
3532#endif
3533
3534#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3535 used += 1;
3536
3537 if (used <= buf_len) {
3538 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3539 }
3540#endif
3541
3542 return used;
3543}
3544
3545MBEDTLS_CHECK_RETURN_CRITICAL
3546static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3547 const unsigned char *buf,
3548 size_t len)
3549{
3550#if defined(MBEDTLS_HAVE_TIME)
3551 uint64_t start;
3552#endif
3553#if defined(MBEDTLS_X509_CRT_PARSE_C)
3554#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3555 size_t cert_len;
3556#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3557#endif /* MBEDTLS_X509_CRT_PARSE_C */
3558
3559 const unsigned char *p = buf;
3560 const unsigned char * const end = buf + len;
3561
3562 /*
3563 * Time
3564 */
3565#if defined(MBEDTLS_HAVE_TIME)
3566 if (8 > (size_t) (end - p)) {
3567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3568 }
3569
3570 start = MBEDTLS_GET_UINT64_BE(p, 0);
3571 p += 8;
3572
3573 session->start = (time_t) start;
3574#endif /* MBEDTLS_HAVE_TIME */
3575
3576 /*
3577 * Basic mandatory fields
3578 */
3579 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3581 }
3582
3583 session->id_len = *p++;
3584 memcpy(session->id, p, 32);
3585 p += 32;
3586
3587 memcpy(session->master, p, 48);
3588 p += 48;
3589
3590 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3591 p += 4;
3592
3593 /* Immediately clear invalid pointer values that have been read, in case
3594 * we exit early before we replaced them with valid ones. */
3595#if defined(MBEDTLS_X509_CRT_PARSE_C)
3596#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3597 session->peer_cert = NULL;
3598#else
3599 session->peer_cert_digest = NULL;
3600#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3601#endif /* MBEDTLS_X509_CRT_PARSE_C */
3602#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3603 session->ticket = NULL;
3604#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3605
3606 /*
3607 * Peer certificate
3608 */
3609#if defined(MBEDTLS_X509_CRT_PARSE_C)
3610#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3611 /* Deserialize CRT from the end of the ticket. */
3612 if (3 > (size_t) (end - p)) {
3613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3614 }
3615
3616 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3617 p += 3;
3618
3619 if (cert_len != 0) {
3620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3621
3622 if (cert_len > (size_t) (end - p)) {
3623 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3624 }
3625
3626 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3627
3628 if (session->peer_cert == NULL) {
3629 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3630 }
3631
3632 mbedtls_x509_crt_init(session->peer_cert);
3633
3634 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3635 p, cert_len)) != 0) {
3636 mbedtls_x509_crt_free(session->peer_cert);
3637 mbedtls_free(session->peer_cert);
3638 session->peer_cert = NULL;
3639 return ret;
3640 }
3641
3642 p += cert_len;
3643 }
3644#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3645 /* Deserialize CRT digest from the end of the ticket. */
3646 if (2 > (size_t) (end - p)) {
3647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3648 }
3649
3650 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3651 session->peer_cert_digest_len = (size_t) *p++;
3652
3653 if (session->peer_cert_digest_len != 0) {
3654 const mbedtls_md_info_t *md_info =
3655 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3656 if (md_info == NULL) {
3657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3658 }
3659 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3661 }
3662
3663 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3665 }
3666
3667 session->peer_cert_digest =
3668 mbedtls_calloc(1, session->peer_cert_digest_len);
3669 if (session->peer_cert_digest == NULL) {
3670 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3671 }
3672
3673 memcpy(session->peer_cert_digest, p,
3674 session->peer_cert_digest_len);
3675 p += session->peer_cert_digest_len;
3676 }
3677#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3678#endif /* MBEDTLS_X509_CRT_PARSE_C */
3679
3680 /*
3681 * Session ticket and associated data
3682 */
3683#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3684#if defined(MBEDTLS_SSL_CLI_C)
3685 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3686 if (3 > (size_t) (end - p)) {
3687 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3688 }
3689
3690 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3691 p += 3;
3692
3693 if (session->ticket_len != 0) {
3694 if (session->ticket_len > (size_t) (end - p)) {
3695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3696 }
3697
3698 session->ticket = mbedtls_calloc(1, session->ticket_len);
3699 if (session->ticket == NULL) {
3700 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3701 }
3702
3703 memcpy(session->ticket, p, session->ticket_len);
3704 p += session->ticket_len;
3705 }
3706
3707 if (4 > (size_t) (end - p)) {
3708 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3709 }
3710
3711 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3712 p += 4;
3713 }
3714#endif /* MBEDTLS_SSL_CLI_C */
3715#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3716 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3717 if (8 > (size_t) (end - p)) {
3718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3719 }
3720 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3721 p += 8;
3722 }
3723#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3724#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3725
3726 /*
3727 * Misc extension-related info
3728 */
3729#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3730 if (1 > (size_t) (end - p)) {
3731 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3732 }
3733
3734 session->mfl_code = *p++;
3735#endif
3736
3737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3738 if (1 > (size_t) (end - p)) {
3739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3740 }
3741
3742 session->encrypt_then_mac = *p++;
3743#endif
3744
3745 /* Done, should have consumed entire buffer */
3746 if (p != end) {
3747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3748 }
3749
3750 return 0;
3751}
3752
3753#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3754
3755#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3756/* Serialization of TLS 1.3 sessions:
3757 *
David Horstmanncb01b362024-02-29 17:31:46 +00003758 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003759 */
3760#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3761MBEDTLS_CHECK_RETURN_CRITICAL
3762static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3763 unsigned char *buf,
3764 size_t buf_len,
3765 size_t *olen)
3766{
3767 unsigned char *p = buf;
3768#if defined(MBEDTLS_SSL_CLI_C) && \
3769 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3770 size_t hostname_len = (session->hostname == NULL) ?
3771 0 : strlen(session->hostname) + 1;
3772#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003773
3774#if defined(MBEDTLS_SSL_SRV_C) && \
3775 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003776 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003777 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003778#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003779 size_t needed = 4 /* ticket_age_add */
3780 + 1 /* ticket_flags */
3781 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003782
David Horstmanne59f9702024-02-29 16:08:57 +00003783 *olen = 0;
3784
3785 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3786 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3787 }
3788 needed += session->resumption_key_len; /* resumption_key */
3789
3790#if defined(MBEDTLS_SSL_EARLY_DATA)
3791 needed += 4; /* max_early_data_size */
3792#endif
3793#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3794 needed += 2; /* record_size_limit */
3795#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3796
3797#if defined(MBEDTLS_HAVE_TIME)
3798 needed += 8; /* ticket_creation_time or ticket_reception_time */
3799#endif
3800
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003801#if defined(MBEDTLS_SSL_SRV_C)
3802 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3803#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003804 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003805 + alpn_len; /* alpn */
3806#endif
3807 }
3808#endif /* MBEDTLS_SSL_SRV_C */
3809
David Horstmanne59f9702024-02-29 16:08:57 +00003810#if defined(MBEDTLS_SSL_CLI_C)
3811 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3812#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3813 needed += 2 /* hostname_len */
3814 + hostname_len; /* hostname */
3815#endif
3816
3817 needed += 4 /* ticket_lifetime */
3818 + 2; /* ticket_len */
3819
3820 /* Check size_t overflow */
3821 if (session->ticket_len > SIZE_MAX - needed) {
3822 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3823 }
3824
3825 needed += session->ticket_len; /* ticket */
3826 }
3827#endif /* MBEDTLS_SSL_CLI_C */
3828
3829 *olen = needed;
3830 if (needed > buf_len) {
3831 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3832 }
3833
3834 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3835 p[4] = session->ticket_flags;
3836
3837 /* save resumption_key */
3838 p[5] = session->resumption_key_len;
3839 p += 6;
3840 memcpy(p, session->resumption_key, session->resumption_key_len);
3841 p += session->resumption_key_len;
3842
3843#if defined(MBEDTLS_SSL_EARLY_DATA)
3844 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3845 p += 4;
3846#endif
3847#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3848 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3849 p += 2;
3850#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3851
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003852#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003853 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003854#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003855 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3856 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003857#endif /* MBEDTLS_HAVE_TIME */
3858
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003859#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003860 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3861 p += 2;
3862
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003863 if (alpn_len > 0) {
3864 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003865 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003866 p += alpn_len;
3867 }
3868#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3869 }
3870#endif /* MBEDTLS_SSL_SRV_C */
3871
David Horstmanne59f9702024-02-29 16:08:57 +00003872#if defined(MBEDTLS_SSL_CLI_C)
3873 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3874#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3875 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3876 p += 2;
3877 if (hostname_len > 0) {
3878 /* save host name */
3879 memcpy(p, session->hostname, hostname_len);
3880 p += hostname_len;
3881 }
3882#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3883
3884#if defined(MBEDTLS_HAVE_TIME)
3885 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3886 p += 8;
3887#endif
3888 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3889 p += 4;
3890
3891 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3892 p += 2;
3893
3894 if (session->ticket != NULL && session->ticket_len > 0) {
3895 memcpy(p, session->ticket, session->ticket_len);
3896 p += session->ticket_len;
3897 }
3898 }
3899#endif /* MBEDTLS_SSL_CLI_C */
3900 return 0;
3901}
3902
3903MBEDTLS_CHECK_RETURN_CRITICAL
3904static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3905 const unsigned char *buf,
3906 size_t len)
3907{
3908 const unsigned char *p = buf;
3909 const unsigned char *end = buf + len;
3910
3911 if (end - p < 6) {
3912 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3913 }
3914 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3915 session->ticket_flags = p[4];
3916
3917 /* load resumption_key */
3918 session->resumption_key_len = p[5];
3919 p += 6;
3920
3921 if (end - p < session->resumption_key_len) {
3922 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3923 }
3924
3925 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3926 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3927 }
3928 memcpy(session->resumption_key, p, session->resumption_key_len);
3929 p += session->resumption_key_len;
3930
3931#if defined(MBEDTLS_SSL_EARLY_DATA)
3932 if (end - p < 4) {
3933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3934 }
3935 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3936 p += 4;
3937#endif
3938#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3939 if (end - p < 2) {
3940 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3941 }
3942 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3943 p += 2;
3944#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3945
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003946#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003947 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003948#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003949 if (end - p < 8) {
3950 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3951 }
3952 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3953 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003954#endif /* MBEDTLS_HAVE_TIME */
3955
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003956#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003957 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003958
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003959 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003960 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3961 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003962
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003963 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3964 p += 2;
3965
3966 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003967 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3968 }
3969
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003970 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003971 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3972 if (ret != 0) {
3973 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003974 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003975 p += alpn_len;
3976 }
3977#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3978 }
3979#endif /* MBEDTLS_SSL_SRV_C */
3980
David Horstmanne59f9702024-02-29 16:08:57 +00003981#if defined(MBEDTLS_SSL_CLI_C)
3982 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3983#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3984 size_t hostname_len;
3985 /* load host name */
3986 if (end - p < 2) {
3987 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3988 }
3989 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3990 p += 2;
3991
3992 if (end - p < (long int) hostname_len) {
3993 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3994 }
3995 if (hostname_len > 0) {
3996 session->hostname = mbedtls_calloc(1, hostname_len);
3997 if (session->hostname == NULL) {
3998 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3999 }
4000 memcpy(session->hostname, p, hostname_len);
4001 p += hostname_len;
4002 }
4003#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4004
4005#if defined(MBEDTLS_HAVE_TIME)
4006 if (end - p < 8) {
4007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4008 }
4009 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4010 p += 8;
4011#endif
4012 if (end - p < 4) {
4013 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4014 }
4015 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4016 p += 4;
4017
4018 if (end - p < 2) {
4019 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4020 }
4021 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4022 p += 2;
4023
4024 if (end - p < (long int) session->ticket_len) {
4025 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4026 }
4027 if (session->ticket_len > 0) {
4028 session->ticket = mbedtls_calloc(1, session->ticket_len);
4029 if (session->ticket == NULL) {
4030 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4031 }
4032 memcpy(session->ticket, p, session->ticket_len);
4033 p += session->ticket_len;
4034 }
4035 }
4036#endif /* MBEDTLS_SSL_CLI_C */
4037
4038 return 0;
4039
4040}
4041#else /* MBEDTLS_SSL_SESSION_TICKETS */
4042MBEDTLS_CHECK_RETURN_CRITICAL
4043static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4044 unsigned char *buf,
4045 size_t buf_len,
4046 size_t *olen)
4047{
4048 ((void) session);
4049 ((void) buf);
4050 ((void) buf_len);
4051 *olen = 0;
4052 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4053}
4054
4055static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritiusd36913a2023-01-24 17:48:29 +01004056 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004057 size_t buf_len)
4058{
4059 ((void) session);
4060 ((void) buf);
4061 ((void) buf_len);
4062 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4063}
4064#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4066
Paul Bakker5121ce52009-01-03 21:22:43 +00004067/*
Hanno Beckera835da52019-05-16 12:39:07 +01004068 * Define ticket header determining Mbed TLS version
4069 * and structure of the ticket.
4070 */
4071
Hanno Becker94ef3b32019-05-16 12:50:45 +01004072/*
Hanno Becker50b59662019-05-28 14:30:45 +01004073 * Define bitflag determining compile-time settings influencing
4074 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004075 */
4076
Hanno Becker50b59662019-05-28 14:30:45 +01004077#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004078#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004079#else
Hanno Becker3e088662019-05-29 11:10:18 +01004080#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004081#endif /* MBEDTLS_HAVE_TIME */
4082
4083#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004084#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004085#else
Hanno Becker3e088662019-05-29 11:10:18 +01004086#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004087#endif /* MBEDTLS_X509_CRT_PARSE_C */
4088
David Horstmann5c5a32f2024-02-13 17:53:35 +00004089#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004090#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004091#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004092#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004093#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4094
Hanno Becker94ef3b32019-05-16 12:50:45 +01004095#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004096#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004097#else
Hanno Becker3e088662019-05-29 11:10:18 +01004098#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004099#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4100
4101#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004102#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004103#else
Hanno Becker3e088662019-05-29 11:10:18 +01004104#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004105#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4106
Hanno Becker94ef3b32019-05-16 12:50:45 +01004107#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004108#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004109#else
Hanno Becker3e088662019-05-29 11:10:18 +01004110#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004111#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4112
Hanno Becker94ef3b32019-05-16 12:50:45 +01004113#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4114#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4115#else
4116#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4117#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4118
David Horstmann92b258b2024-02-13 17:23:34 +00004119#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4120#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4121#else
4122#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4124
4125#if defined(MBEDTLS_SSL_EARLY_DATA)
4126#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4127#else
4128#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4129#endif /* MBEDTLS_SSL_EARLY_DATA */
4130
4131#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4132#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4133#else
4134#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4135#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4136
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004137#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4138 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004139#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4140#else
4141#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4142#endif /* MBEDTLS_SSL_ALPN */
4143
Hanno Becker3e088662019-05-29 11:10:18 +01004144#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4145#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4146#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4147#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004148#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4149#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004150#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004151#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4152#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4153#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004154#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004155
Hanno Becker50b59662019-05-28 14:30:45 +01004156#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 ((uint16_t) ( \
4158 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4159 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4160 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4161 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4162 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4163 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004164 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004165 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4166 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004167 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4168 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4169 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4170 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004171 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004172 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004173 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004174
Chien Wong4e9683e2023-12-28 17:07:43 +08004175static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004176 MBEDTLS_VERSION_MAJOR,
4177 MBEDTLS_VERSION_MINOR,
4178 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4180 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004181};
Hanno Beckera835da52019-05-16 12:39:07 +01004182
4183/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004184 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004185 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004186 *
David Horstmanncb01b362024-02-29 17:31:46 +00004187 * TLS 1.2 session:
4188 *
4189 * struct {
4190 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4191 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4192 * uint32 ticket_lifetime;
4193 * #endif
4194 * } ClientOnlyData;
4195 *
4196 * struct {
4197 * #if defined(MBEDTLS_HAVE_TIME)
4198 * uint64 start_time;
4199 * #endif
4200 * uint8 session_id_len; // at most 32
4201 * opaque session_id[32];
4202 * opaque master[48]; // fixed length in the standard
4203 * uint32 verify_result;
4204 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4205 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4206 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004207 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004208 * opaque peer_cert_digest<0..2^8-1>
4209 * #endif
4210 * select (endpoint) {
4211 * case client: ClientOnlyData;
4212 * case server: uint64 ticket_creation_time;
4213 * };
4214 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4215 * uint8 mfl_code; // up to 255 according to standard
4216 * #endif
4217 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4218 * uint8 encrypt_then_mac; // 0 or 1
4219 * #endif
4220 * } serialized_session_tls12;
4221 *
4222 *
4223 * TLS 1.3 Session:
4224 *
4225 * struct {
4226 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4227 * opaque hostname<0..2^16-1>;
4228 * #endif
4229 * #if defined(MBEDTLS_HAVE_TIME)
4230 * uint64 ticket_reception_time;
4231 * #endif
4232 * uint32 ticket_lifetime;
4233 * opaque ticket<1..2^16-1>;
4234 * } ClientOnlyData;
4235 *
4236 * struct {
4237 * uint32 ticket_age_add;
4238 * uint8 ticket_flags;
4239 * opaque resumption_key<0..255>;
4240 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4241 * uint32 max_early_data_size;
4242 * #endif
4243 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4244 * uint16 record_size_limit;
4245 * #endif
4246 * select ( endpoint ) {
4247 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004248 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004249 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004250 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004251 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004252 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004253 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004254 * #endif
4255 * };
4256 * } serialized_session_tls13;
4257 *
4258 *
4259 * SSL session:
4260 *
4261 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004262 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004263 * opaque mbedtls_version[3]; // library version: major, minor, patch
4264 * opaque session_format[2]; // library-version specific 16-bit field
4265 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004266 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004267 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004268 * Note: When updating the format, remember to keep
4269 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004270 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004271 * // In this version, `session_format` determines
4272 * // the setting of those compile-time
4273 * // configuration options which influence
4274 * // the structure of mbedtls_ssl_session.
4275 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004276 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004277 * // - TLS 1.2 (0x0303)
4278 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004279 * uint8_t endpoint;
4280 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004281 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004282 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004283 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004284 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004285 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004286 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004287 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004288 *
4289 * };
4290 *
4291 * } serialized_session;
4292 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004293 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004294
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004295MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004296static int ssl_session_save(const mbedtls_ssl_session *session,
4297 unsigned char omit_header,
4298 unsigned char *buf,
4299 size_t buf_len,
4300 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004301{
4302 unsigned char *p = buf;
4303 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004304 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004305#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4306 size_t out_len;
4307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4308#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (session == NULL) {
4310 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4311 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004314 /*
4315 * Add Mbed TLS version identifier
4316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (used <= buf_len) {
4320 memcpy(p, ssl_serialized_session_header,
4321 sizeof(ssl_serialized_session_header));
4322 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004323 }
4324 }
4325
4326 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004327 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004328 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004329 used += 1 /* TLS version */
4330 + 1 /* endpoint */
4331 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (used <= buf_len) {
4333 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004334 *p++ = session->endpoint;
4335 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4336 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004337 }
4338
4339 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004340 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004342#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004343 case MBEDTLS_SSL_VERSION_TLS1_2:
4344 used += ssl_tls12_session_save(session, p, remaining_len);
4345 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004346#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4347
Jerry Yu251a12e2022-07-13 15:15:48 +08004348#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 case MBEDTLS_SSL_VERSION_TLS1_3:
4350 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4351 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4352 return ret;
4353 }
4354 used += out_len;
4355 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004356#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4357
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 default:
4359 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004360 }
4361
4362 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (used > buf_len) {
4364 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4365 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004366
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004368}
4369
4370/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004371 * Public wrapper for ssl_session_save()
4372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4374 unsigned char *buf,
4375 size_t buf_len,
4376 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004377{
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004379}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004380
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004381/*
4382 * Deserialize session, see mbedtls_ssl_session_save() for format.
4383 *
4384 * This internal version is wrapped by a public function that cleans up in
4385 * case of error, and has an extra option omit_header.
4386 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004387MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004388static int ssl_session_load(mbedtls_ssl_session *session,
4389 unsigned char omit_header,
4390 const unsigned char *buf,
4391 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004392{
4393 const unsigned char *p = buf;
4394 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004395 size_t remaining_len;
4396
4397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (session == NULL) {
4399 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4400 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004403 /*
4404 * Check Mbed TLS version identifier
4405 */
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4408 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004409 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004410
4411 if (memcmp(p, ssl_serialized_session_header,
4412 sizeof(ssl_serialized_session_header)) != 0) {
4413 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4414 }
4415 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004416 }
4417
4418 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004419 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004420 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004421 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4423 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004424 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004425 session->endpoint = *p++;
4426 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4427 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004428
4429 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004430 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 case MBEDTLS_SSL_VERSION_TLS1_2:
4434 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004435#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4436
Jerry Yu438ddd82022-07-07 06:55:50 +00004437#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 case MBEDTLS_SSL_VERSION_TLS1_3:
4439 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004440#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 default:
4443 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004444 }
4445}
4446
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004447/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004448 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004450int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4451 const unsigned char *buf,
4452 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004453{
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 if (ret != 0) {
4457 mbedtls_ssl_session_free(session);
4458 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004459
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004461}
4462
4463/*
Paul Bakker1961b702013-01-25 14:49:24 +01004464 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004465 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004467static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004468{
4469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4470
Ronald Cron66dbf912022-02-02 15:33:46 +01004471 /*
4472 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004473 * that were written into the output buffer by the previous handshake step,
4474 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004475 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4476 * We proceed to the next handshake step only when all data from the
4477 * previous one have been sent to the peer, thus we make sure that this is
4478 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4479 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4480 * we have to wait before to go ahead.
4481 * In the case of TLS 1.3, handshake step handlers do not send data to the
4482 * peer. Data are only sent here and through
4483 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004484 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4487 return ret;
4488 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004489
4490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4492 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4493 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4494 return ret;
4495 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004496 }
4497#endif /* MBEDTLS_SSL_PROTO_DTLS */
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004500}
4501
Gilles Peskine449bd832023-01-11 14:50:10 +01004502int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004503{
Hanno Becker41934dd2021-08-07 19:13:43 +01004504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004507 ssl->conf == NULL ||
4508 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4510 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004511 }
4512
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 ret = ssl_prepare_handshake_step(ssl);
4514 if (ret != 0) {
4515 return ret;
4516 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 ret = mbedtls_ssl_handle_pending_alert(ssl);
4519 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004520 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 }
Jerry Yue7047812021-09-13 19:26:39 +08004522
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004523 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4524 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4525 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004527#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4529 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004530 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004531
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004533 case MBEDTLS_SSL_HELLO_REQUEST:
4534 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004535 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004536 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004537
Ronald Cron9f0fba32022-02-10 16:45:15 +01004538 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004540 break;
4541
4542 default:
4543#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4545 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4546 } else {
4547 ret = mbedtls_ssl_handshake_client_step(ssl);
4548 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004549#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004551#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004553#endif
4554 }
Jerry Yub9930e72021-08-06 17:11:51 +08004555 }
Ronald Cron6291b232023-03-08 15:51:25 +01004556#endif /* MBEDTLS_SSL_CLI_C */
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004560#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4561 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004562 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004563 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 ret = mbedtls_ssl_handshake_server_step(ssl);
4565 }
Ronald Cron6291b232023-03-08 15:51:25 +01004566#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4567 ret = mbedtls_ssl_handshake_server_step(ssl);
4568#else
4569 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004570#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004571 }
4572#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004575 /* handshake_step return error. And it is same
4576 * with alert_reason.
4577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 if (ssl->send_alert) {
4579 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004580 goto cleanup;
4581 }
4582 }
4583
4584cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004586}
4587
4588/*
4589 * Perform the SSL handshake
4590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004591int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004592{
4593 int ret = 0;
4594
Hanno Beckera817ea42020-10-20 15:20:23 +01004595 /* Sanity checks */
4596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if (ssl == NULL || ssl->conf == NULL) {
4598 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4599 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004600
Hanno Beckera817ea42020-10-20 15:20:23 +01004601#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4603 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4604 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4605 "mbedtls_ssl_set_timer_cb() for DTLS"));
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004607 }
4608#endif /* MBEDTLS_SSL_PROTO_DTLS */
4609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004611
Hanno Beckera817ea42020-10-20 15:20:23 +01004612 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004613 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4614 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004615
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004617 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 }
Paul Bakker1961b702013-01-25 14:49:24 +01004619 }
4620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004624}
4625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626#if defined(MBEDTLS_SSL_RENEGOTIATION)
4627#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004628/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004629 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004630 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004632static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004633{
Janos Follath865b3eb2019-12-16 11:46:15 +00004634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004635
Gilles Peskine449bd832023-01-11 14:50:10 +01004636 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004637
4638 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4640 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4644 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004645 }
4646
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004652
4653/*
4654 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 * - any side: calling mbedtls_ssl_renegotiate(),
4656 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4657 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004658 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004659 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004662int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004663{
Janos Follath865b3eb2019-12-16 11:46:15 +00004664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004667
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if ((ret = ssl_handshake_init(ssl)) != 0) {
4669 return ret;
4670 }
Paul Bakker48916f92012-09-16 19:57:18 +00004671
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004672 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4673 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4676 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4677 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004678 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004680 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004682 }
4683#endif
4684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004685 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4686 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4689 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4690 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004691 }
4692
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004696}
4697
4698/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004699 * Renegotiate current connection on client,
4700 * or request renegotiation on server
4701 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004702int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004704 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004705
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (ssl == NULL || ssl->conf == NULL) {
4707 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4708 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004711 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4713 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4715 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004718
4719 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (ssl->out_left != 0) {
4721 return mbedtls_ssl_flush_output(ssl);
4722 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004725 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004729 /*
4730 * On client, either start the renegotiation process or,
4731 * if already in progress, continue the handshake
4732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4734 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004737
4738 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4739 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4740 return ret;
4741 }
4742 } else {
4743 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4744 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4745 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004746 }
4747 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004753
Gilles Peskine449bd832023-01-11 14:50:10 +01004754void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004755{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004759 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004761
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004762#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Brett Warrene0edc842021-08-17 09:53:13 +01004763#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (ssl->handshake->group_list_heap_allocated) {
4765 mbedtls_free((void *) handshake->group_list);
4766 }
Brett Warrene0edc842021-08-17 09:53:13 +01004767 handshake->group_list = NULL;
4768#endif /* MBEDTLS_DEPRECATED_REMOVED */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004769#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Brett Warrene0edc842021-08-17 09:53:13 +01004770
Ronald Crone68ab4f2022-10-05 12:46:29 +02004771#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004772#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004773 if (ssl->handshake->sig_algs_heap_allocated) {
4774 mbedtls_free((void *) handshake->sig_algs);
4775 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004776 handshake->sig_algs = NULL;
4777#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004778#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if (ssl->handshake->certificate_request_context) {
4780 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004781 }
4782#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004783#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004784
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004785#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4787 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004788 handshake->async_in_progress = 0;
4789 }
4790#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4791
Elena Uziunaite0916cd72024-05-23 17:01:07 +01004792#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004795#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004796 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004797#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004798#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01004799#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004800#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004802#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004803 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004804#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004805#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004809#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004810#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004811 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004813#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004814
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004815#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004816#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004818 /*
4819 * Opaque keys are not stored in the handshake's data and it's the user
4820 * responsibility to destroy them. Clear ones, instead, are created by
4821 * the TLS library and should be destroyed at the same level
4822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4824 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004825 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004826 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4827#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004828 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004829#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004830#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004832 handshake->ecjpake_cache = NULL;
4833 handshake->ecjpake_cache_len = 0;
4834#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004835#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004836
Valerio Setti7aeec542023-07-05 18:57:21 +02004837#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004838 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004839 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004840 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004842#endif
4843
Ronald Cron73fe8df2022-10-05 14:31:43 +02004844#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004847 /* The maintenance of the external PSK key slot is the
4848 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (ssl->handshake->psk_opaque_is_internal) {
4850 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004851 ssl->handshake->psk_opaque_is_internal = 0;
4852 }
4853 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4854 }
Neil Armstronge952a302022-05-03 10:22:14 +02004855#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004857 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004858 }
Neil Armstronge952a302022-05-03 10:22:14 +02004859#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004860#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4863 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004864 /*
4865 * Free only the linked list wrapper, not the keys themselves
4866 * since the belong to the SNI callback
4867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004870
Gilles Peskineeccd8882020-03-10 12:19:08 +01004871#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4873 if (handshake->ecrs_peer_cert != NULL) {
4874 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4875 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004876 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004877#endif
4878
Hanno Becker75173122019-02-06 16:18:31 +00004879#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4880 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004882#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4883
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004884#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4886 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004887#endif /* MBEDTLS_SSL_CLI_C &&
4888 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004889
4890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 mbedtls_ssl_flight_free(handshake->flight);
4892 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004894
Valerio Settiea59c432023-07-25 11:14:03 +02004895#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004896 if (handshake->xxdh_psa_privkey_is_external == 0) {
4897 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 }
Valerio Settiea59c432023-07-25 11:14:03 +02004899#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004900
Ronald Cron6f135e12021-12-08 16:57:54 +01004901#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 mbedtls_ssl_transform_free(handshake->transform_handshake);
4903 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004904#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4906 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004907#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004908#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004909
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004910
4911#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4912 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4913 * processes datagrams and the fact that a datagram is allowed to have
4914 * several records in it, it is possible that the I/O buffers are not
4915 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4917 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004918#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004919
Jerry Yuba9c7272021-10-30 11:54:10 +08004920 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004921 mbedtls_platform_zeroize(handshake,
4922 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004923}
4924
Gilles Peskine449bd832023-01-11 14:50:10 +01004925void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004926{
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004928 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004933#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004934
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004935#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004936#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4937 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004939#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004941#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004942
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004943#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4944 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004945 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004946#endif
4947
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004949}
4950
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004951#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004952
4953#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4954#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4955#else
4956#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4957#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4958
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004959#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004960
4961#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4962#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4963#else
4964#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4965#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4966
4967#if defined(MBEDTLS_SSL_ALPN)
4968#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4969#else
4970#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4971#endif /* MBEDTLS_SSL_ALPN */
4972
4973#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4974#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4975#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4976#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4977
4978#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 ((uint32_t) ( \
4980 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4981 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4982 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4983 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4984 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4985 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4986 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4987 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004988
Chien Wong4e9683e2023-12-28 17:07:43 +08004989static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004990 MBEDTLS_VERSION_MAJOR,
4991 MBEDTLS_VERSION_MINOR,
4992 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4994 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4995 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4996 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4997 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004998};
4999
Paul Bakker5121ce52009-01-03 21:22:43 +00005000/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005001 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005002 *
5003 * The format of the serialized data is:
5004 * (in the presentation language of TLS, RFC 8446 section 3)
5005 *
5006 * // header
5007 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005008 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005009 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005010 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005011 * Note: When updating the format, remember to keep these
5012 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005013 *
5014 * // session sub-structure
5015 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5016 * // transform sub-structure
5017 * uint8 random[64]; // ServerHello.random+ClientHello.random
5018 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5019 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5020 * // fields from ssl_context
5021 * uint32 badmac_seen; // DTLS: number of records with failing MAC
5022 * uint64 in_window_top; // DTLS: last validated record seq_num
5023 * uint64 in_window; // DTLS: bitmask for replay protection
5024 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5025 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5026 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5027 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5028 *
5029 * Note that many fields of the ssl_context or sub-structures are not
5030 * serialized, as they fall in one of the following categories:
5031 *
5032 * 1. forced value (eg in_left must be 0)
5033 * 2. pointer to dynamically-allocated memory (eg session, transform)
5034 * 3. value can be re-derived from other data (eg session keys from MS)
5035 * 4. value was temporary (eg content of input buffer)
5036 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005038int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5039 unsigned char *buf,
5040 size_t buf_len,
5041 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005042{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005043 unsigned char *p = buf;
5044 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005045 size_t session_len;
5046 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005047
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005048 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005049 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5050 * this function's documentation.
5051 *
5052 * These are due to assumptions/limitations in the implementation. Some of
5053 * them are likely to stay (no handshake in progress) some might go away
5054 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005055 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005056 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5058 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5059 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 if (ssl->handshake != NULL) {
5062 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005064 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005065 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (ssl->transform == NULL || ssl->session == NULL) {
5067 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005069 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005070 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 if (mbedtls_ssl_check_pending(ssl) != 0) {
5072 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5073 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (ssl->out_left != 0) {
5076 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5077 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005078 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005079 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5081 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005083 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005084 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5086 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5087 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005088 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005089 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5091 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5092 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005093 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005094 /* Renegotiation must not be enabled */
5095#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5097 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5098 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005099 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005100#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005101
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005102 /*
5103 * Version and format identifier
5104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107 if (used <= buf_len) {
5108 memcpy(p, ssl_serialized_context_header,
5109 sizeof(ssl_serialized_context_header));
5110 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005111 }
5112
5113 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005114 * Session (length + data)
5115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5117 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5118 return ret;
5119 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005120
5121 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (used <= buf_len) {
5123 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005124 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 ret = ssl_session_save(ssl->session, 1,
5127 p, session_len, &session_len);
5128 if (ret != 0) {
5129 return ret;
5130 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005131
5132 p += session_len;
5133 }
5134
5135 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005136 * Transform
5137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 used += sizeof(ssl->transform->randbytes);
5139 if (used <= buf_len) {
5140 memcpy(p, ssl->transform->randbytes,
5141 sizeof(ssl->transform->randbytes));
5142 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005143 }
5144
5145#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005146 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005148 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005150 p += ssl->transform->in_cid_len;
5151
5152 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005154 p += ssl->transform->out_cid_len;
5155 }
5156#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5157
5158 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005159 * Saved fields from top-level ssl_context structure
5160 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005161 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (used <= buf_len) {
5163 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005164 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005165 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005166
5167#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5168 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (used <= buf_len) {
5170 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005171 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005174 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005175 }
5176#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5177
5178#if defined(MBEDTLS_SSL_PROTO_DTLS)
5179 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005181 *p++ = ssl->disable_datagram_packing;
5182 }
5183#endif /* MBEDTLS_SSL_PROTO_DTLS */
5184
Jerry Yuae0b2e22021-10-08 15:21:19 +08005185 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 if (used <= buf_len) {
5187 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005188 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005189 }
5190
5191#if defined(MBEDTLS_SSL_PROTO_DTLS)
5192 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005193 if (used <= buf_len) {
5194 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005195 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005196 }
5197#endif /* MBEDTLS_SSL_PROTO_DTLS */
5198
5199#if defined(MBEDTLS_SSL_ALPN)
5200 {
5201 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005203 : 0;
5204
5205 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005207 *p++ = alpn_len;
5208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (ssl->alpn_chosen != NULL) {
5210 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005211 p += alpn_len;
5212 }
5213 }
5214 }
5215#endif /* MBEDTLS_SSL_ALPN */
5216
5217 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005218 * Done
5219 */
5220 *olen = used;
5221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if (used > buf_len) {
5223 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5224 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005227
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005229}
5230
5231/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005232 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005233 *
5234 * This internal version is wrapped by a public function that cleans up in
5235 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005236 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005237MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005238static int ssl_context_load(mbedtls_ssl_context *ssl,
5239 const unsigned char *buf,
5240 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005241{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005242 const unsigned char *p = buf;
5243 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005244 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005246#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005247 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005248#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005249
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005250 /*
5251 * The context should have been freshly setup or reset.
5252 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005253 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005254 * renegotiating, or if the user mistakenly loaded a session first.)
5255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5257 ssl->session != NULL) {
5258 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005259 }
5260
5261 /*
5262 * We can't check that the config matches the initial one, but we can at
5263 * least check it matches the requirements for serializing.
5264 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005265 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005266#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005267 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005268#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005269 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5270 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5271 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5272 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005274 }
5275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005277
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005278 /*
5279 * Check version identifier
5280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5282 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005283 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005284
5285 if (memcmp(p, ssl_serialized_context_header,
5286 sizeof(ssl_serialized_context_header)) != 0) {
5287 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5288 }
5289 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005290
5291 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005292 * Session
5293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if ((size_t) (end - p) < 4) {
5295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5296 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005297
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005298 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005299 p += 4;
5300
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005301 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005302 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005303 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005304 ssl->session_in = ssl->session;
5305 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005306 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005307
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if ((size_t) (end - p) < session_len) {
5309 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5310 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005311
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 ret = ssl_session_load(ssl->session, 1, p, session_len);
5313 if (ret != 0) {
5314 mbedtls_ssl_session_free(ssl->session);
5315 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005316 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005317
5318 p += session_len;
5319
5320 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005321 * Transform
5322 */
5323
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005324 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005325 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005326#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005327 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005328 ssl->transform_in = ssl->transform;
5329 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005330 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005331#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005332
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005333#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5335 if (prf_func == NULL) {
5336 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5337 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005338
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005339 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005340 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5341 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5342 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005343
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 ret = ssl_tls12_populate_transform(ssl->transform,
5345 ssl->session->ciphersuite,
5346 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005347#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005349#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 prf_func,
5351 p, /* currently pointing to randbytes */
5352 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5353 ssl->conf->endpoint,
5354 ssl);
5355 if (ret != 0) {
5356 return ret;
5357 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005358#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005359 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005360
5361#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5362 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if ((size_t) (end - p) < 1) {
5364 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5365 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005366
5367 ssl->transform->in_cid_len = *p++;
5368
Gilles Peskine449bd832023-01-11 14:50:10 +01005369 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5370 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5371 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005372
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005374 p += ssl->transform->in_cid_len;
5375
5376 ssl->transform->out_cid_len = *p++;
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
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->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005383 p += ssl->transform->out_cid_len;
5384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5385
5386 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005387 * Saved fields from top-level ssl_context structure
5388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 if ((size_t) (end - p) < 4) {
5390 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5391 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005392
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005393 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005394 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005395
5396#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 if ((size_t) (end - p) < 16) {
5398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5399 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005400
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005401 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005402 p += 8;
5403
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005404 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005405 p += 8;
5406#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5407
5408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 if ((size_t) (end - p) < 1) {
5410 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5411 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005412
5413 ssl->disable_datagram_packing = *p++;
5414#endif /* MBEDTLS_SSL_PROTO_DTLS */
5415
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5418 }
5419 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5420 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005421
5422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 if ((size_t) (end - p) < 2) {
5424 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5425 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005426
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005427 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005428 p += 2;
5429#endif /* MBEDTLS_SSL_PROTO_DTLS */
5430
5431#if defined(MBEDTLS_SSL_ALPN)
5432 {
5433 uint8_t alpn_len;
5434 const char **cur;
5435
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if ((size_t) (end - p) < 1) {
5437 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5438 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005439
5440 alpn_len = *p++;
5441
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005443 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5445 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005446 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005447 ssl->alpn_chosen = *cur;
5448 break;
5449 }
5450 }
5451 }
5452
5453 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5455 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5456 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005457
5458 p += alpn_len;
5459 }
5460#endif /* MBEDTLS_SSL_ALPN */
5461
5462 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005463 * Forced fields from top-level ssl_context structure
5464 *
5465 * Most of them already set to the correct value by mbedtls_ssl_init() and
5466 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5467 */
5468 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005469 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005470
Hanno Becker361b10d2019-08-30 10:42:49 +01005471 /* Adjust pointers for header fields of outgoing records to
5472 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005474
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005475#if defined(MBEDTLS_SSL_PROTO_DTLS)
5476 ssl->in_epoch = 1;
5477#endif
5478
5479 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5480 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005481 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5482 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (ssl->handshake != NULL) {
5484 mbedtls_ssl_handshake_free(ssl);
5485 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005486 ssl->handshake = NULL;
5487 }
5488
5489 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005490 * Done - should have consumed entire buffer
5491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 if (p != end) {
5493 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5494 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005497}
5498
5499/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005500 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005502int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5503 const unsigned char *buf,
5504 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005505{
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005507
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 if (ret != 0) {
5509 mbedtls_ssl_free(context);
5510 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005511
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005513}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005514#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005515
5516/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 * Free an SSL context
5518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005519void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005520{
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005522 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005523 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005526
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005528#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5529 size_t out_buf_len = ssl->out_buf_len;
5530#else
5531 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5532#endif
5533
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005534 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005535 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 }
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005539#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5540 size_t in_buf_len = ssl->in_buf_len;
5541#else
5542 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5543#endif
5544
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005545 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005546 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005547 }
5548
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 if (ssl->transform) {
5550 mbedtls_ssl_transform_free(ssl->transform);
5551 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005552 }
5553
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 if (ssl->handshake) {
5555 mbedtls_ssl_handshake_free(ssl);
5556 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005557
5558#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5560 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005561#endif
5562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 mbedtls_ssl_session_free(ssl->session_negotiate);
5564 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005565 }
5566
Ronald Cron6f135e12021-12-08 16:57:54 +01005567#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 mbedtls_ssl_transform_free(ssl->transform_application);
5569 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005570#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (ssl->session) {
5573 mbedtls_ssl_session_free(ssl->session);
5574 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005575 }
5576
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005577#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005579 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005580 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005582
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005583#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005585#endif
5586
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005588
Paul Bakker86f04f42013-02-14 11:20:09 +01005589 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005591}
5592
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005593/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005594 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005595 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005597{
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005599}
5600
Gilles Peskineae270bf2021-06-02 00:05:29 +02005601/* The selection should be the same as mbedtls_x509_crt_profile_default in
5602 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005603 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005604 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005605 * about this list.
5606 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005607static const uint16_t ssl_preset_default_groups[] = {
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005608#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Brett Warrene0edc842021-08-17 09:53:13 +01005609 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005610#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005611#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005612 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005613#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005614#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005615 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005616#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005617#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Brett Warrene0edc842021-08-17 09:53:13 +01005618 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005619#endif
Elena Uziunaite24e24f22024-07-04 17:53:40 +01005620#if defined(PSA_WANT_ECC_SECP_R1_521)
Brett Warrene0edc842021-08-17 09:53:13 +01005621 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005622#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005623#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005624 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005625#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005626#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005627 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005628#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005629#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Brett Warrene0edc842021-08-17 09:53:13 +01005630 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005631#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005632#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005633 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5634 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5635 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5636 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5637 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5638#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005639 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005640};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005641
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005642static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005643 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5644 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5645 0
5646};
5647
Ronald Crone68ab4f2022-10-05 12:46:29 +02005648#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005649
Jerry Yu909df7b2022-01-22 11:56:27 +08005650/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005651 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005652 * rules SHOULD be upheld.
5653 * - No duplicate entries.
5654 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005655 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005656 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005657 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005658static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005659
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005660#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005661 defined(PSA_WANT_ALG_SHA_256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005662 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005663 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005664 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5665#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005666
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005667#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005668 defined(PSA_WANT_ALG_SHA_384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005669 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005670 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005671 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5672#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005673
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005674#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +02005675 defined(PSA_WANT_ALG_SHA_512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005676 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005677 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005678 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5679#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005680
Gabor Mezeic15ef932024-06-13 12:53:54 +02005681#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005682 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005683#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005684
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005685#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005686 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005687#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005688
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005689#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005690 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005691#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005692
Gabor Mezeic15ef932024-06-13 12:53:54 +02005693#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005694 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Gabor Mezeic15ef932024-06-13 12:53:54 +02005695#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005696
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005697#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005698 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005699#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005700
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005701#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005702 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005703#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005704
Gabor Mezei15b95a62022-05-09 16:37:58 +02005705 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005706};
5707
5708/* NOTICE: see above */
5709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5710static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005711
Gabor Mezeic15ef932024-06-13 12:53:54 +02005712#if defined(PSA_WANT_ALG_SHA_512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005713#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005715#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005716#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5717 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005718#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005719#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005721#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005722#endif /* PSA_WANT_ALG_SHA_512 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005723
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005724#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005725#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005727#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005728#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5729 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005730#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005731#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005733#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005734#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005735
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005736#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005737#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005739#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005740#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5741 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005742#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005743#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005745#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005746#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005747
Gabor Mezei15b95a62022-05-09 16:37:58 +02005748 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005749};
5750#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005751
Jerry Yu909df7b2022-01-22 11:56:27 +08005752/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005753static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005754
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005755#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005756 defined(PSA_WANT_ALG_SHA_256) && \
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005757 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005758 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005759 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5760#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005761
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005762#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005763 defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005764 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu53037892022-01-25 11:02:06 +08005765 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005766 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5767#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005768
Gabor Mezei15b95a62022-05-09 16:37:58 +02005769 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005770};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005771
Jerry Yu909df7b2022-01-22 11:56:27 +08005772/* NOTICE: see above */
5773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5774static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005775
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005776#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005777#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005779#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005780#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005781
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005782#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005783#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005785#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005786#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005787
Gabor Mezei15b95a62022-05-09 16:37:58 +02005788 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005789};
Jerry Yu909df7b2022-01-22 11:56:27 +08005790#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5791
Ronald Crone68ab4f2022-10-05 12:46:29 +02005792#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005793
Chien Wong4e9683e2023-12-28 17:07:43 +08005794static const uint16_t ssl_preset_suiteb_groups[] = {
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005795#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005796 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005797#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005798#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005799 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005800#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005801 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005802};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005803
Ronald Crone68ab4f2022-10-05 12:46:29 +02005804#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005805/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005806 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005807MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005808static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005809{
5810 size_t i, j;
5811 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5814 for (j = 0; j < i; j++) {
5815 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005816 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 }
5818 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5819 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5820 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005821 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005822 }
5823 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005825}
5826
Ronald Crone68ab4f2022-10-05 12:46:29 +02005827#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005828
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005829/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005830 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005832int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5833 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005834{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005835#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005837#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005838
Ronald Crone68ab4f2022-10-05 12:46:29 +02005839#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5841 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5842 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005843 }
5844
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5846 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5847 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005848 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005849
5850#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5852 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5853 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005854 }
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5857 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5858 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005859 }
5860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005861#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005862
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005863 /* Use the functions here so that they are covered in tests,
5864 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 mbedtls_ssl_conf_endpoint(conf, endpoint);
5866 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005867
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005868 /*
5869 * Things that are common to all presets
5870 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005871#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005873 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5874#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5875 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5876#endif
5877 }
5878#endif
5879
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005880#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5881 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5882#endif
5883
5884#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5885 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5886#endif
5887
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005888#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005889 conf->f_cookie_write = ssl_cookie_write_dummy;
5890 conf->f_cookie_check = ssl_cookie_check_dummy;
5891#endif
5892
5893#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5894 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5895#endif
5896
Janos Follath088ce432017-04-10 12:42:31 +01005897#if defined(MBEDTLS_SSL_SRV_C)
5898 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005899 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005900#endif
5901
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
5903 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5904 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5905#endif
5906
5907#if defined(MBEDTLS_SSL_RENEGOTIATION)
5908 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005909 memset(conf->renego_period, 0x00, 2);
5910 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005911#endif
5912
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005913#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005915 const unsigned char dhm_p[] =
5916 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5917 const unsigned char dhm_g[] =
5918 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5921 dhm_p, sizeof(dhm_p),
5922 dhm_g, sizeof(dhm_g))) != 0) {
5923 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005924 }
5925 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005926#endif
5927
Ronald Cron6f135e12021-12-08 16:57:54 +01005928#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005929
5930#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005931 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005932#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005933 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005934#endif
5935#endif /* MBEDTLS_SSL_EARLY_DATA */
5936
Jerry Yud0766ec2022-09-22 10:46:57 +08005937#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005938 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005940#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005941 /*
5942 * Allow all TLS 1.3 key exchange modes by default.
5943 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005944 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005945#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005946
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005948#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005949 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005950 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005951#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005953#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005955#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005956 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5957 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005958#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5959 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5960 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5961#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5962 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5963 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5964#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005966#endif
5967 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005968
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005969 /*
5970 * Preset-specific defaults
5971 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005973 /*
5974 * NSA Suite B
5975 */
5976 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005977
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005978 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005979
5980#if defined(MBEDTLS_X509_CRT_PARSE_C)
5981 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005982#endif
5983
Ronald Crone68ab4f2022-10-05 12:46:29 +02005984#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005985#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005987 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005989#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005991#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005992
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02005993#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01005994 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005995#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005996 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005997 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005998
5999 /*
6000 * Default
6001 */
6002 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006003
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006004 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006005
6006#if defined(MBEDTLS_X509_CRT_PARSE_C)
6007 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6008#endif
6009
Ronald Crone68ab4f2022-10-05 12:46:29 +02006010#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006011#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006013 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006015#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006017#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006018
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006019#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006020 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006021#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006022 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006023
6024#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6025 conf->dhm_min_bitlen = 1024;
6026#endif
6027 }
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006030}
6031
6032/*
6033 * Free mbedtls_ssl_config
6034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006035void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006036{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006037 if (conf == NULL) {
6038 return;
6039 }
6040
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006041#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 mbedtls_mpi_free(&conf->dhm_P);
6043 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006044#endif
6045
Ronald Cron73fe8df2022-10-05 14:31:43 +02006046#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006049 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6050 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006053 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006054 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006055 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006056 }
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006059 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006060 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006061 conf->psk_identity_len = 0;
6062 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006063#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006064
6065#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006067#endif
6068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006070}
6071
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006072#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006073 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006074/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006077unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006078{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6081 return MBEDTLS_SSL_SIG_RSA;
6082 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006083#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006084#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6086 return MBEDTLS_SSL_SIG_ECDSA;
6087 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006088#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006090}
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006093{
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006095 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006097 case MBEDTLS_PK_ECDSA:
6098 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006100 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006102 }
6103}
6104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006106{
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108#if defined(MBEDTLS_RSA_C)
6109 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006111#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006112#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006115#endif
6116 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006118 }
6119}
Valerio Settie9646ec2023-08-02 20:02:28 +02006120#endif /* MBEDTLS_PK_C &&
6121 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006122
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006123/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006124 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006126mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006127{
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 switch (hash) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006129#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006132#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006133#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006136#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006137#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006140#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006141#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006144#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006145#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006148#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006149#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006152#endif
6153 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006155 }
6156}
6157
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006158/*
6159 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006162{
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 switch (md) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006164#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006165 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006167#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006168#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006169 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006171#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006172#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006173 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006175#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006176#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006177 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006179#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006180#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006181 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006183#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006184#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006185 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006187#endif
6188 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006190 }
6191}
6192
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006193/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006194 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006195 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006197int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006198{
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006200
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (group_list == NULL) {
6202 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006203 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 for (; *group_list != 0; group_list++) {
6206 if (*group_list == tls_id) {
6207 return 0;
6208 }
6209 }
6210
6211 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006212}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006213
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006214#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006215/*
6216 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006218int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006219{
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006223 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006227}
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006228#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Valerio Setti67419f02023-01-04 16:12:42 +01006229
Valerio Setti18c9fed2022-12-30 17:44:24 +01006230static const struct {
6231 uint16_t tls_id;
6232 mbedtls_ecp_group_id ecp_group_id;
6233 psa_ecc_family_t psa_family;
6234 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006235} tls_id_match_table[] =
6236{
Elena Uziunaite24e24f22024-07-04 17:53:40 +01006237#if defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settiacd32c02023-06-29 18:06:29 +02006238 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006239#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006240#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settiacd32c02023-06-29 18:06:29 +02006241 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006242#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01006243#if defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006244 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006245#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006246#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006247 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006248#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01006249#if defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006250 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006251#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006252#if defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006253 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006254#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006255#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006256 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006257#endif
Elena Uziunaiteeaa0cf02024-07-04 17:41:25 +01006258#if defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02006259 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006260#endif
Elena Uziunaite63cb13e2024-09-05 12:43:14 +01006261#if defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02006262 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006263#endif
Elena Uziunaitea3632862024-07-04 13:52:43 +01006264#if defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006265 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006266#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006267#if defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006268 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006269#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006270#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settiacd32c02023-06-29 18:06:29 +02006271 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006272#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006273#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006274 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006275#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006276 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006277};
6278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006280 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006281 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006282{
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6284 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006285 if (type != NULL) {
6286 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 }
6288 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006289 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006291 return PSA_SUCCESS;
6292 }
6293 }
6294
6295 return PSA_ERROR_NOT_SUPPORTED;
6296}
6297
Gilles Peskine449bd832023-01-11 14:50:10 +01006298mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006299{
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6301 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006302 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006304 }
6305
6306 return MBEDTLS_ECP_DP_NONE;
6307}
6308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006310{
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6312 i++) {
6313 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006314 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006316 }
6317
6318 return 0;
6319}
6320
Valerio Setti67419f02023-01-04 16:12:42 +01006321#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006322static const struct {
6323 uint16_t tls_id;
6324 const char *name;
6325} tls_id_curve_name_table[] =
6326{
Valerio Setti54e23792023-07-07 10:49:27 +02006327 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6328 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6329 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6330 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6331 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6332 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6333 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6334 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6335 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6336 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6337 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6338 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6339 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006340 { 0, NULL },
6341};
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006344{
Valerio Settiacd32c02023-06-29 18:06:29 +02006345 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6346 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6347 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006348 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006349 }
6350
6351 return NULL;
6352}
Valerio Setti67419f02023-01-04 16:12:42 +01006353#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006354
Jerry Yu148165c2021-09-24 23:20:59 +08006355#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006356int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6357 const mbedtls_md_type_t md,
6358 unsigned char *dst,
6359 size_t dst_len,
6360 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006361{
Ronald Cronf6893e12022-01-07 22:09:01 +01006362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6363 psa_hash_operation_t *hash_operation_to_clone;
6364 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6365
Jerry Yu148165c2021-09-24 23:20:59 +08006366 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006367
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006369#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 case MBEDTLS_MD_SHA384:
6371 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6372 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006373#endif
6374
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006375#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 case MBEDTLS_MD_SHA256:
6377 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6378 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006379#endif
6380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 default:
6382 goto exit;
6383 }
6384
6385 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6386 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006387 goto exit;
6388 }
6389
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6391 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006392 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006394
6395exit:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006396#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006397 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006398 (void) ssl;
6399#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006400 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006401}
6402#else /* MBEDTLS_USE_PSA_CRYPTO */
6403
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006404#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006406static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6407 unsigned char *dst,
6408 size_t dst_len,
6409 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006410{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006411 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006412 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 if (dst_len < 48) {
6415 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6416 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006417
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006418 mbedtls_md_init(&sha384);
6419 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006420 if (ret != 0) {
6421 goto exit;
6422 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006423 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006424 if (ret != 0) {
6425 goto exit;
6426 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006427
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006428 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006429 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006430 goto exit;
6431 }
6432
6433 *olen = 48;
6434
6435exit:
6436
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006437 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006439}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006440#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006441
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006442#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006443MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006444static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6445 unsigned char *dst,
6446 size_t dst_len,
6447 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006448{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006449 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006450 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 if (dst_len < 32) {
6453 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6454 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006455
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006456 mbedtls_md_init(&sha256);
6457 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6458 if (ret != 0) {
6459 goto exit;
6460 }
6461 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6462 if (ret != 0) {
6463 goto exit;
6464 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006465
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006466 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6467 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006468 goto exit;
6469 }
6470
6471 *olen = 32;
6472
6473exit:
6474
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006475 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006477}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006478#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006479
Gilles Peskine449bd832023-01-11 14:50:10 +01006480int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6481 const mbedtls_md_type_t md,
6482 unsigned char *dst,
6483 size_t dst_len,
6484 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006485{
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006487
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006488#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 case MBEDTLS_MD_SHA384:
6490 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006491#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006492
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006493#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 case MBEDTLS_MD_SHA256:
6495 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006496#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 default:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006499#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006500 !defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 (void) ssl;
6502 (void) dst;
6503 (void) dst_len;
6504 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006505#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006507 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006509}
XiaokangQian647719a2021-12-07 09:16:29 +00006510
Jerry Yu148165c2021-09-24 23:20:59 +08006511#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006512
Ronald Crone68ab4f2022-10-05 12:46:29 +02006513#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006514/* mbedtls_ssl_parse_sig_alg_ext()
6515 *
6516 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6517 * value (TLS 1.3 RFC8446):
6518 * enum {
6519 * ....
6520 * ecdsa_secp256r1_sha256( 0x0403 ),
6521 * ecdsa_secp384r1_sha384( 0x0503 ),
6522 * ecdsa_secp521r1_sha512( 0x0603 ),
6523 * ....
6524 * } SignatureScheme;
6525 *
6526 * struct {
6527 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6528 * } SignatureSchemeList;
6529 *
6530 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6531 * value (TLS 1.2 RFC5246):
6532 * enum {
6533 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6534 * sha512(6), (255)
6535 * } HashAlgorithm;
6536 *
6537 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6538 * SignatureAlgorithm;
6539 *
6540 * struct {
6541 * HashAlgorithm hash;
6542 * SignatureAlgorithm signature;
6543 * } SignatureAndHashAlgorithm;
6544 *
6545 * SignatureAndHashAlgorithm
6546 * supported_signature_algorithms<2..2^16-2>;
6547 *
6548 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6549 * generalization of the TLS 1.2 signature algorithm extension.
6550 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6551 * `SignatureScheme` field of TLS 1.3
6552 *
6553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006554int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6555 const unsigned char *buf,
6556 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006557{
6558 const unsigned char *p = buf;
6559 size_t supported_sig_algs_len = 0;
6560 const unsigned char *supported_sig_algs_end;
6561 uint16_t sig_alg;
6562 uint32_t common_idx = 0;
6563
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6565 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006566 p += 2;
6567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 memset(ssl->handshake->received_sig_algs, 0,
6569 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006572 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 while (p < supported_sig_algs_end) {
6574 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6575 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006576 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006577 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6578 sig_alg,
6579 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006580#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6582 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6583 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006584 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006585 }
6586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6589 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006592 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6593 common_idx += 1;
6594 }
6595 }
6596 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 if (p != end) {
6598 MBEDTLS_SSL_DEBUG_MSG(1,
6599 ("Signature algorithms extension length misaligned"));
6600 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6601 MBEDTLS_ERR_SSL_DECODE_ERROR);
6602 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006603 }
6604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 if (common_idx == 0) {
6606 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6607 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6608 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6609 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006610 }
6611
Gabor Mezei15b95a62022-05-09 16:37:58 +02006612 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006614}
6615
Ronald Crone68ab4f2022-10-05 12:46:29 +02006616#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006617
Jerry Yudc7bd172022-02-17 13:44:15 +08006618#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6619
6620#if defined(MBEDTLS_USE_PSA_CRYPTO)
6621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6623 mbedtls_svc_key_id_t key,
6624 psa_algorithm_t alg,
6625 const unsigned char *raw_psk, size_t raw_psk_length,
6626 const unsigned char *seed, size_t seed_length,
6627 const unsigned char *label, size_t label_length,
6628 const unsigned char *other_secret,
6629 size_t other_secret_length,
6630 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006631{
6632 psa_status_t status;
6633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 status = psa_key_derivation_setup(derivation, alg);
6635 if (status != PSA_SUCCESS) {
6636 return status;
6637 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006638
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6640 status = psa_key_derivation_input_bytes(derivation,
6641 PSA_KEY_DERIVATION_INPUT_SEED,
6642 seed, seed_length);
6643 if (status != PSA_SUCCESS) {
6644 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006645 }
6646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 if (other_secret != NULL) {
6648 status = psa_key_derivation_input_bytes(derivation,
6649 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6650 other_secret, other_secret_length);
6651 if (status != PSA_SUCCESS) {
6652 return status;
6653 }
6654 }
6655
6656 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006657 status = psa_key_derivation_input_bytes(
6658 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 raw_psk, raw_psk_length);
6660 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006661 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006663 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 if (status != PSA_SUCCESS) {
6665 return status;
6666 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 status = psa_key_derivation_input_bytes(derivation,
6669 PSA_KEY_DERIVATION_INPUT_LABEL,
6670 label, label_length);
6671 if (status != PSA_SUCCESS) {
6672 return status;
6673 }
6674 } else {
6675 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006676 }
6677
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 status = psa_key_derivation_set_capacity(derivation, capacity);
6679 if (status != PSA_SUCCESS) {
6680 return status;
6681 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006682
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006684}
6685
Andrzej Kurek57d10632022-10-24 10:32:01 -04006686#if defined(PSA_WANT_ALG_SHA_384) || \
6687 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006689static int tls_prf_generic(mbedtls_md_type_t md_type,
6690 const unsigned char *secret, size_t slen,
6691 const char *label,
6692 const unsigned char *random, size_t rlen,
6693 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006694{
6695 psa_status_t status;
6696 psa_algorithm_t alg;
6697 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6698 psa_key_derivation_operation_t derivation =
6699 PSA_KEY_DERIVATION_OPERATION_INIT;
6700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006702 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006704 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006706
6707 /* Normally a "secret" should be long enough to be impossible to
6708 * find by brute force, and in particular should not be empty. But
6709 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6710 * and for this use case it makes sense to have a 0-length "secret".
6711 * Since the key API doesn't allow importing a key of length 0,
6712 * keep master_key=0, which setup_psa_key_derivation() understands
6713 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006715 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006716 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6717 psa_set_key_algorithm(&key_attributes, alg);
6718 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6721 if (status != PSA_SUCCESS) {
6722 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6723 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006724 }
6725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 status = setup_psa_key_derivation(&derivation,
6727 master_key, alg,
6728 NULL, 0,
6729 random, rlen,
6730 (unsigned char const *) label,
6731 (size_t) strlen(label),
6732 NULL, 0,
6733 dlen);
6734 if (status != PSA_SUCCESS) {
6735 psa_key_derivation_abort(&derivation);
6736 psa_destroy_key(master_key);
6737 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006738 }
6739
Gilles Peskine449bd832023-01-11 14:50:10 +01006740 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6741 if (status != PSA_SUCCESS) {
6742 psa_key_derivation_abort(&derivation);
6743 psa_destroy_key(master_key);
6744 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006745 }
6746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 status = psa_key_derivation_abort(&derivation);
6748 if (status != PSA_SUCCESS) {
6749 psa_destroy_key(master_key);
6750 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006751 }
6752
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 if (!mbedtls_svc_key_id_is_null(master_key)) {
6754 status = psa_destroy_key(master_key);
6755 }
6756 if (status != PSA_SUCCESS) {
6757 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6758 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006759
Gilles Peskine449bd832023-01-11 14:50:10 +01006760 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006761}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006762#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006763#else /* MBEDTLS_USE_PSA_CRYPTO */
6764
Andrzej Kurek57d10632022-10-24 10:32:01 -04006765#if defined(MBEDTLS_MD_C) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006766 (defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006767 defined(PSA_WANT_ALG_SHA_384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006768MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006769static int tls_prf_generic(mbedtls_md_type_t md_type,
6770 const unsigned char *secret, size_t slen,
6771 const char *label,
6772 const unsigned char *random, size_t rlen,
6773 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006774{
6775 size_t nb;
6776 size_t i, j, k, md_len;
6777 unsigned char *tmp;
6778 size_t tmp_len = 0;
6779 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6780 const mbedtls_md_info_t *md_info;
6781 mbedtls_md_context_t md_ctx;
6782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6787 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6788 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 tmp_len = md_len + strlen(label) + rlen;
6793 tmp = mbedtls_calloc(1, tmp_len);
6794 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006795 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6796 goto exit;
6797 }
6798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 nb = strlen(label);
6800 memcpy(tmp + md_len, label, nb);
6801 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006802 nb += rlen;
6803
6804 /*
6805 * Compute P_<hash>(secret, label + random)[0..dlen]
6806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006808 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6812 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006813 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 }
6815 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6816 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006817 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 }
6819 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6820 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 for (i = 0; i < dlen; i += md_len) {
6825 ret = mbedtls_md_hmac_reset(&md_ctx);
6826 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006827 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 }
6829 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6830 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006831 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 }
6833 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6834 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006835 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006837
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 ret = mbedtls_md_hmac_reset(&md_ctx);
6839 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006840 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 }
6842 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6843 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006844 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 }
6846 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6847 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006848 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006854 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006856 }
6857
6858exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 if (tmp != NULL) {
6862 mbedtls_platform_zeroize(tmp, tmp_len);
6863 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006870}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006871#endif /* MBEDTLS_MD_C && ( PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006872#endif /* MBEDTLS_USE_PSA_CRYPTO */
6873
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006874#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006876static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6877 const char *label,
6878 const unsigned char *random, size_t rlen,
6879 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006880{
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6882 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006883}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006884#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006885
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006886#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006887MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006888static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6889 const char *label,
6890 const unsigned char *random, size_t rlen,
6891 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006892{
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6894 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006895}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006896#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006897
Jerry Yuf009d862022-02-17 14:01:37 +08006898/*
6899 * Set appropriate PRF function and other SSL / TLS1.2 functions
6900 *
6901 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006902 * - hash associated with the ciphersuite (only used by TLS 1.2)
6903 *
6904 * Outputs:
6905 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006908static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6909 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006910{
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006911#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006913 handshake->tls_prf = tls_prf_sha384;
6914 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6915 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006917#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006918#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuf009d862022-02-17 14:01:37 +08006919 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006920 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006921 handshake->tls_prf = tls_prf_sha256;
6922 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6923 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6924 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006925#else
Jerry Yuf009d862022-02-17 14:01:37 +08006926 {
Jerry Yuf009d862022-02-17 14:01:37 +08006927 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006928 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006930 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006931#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006932
Gilles Peskine449bd832023-01-11 14:50:10 +01006933 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006934}
Jerry Yud6ab2352022-02-17 14:03:43 +08006935
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006936/*
6937 * Compute master secret if needed
6938 *
6939 * Parameters:
6940 * [in/out] handshake
6941 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6942 * (PSA-PSK) ciphersuite_info, psk_opaque
6943 * [out] premaster (cleared)
6944 * [out] master
6945 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6946 * debug: conf->f_dbg, conf->p_dbg
6947 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006948 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006949 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006950MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006951static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6952 unsigned char *master,
6953 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006954{
6955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6956
6957 /* cf. RFC 5246, Section 8.1:
6958 * "The master secret is always exactly 48 bytes in length." */
6959 size_t const master_secret_len = 48;
6960
6961#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6962 unsigned char session_hash[48];
6963#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6964
6965 /* The label for the KDF used for key expansion.
6966 * This is either "master secret" or "extended master secret"
6967 * depending on whether the Extended Master Secret extension
6968 * is used. */
6969 char const *lbl = "master secret";
6970
Przemek Stekielae4ed302022-04-05 17:15:55 +02006971 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006972 * - If the Extended Master Secret extension is not used,
6973 * this is ClientHello.Random + ServerHello.Random
6974 * (see Sect. 8.1 in RFC 5246).
6975 * - If the Extended Master Secret extension is used,
6976 * this is the transcript of the handshake so far.
6977 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006978 unsigned char const *seed = handshake->randbytes;
6979 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006980
6981#if !defined(MBEDTLS_DEBUG_C) && \
6982 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6983 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006985 ssl = NULL; /* make sure we don't use it except for those cases */
6986 (void) ssl;
6987#endif
6988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (handshake->resume != 0) {
6990 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6991 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006992 }
6993
6994#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006996 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006997 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006998 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6999 if (ret != 0) {
7000 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7001 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007002
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7004 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007005 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007006#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007007
Przemek Stekiel99114f32022-04-22 11:20:09 +02007008#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007009 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007011 /* Perform PSK-to-MS expansion in a single step. */
7012 psa_status_t status;
7013 psa_algorithm_t alg;
7014 mbedtls_svc_key_id_t psk;
7015 psa_key_derivation_operation_t derivation =
7016 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007017 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007020
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007024 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007026 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007028
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007029 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007031
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007033 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007034 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007035 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007036 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007037 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007038 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007039 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7040 other_secret = handshake->premaster + 2;
7041 break;
7042 default:
7043 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007044 }
7045
Gilles Peskine449bd832023-01-11 14:50:10 +01007046 status = setup_psa_key_derivation(&derivation, psk, alg,
7047 ssl->conf->psk, ssl->conf->psk_len,
7048 seed, seed_len,
7049 (unsigned char const *) lbl,
7050 (size_t) strlen(lbl),
7051 other_secret, other_secret_len,
7052 master_secret_len);
7053 if (status != PSA_SUCCESS) {
7054 psa_key_derivation_abort(&derivation);
7055 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007056 }
7057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 status = psa_key_derivation_output_bytes(&derivation,
7059 master,
7060 master_secret_len);
7061 if (status != PSA_SUCCESS) {
7062 psa_key_derivation_abort(&derivation);
7063 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007064 }
7065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 status = psa_key_derivation_abort(&derivation);
7067 if (status != PSA_SUCCESS) {
7068 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7069 }
7070 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007071#endif
7072 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007073#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7075 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007076 psa_status_t status;
7077 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7078 psa_key_derivation_operation_t derivation =
7079 PSA_KEY_DERIVATION_OPERATION_INIT;
7080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007082
7083 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7084
Gilles Peskine449bd832023-01-11 14:50:10 +01007085 status = psa_key_derivation_setup(&derivation, alg);
7086 if (status != PSA_SUCCESS) {
7087 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007088 }
7089
Gilles Peskine449bd832023-01-11 14:50:10 +01007090 status = psa_key_derivation_set_capacity(&derivation,
7091 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7092 if (status != PSA_SUCCESS) {
7093 psa_key_derivation_abort(&derivation);
7094 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007095 }
7096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7098 &derivation);
7099 if (status != PSA_SUCCESS) {
7100 psa_key_derivation_abort(&derivation);
7101 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007102 }
7103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 status = psa_key_derivation_output_bytes(&derivation,
7105 handshake->premaster,
7106 handshake->pmslen);
7107 if (status != PSA_SUCCESS) {
7108 psa_key_derivation_abort(&derivation);
7109 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7110 }
7111
7112 status = psa_key_derivation_abort(&derivation);
7113 if (status != PSA_SUCCESS) {
7114 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007115 }
7116 }
7117#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7119 lbl, seed, seed_len,
7120 master,
7121 master_secret_len);
7122 if (ret != 0) {
7123 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7124 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007125 }
7126
Gilles Peskine449bd832023-01-11 14:50:10 +01007127 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7128 handshake->premaster,
7129 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 mbedtls_platform_zeroize(handshake->premaster,
7132 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007133 }
7134
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007136}
7137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007139{
7140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7141 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7142 ssl->handshake->ciphersuite_info;
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007145
7146 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007148 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 if (ret != 0) {
7150 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7151 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007152 }
7153
7154 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 ret = ssl_compute_master(ssl->handshake,
7156 ssl->session_negotiate->master,
7157 ssl);
7158 if (ret != 0) {
7159 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7160 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007161 }
7162
7163 /* Swap the client and server random values:
7164 * - MS derivation wanted client+server (RFC 5246 8.1)
7165 * - key derivation wants server+client (RFC 5246 6.3) */
7166 {
7167 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007168 memcpy(tmp, ssl->handshake->randbytes, 64);
7169 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7170 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7171 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007172 }
7173
7174 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7176 ssl->session_negotiate->ciphersuite,
7177 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007178#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007180#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007181 ssl->handshake->tls_prf,
7182 ssl->handshake->randbytes,
7183 ssl->tls_version,
7184 ssl->conf->endpoint,
7185 ssl);
7186 if (ret != 0) {
7187 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7188 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007189 }
7190
7191 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7193 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007196
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007198}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007199
Gilles Peskine449bd832023-01-11 14:50:10 +01007200int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007201{
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007203#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007204 case MBEDTLS_SSL_HASH_SHA384:
7205 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7206 break;
7207#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007208#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007209 case MBEDTLS_SSL_HASH_SHA256:
7210 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7211 break;
7212#endif
7213 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007215 }
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007216#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007217 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007218 (void) ssl;
7219#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007221}
7222
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007223#if defined(MBEDTLS_USE_PSA_CRYPTO)
7224static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7225 const psa_hash_operation_t *hs_op,
7226 size_t buffer_size,
7227 unsigned char *hash,
7228 size_t *hlen)
7229{
7230 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007231 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007232
7233#if !defined(MBEDTLS_DEBUG_C)
7234 (void) ssl;
7235#endif
7236 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007237 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007238 if (status != PSA_SUCCESS) {
7239 goto exit;
7240 }
7241
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007242 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007243 if (status != PSA_SUCCESS) {
7244 goto exit;
7245 }
7246
7247 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7248 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7249
7250exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007251 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007252 return mbedtls_md_error_from_psa(status);
7253}
7254#else
7255static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7256 const mbedtls_md_context_t *hs_ctx,
7257 unsigned char *hash,
7258 size_t *hlen)
7259{
7260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007261 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007262
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007263 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007264
7265#if !defined(MBEDTLS_DEBUG_C)
7266 (void) ssl;
7267#endif
7268 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7269
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007270 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007271 if (ret != 0) {
7272 goto exit;
7273 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007274 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007275 if (ret != 0) {
7276 goto exit;
7277 }
7278
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007279 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007280 if (ret != 0) {
7281 goto exit;
7282 }
7283
7284 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7285
7286 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7287 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7288
7289exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007290 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007291 return ret;
7292}
7293#endif /* MBEDTLS_USE_PSA_CRYPTO */
7294
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007295#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007296int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7297 unsigned char *hash,
7298 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007299{
7300#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007301 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7302 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007303#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007304 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7305 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007306#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007307}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007308#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007309
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007310#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007311int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7312 unsigned char *hash,
7313 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007314{
7315#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007316 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7317 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007318#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007319 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7320 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007321#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007322}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007323#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007324
Neil Armstrong80f6f322022-05-03 17:56:38 +02007325#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7326 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007327int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007328{
7329 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007331 const unsigned char *psk = NULL;
7332 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007336 /*
7337 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007338 * checked before calling this function.
7339 *
7340 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007341 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7344 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7345 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007346 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007347 }
7348
7349 /*
7350 * PMS = struct {
7351 * opaque other_secret<0..2^16-1>;
7352 * opaque psk<0..2^16-1>;
7353 * };
7354 * with "other_secret" depending on the particular key exchange
7355 */
7356#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7358 if (end - p < 2) {
7359 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7360 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007363 p += 2;
7364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (end < p || (size_t) (end - p) < psk_len) {
7366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7367 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007368
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007370 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007372#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007373#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7376 size_t len;
7377
7378 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007380 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7382 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7383 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007386 p += 2 + len;
7387
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7389 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007390#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007391#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7394 size_t zlen;
7395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007397 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7399 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7400 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007401 }
7402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007404 p += 2 + zlen;
7405
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7407 MBEDTLS_DEBUG_ECDH_Z);
7408 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007409#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007410 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7412 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007413 }
7414
7415 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (end - p < 2) {
7417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7418 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007421 p += 2;
7422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (end < p || (size_t) (end - p) < psk_len) {
7424 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7425 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007428 p += psk_len;
7429
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007430 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007433}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007434#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007435
7436#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007437MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007438static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007439
7440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007441int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007442{
7443 /* If renegotiation is not enforced, retransmit until we would reach max
7444 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007446 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7447 unsigned char doublings = 1;
7448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007450 ++doublings;
7451 ratio >>= 1;
7452 }
7453
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 if (++ssl->renego_records_seen > doublings) {
7455 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7456 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007457 }
7458 }
7459
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007461}
7462#endif
7463#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007464
Jerry Yud9526692022-02-17 14:23:47 +08007465/*
7466 * Handshake functions
7467 */
7468#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7469/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007470int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007471{
7472 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7473 ssl->handshake->ciphersuite_info;
7474
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7478 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007479 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007481 }
7482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7484 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007485}
7486
Gilles Peskine449bd832023-01-11 14:50:10 +01007487int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007488{
7489 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7490 ssl->handshake->ciphersuite_info;
7491
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007493
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7495 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007496 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007498 }
7499
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7501 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007502}
7503
7504#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7505/* Some certificate support -> implement write and parse */
7506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007508{
7509 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7510 size_t i, n;
7511 const mbedtls_x509_crt *crt;
7512 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7513 ssl->handshake->ciphersuite_info;
7514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007516
Gilles Peskine449bd832023-01-11 14:50:10 +01007517 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7518 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007519 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007521 }
7522
7523#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7525 if (ssl->handshake->client_auth == 0) {
7526 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007527 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007529 }
7530 }
7531#endif /* MBEDTLS_SSL_CLI_C */
7532#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7534 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007535 /* Should never happen because we shouldn't have picked the
7536 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007538 }
7539 }
7540#endif
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007543
7544 /*
7545 * 0 . 0 handshake type
7546 * 1 . 3 handshake length
7547 * 4 . 6 length of all certs
7548 * 7 . 9 length of cert. 1
7549 * 10 . n-1 peer certificate
7550 * n . n+2 length of cert. 2
7551 * n+3 . ... upper level cert, etc.
7552 */
7553 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007557 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7559 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7560 " > %" MBEDTLS_PRINTF_SIZET,
7561 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7562 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007563 }
7564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7566 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7567 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007568
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007570 i += n; crt = crt->next;
7571 }
7572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7574 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7575 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007576
7577 ssl->out_msglen = i;
7578 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7579 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7580
7581 ssl->state++;
7582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7585 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007586 }
7587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007591}
7592
7593#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7594
7595#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007596MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007597static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7598 unsigned char *crt_buf,
7599 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007600{
7601 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (peer_crt == NULL) {
7604 return -1;
7605 }
Jerry Yud9526692022-02-17 14:23:47 +08007606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (peer_crt->raw.len != crt_buf_len) {
7608 return -1;
7609 }
Jerry Yud9526692022-02-17 14:23:47 +08007610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007612}
7613#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007614MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007615static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7616 unsigned char *crt_buf,
7617 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007618{
7619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7620 unsigned char const * const peer_cert_digest =
7621 ssl->session->peer_cert_digest;
7622 mbedtls_md_type_t const peer_cert_digest_type =
7623 ssl->session->peer_cert_digest_type;
7624 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007626 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7627 size_t digest_len;
7628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 if (peer_cert_digest == NULL || digest_info == NULL) {
7630 return -1;
7631 }
Jerry Yud9526692022-02-17 14:23:47 +08007632
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 digest_len = mbedtls_md_get_size(digest_info);
7634 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7635 return -1;
7636 }
Jerry Yud9526692022-02-17 14:23:47 +08007637
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7639 if (ret != 0) {
7640 return -1;
7641 }
Jerry Yud9526692022-02-17 14:23:47 +08007642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007644}
7645#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7646#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7647
7648/*
7649 * Once the certificate message is read, parse it into a cert chain and
7650 * perform basic checks, but leave actual verification to the caller
7651 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007652MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007653static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7654 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007655{
7656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7657#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007659#endif
7660 size_t i, n;
7661 uint8_t alert;
7662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7664 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7665 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7666 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7667 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007668 }
7669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7671 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7672 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7673 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007674 }
7675
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7677 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7678 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7679 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7680 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007681 }
7682
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007684
7685 /*
7686 * Same message structure as in mbedtls_ssl_write_certificate()
7687 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007688 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007689
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 if (ssl->in_msg[i] != 0 ||
7691 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7692 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7693 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7694 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7695 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007696 }
7697
7698 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7699 i += 3;
7700
7701 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007703 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 if (i + 3 > ssl->in_hslen) {
7705 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7706 mbedtls_ssl_send_alert_message(ssl,
7707 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7708 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7709 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007710 }
7711 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7712 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 if (ssl->in_msg[i] != 0) {
7714 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7715 mbedtls_ssl_send_alert_message(ssl,
7716 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7717 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7718 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007719 }
7720
7721 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007722 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007723 i += 3;
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 if (n < 128 || i + n > ssl->in_hslen) {
7726 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7727 mbedtls_ssl_send_alert_message(ssl,
7728 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7729 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7730 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007731 }
7732
7733 /* Check if we're handling the first CRT in the chain. */
7734#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007736 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007738 /* During client-side renegotiation, check that the server's
7739 * end-CRTs hasn't changed compared to the initial handshake,
7740 * mitigating the triple handshake attack. On success, reuse
7741 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7743 if (ssl_check_peer_crt_unchanged(ssl,
7744 &ssl->in_msg[i],
7745 n) != 0) {
7746 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7747 mbedtls_ssl_send_alert_message(ssl,
7748 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7749 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7750 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007751 }
7752
7753 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007755 }
7756#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7757
7758 /* Parse the next certificate in the chain. */
7759#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007761#else
7762 /* If we don't need to store the CRT chain permanently, parse
7763 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007765#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007767 case 0: /*ok*/
7768 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7769 /* Ignore certificate with an unknown algorithm: maybe a
7770 prior certificate was already trusted. */
7771 break;
7772
7773 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7774 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7775 goto crt_parse_der_failed;
7776
7777 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7778 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7779 goto crt_parse_der_failed;
7780
7781 default:
7782 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007783crt_parse_der_failed:
7784 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7785 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7786 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007787 }
7788
7789 i += n;
7790 }
7791
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7793 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007794}
7795
7796#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007797MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007798static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007799{
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7801 return -1;
7802 }
Jerry Yud9526692022-02-17 14:23:47 +08007803
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007805 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7806 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007807 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7808 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7809 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007810 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007811 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007812}
7813#endif /* MBEDTLS_SSL_SRV_C */
7814
7815/* Check if a certificate message is expected.
7816 * Return either
7817 * - SSL_CERTIFICATE_EXPECTED, or
7818 * - SSL_CERTIFICATE_SKIP
7819 * indicating whether a Certificate message is expected or not.
7820 */
7821#define SSL_CERTIFICATE_EXPECTED 0
7822#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007823MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007824static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7825 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007826{
7827 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7828 ssl->handshake->ciphersuite_info;
7829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7831 return SSL_CERTIFICATE_SKIP;
7832 }
Jerry Yud9526692022-02-17 14:23:47 +08007833
7834#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007837 ssl->session_negotiate->verify_result =
7838 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007840 }
7841 }
7842#else
7843 ((void) authmode);
7844#endif /* MBEDTLS_SSL_SRV_C */
7845
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007847}
7848
Jerry Yud9526692022-02-17 14:23:47 +08007849#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007850MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007851static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7852 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007853{
7854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7855 /* Remember digest of the peer's end-CRT. */
7856 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7858 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7859 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7860 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7861 mbedtls_ssl_send_alert_message(ssl,
7862 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7863 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007864
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007866 }
7867
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 ret = mbedtls_md(mbedtls_md_info_from_type(
7869 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7870 start, len,
7871 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007872
7873 ssl->session_negotiate->peer_cert_digest_type =
7874 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7875 ssl->session_negotiate->peer_cert_digest_len =
7876 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007879}
7880
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007881MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007882static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7883 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007884{
7885 unsigned char *end = start + len;
7886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7887
7888 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7890 ret = mbedtls_pk_parse_subpubkey(&start, end,
7891 &ssl->handshake->peer_pubkey);
7892 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007893 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007895 }
7896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007898}
7899#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007902{
7903 int ret = 0;
7904 int crt_expected;
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +02007905 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007906#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7907 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7908 ? ssl->handshake->sni_authmode
7909 : ssl->conf->authmode;
7910#else
7911 const int authmode = ssl->conf->authmode;
7912#endif
7913 void *rs_ctx = NULL;
7914 mbedtls_x509_crt *chain = NULL;
7915
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7919 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7920 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007921 goto exit;
7922 }
7923
7924#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 if (ssl->handshake->ecrs_enabled &&
7926 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007927 chain = ssl->handshake->ecrs_peer_cert;
7928 ssl->handshake->ecrs_peer_cert = NULL;
7929 goto crt_verify;
7930 }
7931#endif
7932
Gilles Peskine449bd832023-01-11 14:50:10 +01007933 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007934 /* mbedtls_ssl_read_record may have sent an alert already. We
7935 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007937 goto exit;
7938 }
7939
7940#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007942 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7943
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007945 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 }
Jerry Yud9526692022-02-17 14:23:47 +08007947
7948 goto exit;
7949 }
7950#endif /* MBEDTLS_SSL_SRV_C */
7951
7952 /* Clear existing peer CRT structure in case we tried to
7953 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007955
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7957 if (chain == NULL) {
7958 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7959 sizeof(mbedtls_x509_crt)));
7960 mbedtls_ssl_send_alert_message(ssl,
7961 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7962 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007963
7964 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7965 goto exit;
7966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007968
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 ret = ssl_parse_certificate_chain(ssl, chain);
7970 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007971 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 }
Jerry Yud9526692022-02-17 14:23:47 +08007973
7974#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007976 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 }
Jerry Yud9526692022-02-17 14:23:47 +08007978
7979crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007981 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 }
Jerry Yud9526692022-02-17 14:23:47 +08007983#endif
7984
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +02007985 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
7986 ssl->handshake->ciphersuite_info,
7987 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007989 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 }
Jerry Yud9526692022-02-17 14:23:47 +08007991
7992#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7993 {
7994 unsigned char *crt_start, *pk_start;
7995 size_t crt_len, pk_len;
7996
7997 /* We parse the CRT chain without copying, so
7998 * these pointers point into the input buffer,
7999 * and are hence still valid after freeing the
8000 * CRT chain. */
8001
8002 crt_start = chain->raw.p;
8003 crt_len = chain->raw.len;
8004
8005 pk_start = chain->pk_raw.p;
8006 pk_len = chain->pk_raw.len;
8007
8008 /* Free the CRT structures before computing
8009 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 mbedtls_x509_crt_free(chain);
8011 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008012 chain = NULL;
8013
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8015 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008016 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008017 }
Jerry Yud9526692022-02-17 14:23:47 +08008018
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8020 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008021 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 }
Jerry Yud9526692022-02-17 14:23:47 +08008023 }
8024#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8025 /* Pass ownership to session structure. */
8026 ssl->session_negotiate->peer_cert = chain;
8027 chain = NULL;
8028#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8029
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008031
8032exit:
8033
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008035 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 }
Jerry Yud9526692022-02-17 14:23:47 +08008037
8038#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008040 ssl->handshake->ecrs_peer_cert = chain;
8041 chain = NULL;
8042 }
8043#endif
8044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 if (chain != NULL) {
8046 mbedtls_x509_crt_free(chain);
8047 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008048 }
8049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008051}
8052#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8053
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008054static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8055 unsigned char *padbuf, size_t hlen,
8056 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008057{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008058 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008059 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008060#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008061 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008062 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008063 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008064 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008065#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008067 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008068 mbedtls_md_context_t cloned_ctx;
8069 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008070#endif
8071
8072 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008074 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008075 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008076
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008078 ? "client finished"
8079 : "server finished";
8080
8081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008082 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008083
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008084 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008086 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008087 }
8088
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008089 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008091 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008092 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008093 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008094#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008095 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008096
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008097 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008098 if (ret != 0) {
8099 goto exit;
8100 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008101 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008102 if (ret != 0) {
8103 goto exit;
8104 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008105
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008106 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008107 if (ret != 0) {
8108 goto exit;
8109 }
8110#endif /* MBEDTLS_USE_PSA_CRYPTO */
8111
8112 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8113
Jerry Yu615bd6f2022-02-17 14:25:15 +08008114 /*
8115 * TLSv1.2:
8116 * hash = PRF( master, finished_label,
8117 * Hash( handshake ) )[0.11]
8118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008120 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008121
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008123
Paul Elliottecb95be2023-08-11 16:41:04 +01008124 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008125
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008126 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008127
8128exit:
8129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008130 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008131 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008132#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008133 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008134 return ret;
8135#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008136}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008137
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008138#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008139static int ssl_calc_finished_tls_sha256(
8140 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8141{
8142 unsigned char padbuf[32];
8143 return ssl_calc_finished_tls_generic(ssl,
8144#if defined(MBEDTLS_USE_PSA_CRYPTO)
8145 &ssl->handshake->fin_sha256_psa,
8146#else
8147 &ssl->handshake->fin_sha256,
8148#endif
8149 padbuf, sizeof(padbuf),
8150 buf, from);
8151}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008152#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008153
Jerry Yub7ba49e2022-02-17 14:25:53 +08008154
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008155#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008156static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008158{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008159 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008160 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008162 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008163#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008164 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008165#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008166 padbuf, sizeof(padbuf),
8167 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008168}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008169#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008170
Gilles Peskine449bd832023-01-11 14:50:10 +01008171void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008172{
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008174
8175 /*
8176 * Free our handshake params
8177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 mbedtls_ssl_handshake_free(ssl);
8179 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008180 ssl->handshake = NULL;
8181
8182 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008183 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008185 if (ssl->transform) {
8186 mbedtls_ssl_transform_free(ssl->transform);
8187 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008188 }
8189 ssl->transform = ssl->transform_negotiate;
8190 ssl->transform_negotiate = NULL;
8191
Gilles Peskine449bd832023-01-11 14:50:10 +01008192 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008193}
8194
Gilles Peskine449bd832023-01-11 14:50:10 +01008195void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008196{
8197 int resume = ssl->handshake->resume;
8198
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008200
8201#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008202 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008203 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8204 ssl->renego_records_seen = 0;
8205 }
8206#endif
8207
8208 /*
8209 * Free the previous session and switch in the current one
8210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008212#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8213 /* RFC 7366 3.1: keep the EtM state */
8214 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008216#endif
8217
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 mbedtls_ssl_session_free(ssl->session);
8219 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008220 }
8221 ssl->session = ssl->session_negotiate;
8222 ssl->session_negotiate = NULL;
8223
8224 /*
8225 * Add cache entry
8226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008228 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 resume == 0) {
8230 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8231 ssl->session->id,
8232 ssl->session->id_len,
8233 ssl->session) != 0) {
8234 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8235 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008236 }
8237
8238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8240 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008241 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008243
8244 /* Keep last flight around in case we need to resend it:
8245 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8247 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008248#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008250
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008251 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008252
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008254}
8255
Gilles Peskine449bd832023-01-11 14:50:10 +01008256int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008257{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008258 int ret;
8259 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008260
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008262
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008264
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008265 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8266 if (ret != 0) {
8267 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8268 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008269
8270 /*
8271 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8272 * may define some other value. Currently (early 2016), no defined
8273 * ciphersuite does this (and this is unlikely to change as activity has
8274 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8275 */
8276 hash_len = 12;
8277
8278#if defined(MBEDTLS_SSL_RENEGOTIATION)
8279 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008281#endif
8282
8283 ssl->out_msglen = 4 + hash_len;
8284 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8285 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8286
8287 /*
8288 * In case of session resuming, invert the client and server
8289 * ChangeCipherSpec messages order.
8290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008292#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008294 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008296#endif
8297#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008299 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008301#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008303 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008305
8306 /*
8307 * Switch to our negotiated transform and session parameters for outbound
8308 * data.
8309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008311
8312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008314 unsigned char i;
8315
8316 /* Remember current epoch settings for resending */
8317 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8319 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008320
8321 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008323
8324
8325 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 for (i = 2; i > 0; i--) {
8327 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008328 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 }
8330 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008331
8332 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 if (i == 0) {
8334 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8335 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008336 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008338#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008340
8341 ssl->transform_out = ssl->transform_negotiate;
8342 ssl->session_out = ssl->session_negotiate;
8343
8344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8346 mbedtls_ssl_send_flight_completed(ssl);
8347 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008348#endif
8349
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8351 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8352 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008353 }
8354
8355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8357 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8358 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8359 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008360 }
8361#endif
8362
Gilles Peskine449bd832023-01-11 14:50:10 +01008363 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008364
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008366}
8367
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008368#define SSL_MAX_HASH_LEN 12
8369
Gilles Peskine449bd832023-01-11 14:50:10 +01008370int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008371{
8372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8373 unsigned int hash_len = 12;
8374 unsigned char buf[SSL_MAX_HASH_LEN];
8375
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008377
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008378 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8379 if (ret != 0) {
8380 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8381 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8384 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008385 goto exit;
8386 }
8387
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8389 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8390 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008392 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8393 goto exit;
8394 }
8395
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8397 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8398 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008399 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8400 goto exit;
8401 }
8402
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8404 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8405 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8406 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008407 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8408 goto exit;
8409 }
8410
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8412 buf, hash_len) != 0) {
8413 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8414 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8415 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008416 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8417 goto exit;
8418 }
8419
8420#if defined(MBEDTLS_SSL_RENEGOTIATION)
8421 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008423#endif
8424
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008426#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008428 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008430#endif
8431#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008433 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008435#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008437 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008439
8440#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8442 mbedtls_ssl_recv_flight_completed(ssl);
8443 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008444#endif
8445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008447
8448exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 mbedtls_platform_zeroize(buf, hash_len);
8450 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008451}
8452
Jerry Yu392112c2022-02-17 14:34:10 +08008453#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8454/*
8455 * Helper to get TLS 1.2 PRF from ciphersuite
8456 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008458static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008459{
Jerry Yu392112c2022-02-17 14:34:10 +08008460 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008462#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8464 return tls_prf_sha384;
8465 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008466#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008467#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008468 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8470 return tls_prf_sha256;
8471 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008472 }
8473#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008474#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008475 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008476 (void) ciphersuite_info;
8477#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008478
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008480}
8481#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008484{
8485 ((void) tls_prf);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008486#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 if (tls_prf == tls_prf_sha384) {
8488 return MBEDTLS_SSL_TLS_PRF_SHA384;
8489 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008490#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008491#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 if (tls_prf == tls_prf_sha256) {
8493 return MBEDTLS_SSL_TLS_PRF_SHA256;
8494 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008495#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008497}
8498
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008499/*
8500 * Populate a transform structure with session keys and all the other
8501 * necessary information.
8502 *
8503 * Parameters:
8504 * - [in/out]: transform: structure to populate
8505 * [in] must be just initialised with mbedtls_ssl_transform_init()
8506 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8507 * - [in] ciphersuite
8508 * - [in] master
8509 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008510 * - [in] tls_prf: pointer to PRF to use for key derivation
8511 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008512 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008513 * - [in] endpoint: client or server
8514 * - [in] ssl: used for:
8515 * - ssl->conf->{f,p}_export_keys
8516 * [in] optionally used for:
8517 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8518 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008519MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008520static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8521 int ciphersuite,
8522 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008523#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008525#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 ssl_tls_prf_t tls_prf,
8527 const unsigned char randbytes[64],
8528 mbedtls_ssl_protocol_version tls_version,
8529 unsigned endpoint,
8530 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008531{
8532 int ret = 0;
8533 unsigned char keyblk[256];
8534 unsigned char *key1;
8535 unsigned char *key2;
8536 unsigned char *mac_enc;
8537 unsigned char *mac_dec;
8538 size_t mac_key_len = 0;
8539 size_t iv_copy_len;
8540 size_t keylen;
8541 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008542 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008543#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008544 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008545 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008546#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008547
8548#if defined(MBEDTLS_USE_PSA_CRYPTO)
8549 psa_key_type_t key_type;
8550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8551 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008552 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008553 size_t key_bits;
8554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8555#endif
8556
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008557 /*
8558 * Some data just needs copying into the structure
8559 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008560#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008561 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008562#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008563 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008564
8565#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008567#endif
8568
8569#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008571 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8572 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008574 }
8575#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8576
8577 /*
8578 * Get various info structures
8579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8581 if (ciphersuite_info == NULL) {
8582 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8583 ciphersuite));
8584 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008585 }
8586
Neil Armstrongab555e02022-04-04 11:07:59 +02008587 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008588#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008590#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008592
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008594 transform->taglen =
8595 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008597
8598#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008599 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008600 transform->taglen,
8601 &alg,
8602 &key_type,
8603 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008604 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008606 goto end;
8607 }
8608#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008609 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 if (cipher_info == NULL) {
8611 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8612 ciphersuite_info->cipher));
8613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008614 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008615#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008616
Neil Armstronge4512952022-03-08 09:08:22 +01008617#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008618 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008620 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 (unsigned) ciphersuite_info->mac));
8622 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008623 }
8624#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008625 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 if (md_info == NULL) {
8627 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8628 (unsigned) ciphersuite_info->mac));
8629 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008630 }
Neil Armstronge4512952022-03-08 09:08:22 +01008631#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008632
8633#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8634 /* Copy own and peer's CID if the use of the CID
8635 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8637 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008638
8639 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8641 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8642 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008643
8644 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8646 ssl->handshake->peer_cid_len);
8647 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8648 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008649 }
8650#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8651
8652 /*
8653 * Compute key block using the PRF
8654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8656 if (ret != 0) {
8657 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8658 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008659 }
8660
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8662 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8663 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8664 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8665 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008666
8667 /*
8668 * Determine the appropriate key, IV and MAC length.
8669 */
8670
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008671#if defined(MBEDTLS_USE_PSA_CRYPTO)
8672 keylen = PSA_BITS_TO_BYTES(key_bits);
8673#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008675#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008676
Valerio Settie5707042023-10-11 11:54:42 +02008677#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008679 size_t explicit_ivlen;
8680
8681 transform->maclen = 0;
8682 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008683
8684 /* All modes haves 96-bit IVs, but the length of the static parts vary
8685 * with mode and version:
8686 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8687 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8688 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8689 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8690 * sequence number).
8691 */
8692 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008693
8694 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008695#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008697#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8699 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008700#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008703 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008705 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008707
8708 /* Minimum length of encrypted record */
8709 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8710 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 } else
Valerio Settie5707042023-10-11 11:54:42 +02008712#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008713#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008715 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008717#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008719#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008720 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008721#endif /* MBEDTLS_USE_PSA_CRYPTO */
8722
8723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008724 /* Get MAC length */
8725 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8726#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008727 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8729 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8730 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008731 goto end;
8732 }
8733
8734 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008736#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008737 transform->maclen = mac_key_len;
8738
8739 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008740#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008742#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008743 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008745
8746 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008748 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008750 /*
8751 * GenericBlockCipher:
8752 * 1. if EtM is in use: one block plus MAC
8753 * otherwise: * first multiple of blocklen greater than maclen
8754 * 2. IV
8755 */
8756#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008758 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 + block_size;
8760 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008761#endif
8762 {
8763 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 + block_size
8765 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766 }
8767
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008769 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 } else {
8771 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008772 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8773 goto end;
8774 }
8775 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008777#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8778 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8780 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008781 }
8782
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8784 (unsigned) keylen,
8785 (unsigned) transform->minlen,
8786 (unsigned) transform->ivlen,
8787 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008788
8789 /*
8790 * Finally setup the cipher contexts, IVs and MAC secrets.
8791 */
8792#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008794 key1 = keyblk + mac_key_len * 2;
8795 key2 = keyblk + mac_key_len * 2 + keylen;
8796
8797 mac_enc = keyblk;
8798 mac_dec = keyblk + mac_key_len;
8799
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 iv_copy_len = (transform->fixed_ivlen) ?
8801 transform->fixed_ivlen : transform->ivlen;
8802 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8803 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8804 iv_copy_len);
8805 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008806#endif /* MBEDTLS_SSL_CLI_C */
8807#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008809 key1 = keyblk + mac_key_len * 2 + keylen;
8810 key2 = keyblk + mac_key_len * 2;
8811
8812 mac_enc = keyblk + mac_key_len;
8813 mac_dec = keyblk;
8814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 iv_copy_len = (transform->fixed_ivlen) ?
8816 transform->fixed_ivlen : transform->ivlen;
8817 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8818 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8819 iv_copy_len);
8820 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008821#endif /* MBEDTLS_SSL_SRV_C */
8822 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008824 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8825 goto end;
8826 }
8827
Paul Elliott4fb19552023-10-18 12:15:30 +01008828 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 ssl->f_export_keys(ssl->p_export_keys,
8830 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8831 master, 48,
8832 randbytes + 32,
8833 randbytes,
8834 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008835 }
8836
8837#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008838 transform->psa_alg = alg;
8839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8841 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8842 psa_set_key_algorithm(&attributes, alg);
8843 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008844
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 if ((status = psa_import_key(&attributes,
8846 key1,
8847 PSA_BITS_TO_BYTES(key_bits),
8848 &transform->psa_key_enc)) != PSA_SUCCESS) {
8849 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008850 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008852 goto end;
8853 }
8854
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 if ((status = psa_import_key(&attributes,
8858 key2,
8859 PSA_BITS_TO_BYTES(key_bits),
8860 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008861 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008863 goto end;
8864 }
8865 }
8866#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8868 cipher_info)) != 0) {
8869 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008870 goto end;
8871 }
8872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8874 cipher_info)) != 0) {
8875 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008876 goto end;
8877 }
8878
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8880 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8881 MBEDTLS_ENCRYPT)) != 0) {
8882 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008883 goto end;
8884 }
8885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8887 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8888 MBEDTLS_DECRYPT)) != 0) {
8889 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008890 goto end;
8891 }
8892
8893#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8895 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8896 MBEDTLS_PADDING_NONE)) != 0) {
8897 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008898 goto end;
8899 }
8900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8902 MBEDTLS_PADDING_NONE)) != 0) {
8903 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008904 goto end;
8905 }
8906 }
8907#endif /* MBEDTLS_CIPHER_MODE_CBC */
8908#endif /* MBEDTLS_USE_PSA_CRYPTO */
8909
Neil Armstrong29c0c042022-03-17 17:47:28 +01008910#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8911 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8912 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008914#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8918 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8919 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008920
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 if ((status = psa_import_key(&attributes,
8922 mac_enc, mac_key_len,
8923 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008924 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008926 goto end;
8927 }
8928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8930 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008931#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008933#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008935 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008936 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8937 PSA_KEY_USAGE_VERIFY_HASH);
8938 } else {
8939 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8940 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008941
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 if ((status = psa_import_key(&attributes,
8943 mac_dec, mac_key_len,
8944 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008945 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008947 goto end;
8948 }
8949#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8951 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008952 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 }
8954 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8955 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008956 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008958#endif /* MBEDTLS_USE_PSA_CRYPTO */
8959 }
8960#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8961
8962 ((void) mac_dec);
8963 ((void) mac_enc);
8964
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008965end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8967 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008968}
8969
Valerio Settia08b1a42022-11-17 15:10:02 +01008970#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8971 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008972int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 psa_pake_operation_t *pake_ctx,
8974 const unsigned char *buf,
8975 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008976{
8977 psa_status_t status;
8978 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008979 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008980 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8981 * At round two perform a single cycle
8982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008984
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 for (; remaining_steps > 0; remaining_steps--) {
8986 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008987 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008989 /* Length is stored at the first byte */
8990 size_t length = buf[input_offset];
8991 input_offset += 1;
8992
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008994 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8995 }
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 status = psa_pake_input(pake_ctx, step,
8998 buf + input_offset, length);
8999 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009000 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009001 }
9002
9003 input_offset += length;
9004 }
9005 }
9006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009008 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009012}
9013
Valerio Setti6b3dab02022-11-17 17:14:54 +01009014int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 psa_pake_operation_t *pake_ctx,
9016 unsigned char *buf,
9017 size_t len, size_t *olen,
9018 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009019{
9020 psa_status_t status;
9021 size_t output_offset = 0;
9022 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009023 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009024 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9025 * At round two perform a single cycle
9026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 for (; remaining_steps > 0; remaining_steps--) {
9030 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9031 step <= PSA_PAKE_STEP_ZK_PROOF;
9032 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009033 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009034 * For each step, prepend 1 byte with the length of the data as
9035 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 status = psa_pake_output(pake_ctx, step,
9038 buf + output_offset + 1,
9039 len - output_offset - 1,
9040 &output_len);
9041 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009042 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009043 }
9044
Valerio Setti99d88c12022-11-22 16:03:43 +01009045 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009046
9047 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009048 }
9049 }
9050
9051 *olen = output_offset;
9052
Gilles Peskine449bd832023-01-11 14:50:10 +01009053 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009054}
Valerio Settia08b1a42022-11-17 15:10:02 +01009055#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9056
Jerry Yuee40f9d2022-02-17 14:55:16 +08009057#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009058int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9059 unsigned char *hash, size_t *hashlen,
9060 unsigned char *data, size_t data_len,
9061 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009062{
9063 psa_status_t status;
9064 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009065 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 if ((status = psa_hash_setup(&hash_operation,
9070 hash_alg)) != PSA_SUCCESS) {
9071 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009072 goto exit;
9073 }
9074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9076 64)) != PSA_SUCCESS) {
9077 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009078 goto exit;
9079 }
9080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 if ((status = psa_hash_update(&hash_operation,
9082 data, data_len)) != PSA_SUCCESS) {
9083 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009084 goto exit;
9085 }
9086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9088 hashlen)) != PSA_SUCCESS) {
9089 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9090 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009091 }
9092
9093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 if (status != PSA_SUCCESS) {
9095 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9096 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9097 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009098 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009100 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9101 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009103 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009105 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009107 }
9108 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009110}
9111
9112#else
9113
Gilles Peskine449bd832023-01-11 14:50:10 +01009114int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9115 unsigned char *hash, size_t *hashlen,
9116 unsigned char *data, size_t data_len,
9117 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009118{
9119 int ret = 0;
9120 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9122 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009123
Gilles Peskine449bd832023-01-11 14:50:10 +01009124 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009127
9128 /*
9129 * digitally-signed struct {
9130 * opaque client_random[32];
9131 * opaque server_random[32];
9132 * ServerDHParams params;
9133 * };
9134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9136 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009137 goto exit;
9138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9140 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009141 goto exit;
9142 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9144 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009145 goto exit;
9146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9148 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009149 goto exit;
9150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9152 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009153 goto exit;
9154 }
9155
9156exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 if (ret != 0) {
9160 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9161 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9162 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009165}
9166#endif /* MBEDTLS_USE_PSA_CRYPTO */
9167
Jerry Yud9d91da2022-02-17 14:57:06 +08009168#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9169
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009170/* Find the preferred hash for a given signature algorithm. */
9171unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 mbedtls_ssl_context *ssl,
9173 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009174{
Gabor Mezei078e8032022-04-27 21:17:56 +02009175 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009176 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9179 return MBEDTLS_SSL_HASH_NONE;
9180 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009183 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9185 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009186 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9188 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009189
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009190 mbedtls_md_type_t md_alg =
9191 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9192 if (md_alg == MBEDTLS_MD_NONE) {
9193 continue;
9194 }
9195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009197#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009199 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009200 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009201
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9203 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9204 PSA_ALG_ECDSA(psa_hash_alg),
9205 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009206 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009208
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9210 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9211 PSA_ALG_RSA_PKCS1V15_SIGN(
9212 psa_hash_alg),
9213 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009214 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009216 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009217#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009218
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009220 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009221 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009222
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009224}
9225
9226#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9227
Jerry Yudc7bd172022-02-17 13:44:15 +08009228#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9229
XiaokangQian75d40ef2022-04-20 11:05:24 +00009230int mbedtls_ssl_validate_ciphersuite(
9231 const mbedtls_ssl_context *ssl,
9232 const mbedtls_ssl_ciphersuite_t *suite_info,
9233 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009234 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009235{
9236 (void) ssl;
9237
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 if (suite_info == NULL) {
9239 return -1;
9240 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 if ((suite_info->min_tls_version > max_tls_version) ||
9243 (suite_info->max_tls_version < min_tls_version)) {
9244 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009245 }
9246
XiaokangQian060d8672022-04-21 09:24:56 +00009247#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009248#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009249#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9251 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009252#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009253 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9254 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009255#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009256 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009258 }
9259#endif
9260
9261 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9262#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9264 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9265 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009266 }
9267#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9268#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009271}
9272
Ronald Crone68ab4f2022-10-05 12:46:29 +02009273#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009274/*
9275 * Function for writing a signature algorithm extension.
9276 *
9277 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9278 * value (TLS 1.3 RFC8446):
9279 * enum {
9280 * ....
9281 * ecdsa_secp256r1_sha256( 0x0403 ),
9282 * ecdsa_secp384r1_sha384( 0x0503 ),
9283 * ecdsa_secp521r1_sha512( 0x0603 ),
9284 * ....
9285 * } SignatureScheme;
9286 *
9287 * struct {
9288 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9289 * } SignatureSchemeList;
9290 *
9291 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9292 * value (TLS 1.2 RFC5246):
9293 * enum {
9294 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9295 * sha512(6), (255)
9296 * } HashAlgorithm;
9297 *
9298 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9299 * SignatureAlgorithm;
9300 *
9301 * struct {
9302 * HashAlgorithm hash;
9303 * SignatureAlgorithm signature;
9304 * } SignatureAndHashAlgorithm;
9305 *
9306 * SignatureAndHashAlgorithm
9307 * supported_signature_algorithms<2..2^16-2>;
9308 *
9309 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9310 * generalization of the TLS 1.2 signature algorithm extension.
9311 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9312 * `SignatureScheme` field of TLS 1.3
9313 *
9314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009315int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9316 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009317{
9318 unsigned char *p = buf;
9319 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9320 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9321
9322 *out_len = 0;
9323
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009325
9326 /* Check if we have space for header and length field:
9327 * - extension_type (2 bytes)
9328 * - extension_data_length (2 bytes)
9329 * - supported_signature_algorithms_length (2 bytes)
9330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009332 p += 6;
9333
9334 /*
9335 * Write supported_signature_algorithms
9336 */
9337 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9339 if (sig_alg == NULL) {
9340 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9341 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009342
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9344 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9345 *sig_alg,
9346 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9347 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009348 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009349 }
9350 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9351 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009352 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009353 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9354 *sig_alg,
9355 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009356 }
9357
9358 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009359 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 if (supported_sig_alg_len == 0) {
9361 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9362 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009363 }
9364
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9366 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9367 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009368
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009369 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009370
9371#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009372 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009373#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009374
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009376}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009377#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009378
XiaokangQian40a35232022-05-07 09:02:40 +00009379#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009380/*
9381 * mbedtls_ssl_parse_server_name_ext
9382 *
9383 * Structure of server_name extension:
9384 *
9385 * enum {
9386 * host_name(0), (255)
9387 * } NameType;
9388 * opaque HostName<1..2^16-1>;
9389 *
9390 * struct {
9391 * NameType name_type;
9392 * select (name_type) {
9393 * case host_name: HostName;
9394 * } name;
9395 * } ServerName;
9396 * struct {
9397 * ServerName server_name_list<1..2^16-1>
9398 * } ServerNameList;
9399 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009400MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009401int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9402 const unsigned char *buf,
9403 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009404{
9405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9406 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009407 size_t server_name_list_len, hostname_len;
9408 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009409
Gilles Peskine449bd832023-01-11 14:50:10 +01009410 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009411
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9413 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009414 p += 2;
9415
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009417 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 while (p < server_name_list_end) {
9419 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9420 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9421 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9422 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009423
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009425 /* sni_name is intended to be used only during the parsing of the
9426 * ClientHello message (it is reset to NULL before the end of
9427 * the message parsing). Thus it is ok to just point to the
9428 * reception buffer and not make a copy of it.
9429 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009430 ssl->handshake->sni_name = p + 3;
9431 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009432 if (ssl->conf->f_sni == NULL) {
9433 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009434 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9436 ssl, p + 3, hostname_len);
9437 if (ret != 0) {
9438 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9439 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9440 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9441 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9442 }
9443 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009444 }
9445
9446 p += hostname_len + 3;
9447 }
9448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009450}
9451#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9452
XiaokangQianacb39922022-06-17 10:18:48 +00009453#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009454MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009455int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9456 const unsigned char *buf,
9457 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009458{
9459 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009460 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009461 const unsigned char *protocol_name_list;
9462 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009463 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009464
9465 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 if (ssl->conf->alpn_list == NULL) {
9467 return 0;
9468 }
XiaokangQianacb39922022-06-17 10:18:48 +00009469
9470 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009471 * RFC7301, section 3.1
9472 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009473 *
XiaokangQianc7403452022-06-23 03:24:12 +00009474 * struct {
9475 * ProtocolName protocol_name_list<2..2^16-1>
9476 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009477 */
9478
XiaokangQianc7403452022-06-23 03:24:12 +00009479 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009480 * protocol_name_list_len 2 bytes
9481 * protocol_name_len 1 bytes
9482 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009485
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009487 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009489 protocol_name_list = p;
9490 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009491
9492 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009494 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9496 protocol_name_len);
9497 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009498 MBEDTLS_SSL_PEND_FATAL_ALERT(
9499 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9501 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009502 }
9503
9504 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009505 }
9506
9507 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9509 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009512 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009513 if (protocol_name_len == alpn_len &&
9514 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009515 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009517 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009518
9519 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009520 }
9521 }
9522
XiaokangQian95d5f542022-06-24 02:29:26 +00009523 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009524 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9526 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9527 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009528}
9529
Gilles Peskine449bd832023-01-11 14:50:10 +01009530int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9531 unsigned char *buf,
9532 unsigned char *end,
9533 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009534{
9535 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009536 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009537 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009538
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 if (ssl->alpn_chosen == NULL) {
9540 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009541 }
9542
Gilles Peskine449bd832023-01-11 14:50:10 +01009543 protocol_name_len = strlen(ssl->alpn_chosen);
9544 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009545
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009547 /*
9548 * 0 . 1 ext identifier
9549 * 2 . 3 ext length
9550 * 4 . 5 protocol list length
9551 * 6 . 6 protocol name length
9552 * 7 . 7+n protocol name
9553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009555
XiaokangQian95d5f542022-06-24 02:29:26 +00009556 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009557
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9559 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009560 /* Note: the length of the chosen protocol has been checked to be less
9561 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009564
Gilles Peskine449bd832023-01-11 14:50:10 +01009565 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009566
9567#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009569#endif
9570
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009572}
9573#endif /* MBEDTLS_SSL_ALPN */
9574
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009575#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009576 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009577 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009578 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009579int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9580 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009581{
9582 /* Initialize to suppress unnecessary compiler warning */
9583 size_t hostname_len = 0;
9584
9585 /* Check if new hostname is valid before
9586 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 if (hostname != NULL) {
9588 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009589
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9592 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009593 }
9594
9595 /* Now it's clear that we will overwrite the old hostname,
9596 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009597 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009598 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009600 }
9601
9602 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009604 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 } else {
9606 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9607 if (session->hostname == NULL) {
9608 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9609 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009612 }
9613
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009615}
Xiaokang Qian03409292022-10-12 02:49:52 +00009616#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9617 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009618 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009619 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009620
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009621#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9622 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009623int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9624 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009625{
9626 size_t alpn_len = 0;
9627
9628 if (alpn != NULL) {
9629 alpn_len = strlen(alpn);
9630
9631 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9633 }
9634 }
9635
9636 if (session->ticket_alpn != NULL) {
9637 mbedtls_zeroize_and_free(session->ticket_alpn,
9638 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009639 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009640 }
9641
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009642 if (alpn != NULL) {
9643 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009644 if (session->ticket_alpn == NULL) {
9645 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9646 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009647 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009648 }
9649
9650 return 0;
9651}
9652#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009653
9654/*
9655 * The following functions are used by 1.2 and 1.3, client and server.
9656 */
9657#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9658int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9659 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9660 int recv_endpoint,
9661 mbedtls_ssl_protocol_version tls_version,
9662 uint32_t *flags)
9663{
9664 int ret = 0;
9665 unsigned int usage = 0;
9666 const char *ext_oid;
9667 size_t ext_len;
9668
9669 /*
9670 * keyUsage
9671 */
9672
9673 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9674 * to check what a compliant client will think while choosing which cert
9675 * to send to the client. */
9676#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9677 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9678 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9679 /* TLS 1.2 server part of the key exchange */
9680 switch (ciphersuite->key_exchange) {
9681 case MBEDTLS_KEY_EXCHANGE_RSA:
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009682 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9683 break;
9684
9685 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9686 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9687 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9688 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9689 break;
9690
9691 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9692 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9693 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9694 break;
9695
9696 /* Don't use default: we want warnings when adding new values */
9697 case MBEDTLS_KEY_EXCHANGE_NONE:
9698 case MBEDTLS_KEY_EXCHANGE_PSK:
9699 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9700 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9701 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9702 usage = 0;
9703 }
9704 } else
9705#endif
9706 {
9707 /* This is either TLS 1.3 authentication, which always uses signatures,
9708 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9709 * options we implement, both using signatures. */
9710 (void) tls_version;
9711 (void) ciphersuite;
9712 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9713 }
9714
9715 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9716 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9717 ret = -1;
9718 }
9719
9720 /*
9721 * extKeyUsage
9722 */
9723
9724 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9725 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9726 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9727 } else {
9728 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9729 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9730 }
9731
9732 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9733 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9734 ret = -1;
9735 }
9736
9737 return ret;
9738}
9739
9740int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9741 int authmode,
9742 mbedtls_x509_crt *chain,
9743 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9744 void *rs_ctx)
9745{
9746 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9747 return 0;
9748 }
9749
9750 /*
9751 * Primary check: use the appropriate X.509 verification function
9752 */
9753 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9754 void *p_vrfy;
9755 if (ssl->f_vrfy != NULL) {
9756 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9757 f_vrfy = ssl->f_vrfy;
9758 p_vrfy = ssl->p_vrfy;
9759 } else {
9760 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9761 f_vrfy = ssl->conf->f_vrfy;
9762 p_vrfy = ssl->conf->p_vrfy;
9763 }
9764
9765 int ret = 0;
9766 int have_ca_chain_or_callback = 0;
9767#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9768 if (ssl->conf->f_ca_cb != NULL) {
9769 ((void) rs_ctx);
9770 have_ca_chain_or_callback = 1;
9771
9772 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9773 ret = mbedtls_x509_crt_verify_with_ca_cb(
9774 chain,
9775 ssl->conf->f_ca_cb,
9776 ssl->conf->p_ca_cb,
9777 ssl->conf->cert_profile,
9778 ssl->hostname,
9779 &ssl->session_negotiate->verify_result,
9780 f_vrfy, p_vrfy);
9781 } else
9782#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9783 {
9784 mbedtls_x509_crt *ca_chain;
9785 mbedtls_x509_crl *ca_crl;
9786#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9787 if (ssl->handshake->sni_ca_chain != NULL) {
9788 ca_chain = ssl->handshake->sni_ca_chain;
9789 ca_crl = ssl->handshake->sni_ca_crl;
9790 } else
9791#endif
9792 {
9793 ca_chain = ssl->conf->ca_chain;
9794 ca_crl = ssl->conf->ca_crl;
9795 }
9796
9797 if (ca_chain != NULL) {
9798 have_ca_chain_or_callback = 1;
9799 }
9800
9801 ret = mbedtls_x509_crt_verify_restartable(
9802 chain,
9803 ca_chain, ca_crl,
9804 ssl->conf->cert_profile,
9805 ssl->hostname,
9806 &ssl->session_negotiate->verify_result,
9807 f_vrfy, p_vrfy, rs_ctx);
9808 }
9809
9810 if (ret != 0) {
9811 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9812 }
9813
9814#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9815 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9816 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9817 }
9818#endif
9819
9820 /*
9821 * Secondary checks: always done, but change 'ret' only if it was 0
9822 */
9823
9824 /* With TLS 1.2 and ECC certs, check that the curve used by the
9825 * certificate is on our list of acceptable curves.
9826 *
9827 * With TLS 1.3 this is not needed because the curve is part of the
9828 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9829 * we validate the signature made with the key associated to this cert.
9830 */
9831#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9832 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
9833 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9834 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9835 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9836 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9837 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9838 if (ret == 0) {
9839 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9840 }
9841 }
9842 }
9843#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9844
9845 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9846 if (mbedtls_ssl_check_cert_usage(chain,
9847 ciphersuite_info,
9848 ssl->conf->endpoint,
9849 ssl->tls_version,
9850 &ssl->session_negotiate->verify_result) != 0) {
9851 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9852 if (ret == 0) {
9853 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9854 }
9855 }
9856
9857 /* With authmode optional, we want to keep going if the certificate was
9858 * unacceptable, but still fail on other errors (out of memory etc),
9859 * including fatal errors from the f_vrfy callback.
9860 *
9861 * The only acceptable errors are:
9862 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9863 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9864 * Anything else is a fatal error. */
9865 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9866 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9867 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9868 ret = 0;
9869 }
9870
9871 /* Return a specific error as this is a user error: inconsistent
9872 * configuration - can't verify without trust anchors. */
9873 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9874 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9875 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9876 }
9877
9878 if (ret != 0) {
9879 uint8_t alert;
9880
9881 /* The certificate may have been rejected for several reasons.
9882 Pick one and send the corresponding alert. Which alert to send
9883 may be a subject of debate in some cases. */
9884 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9885 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9886 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9887 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9888 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9889 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9890 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9891 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9892 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9893 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9894 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9895 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9896 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9897 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9898 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9899 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9900 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9901 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9902 } else {
9903 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9904 }
9905 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9906 alert);
9907 }
9908
9909#if defined(MBEDTLS_DEBUG_C)
9910 if (ssl->session_negotiate->verify_result != 0) {
9911 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9912 (unsigned int) ssl->session_negotiate->verify_result));
9913 } else {
9914 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9915 }
9916#endif /* MBEDTLS_DEBUG_C */
9917
9918 return ret;
9919}
9920#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922#endif /* MBEDTLS_SSL_TLS_C */