blob: 450c397c78d1a193b7b0d8c79ac37a6c85865dbe [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;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000348 size_t hshdr_in = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 written_in = ssl->in_msg - ssl->in_buf;
351 iv_offset_in = ssl->in_iv - ssl->in_buf;
352 len_offset_in = ssl->in_len - ssl->in_buf;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000353 hdr_in = ssl->in_hdr - ssl->in_buf;
354 if (ssl->in_hshdr != NULL) {
355 hshdr_in = ssl->in_hshdr - ssl->in_buf;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->in_buf_len < in_buf_new_len) {
360 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
364 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200371 written_out = ssl->out_msg - ssl->out_buf;
372 iv_offset_out = ssl->out_iv - ssl->out_buf;
373 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200375 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 ssl->out_buf_len < out_buf_new_len) {
377 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
378 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
379 } else {
380 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
381 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200382 modified = 1;
383 }
384 }
385 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200387 /* Update pointers here to avoid doing it twice. */
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000388 ssl->in_hdr = ssl->in_buf + hdr_in;
389 mbedtls_ssl_update_in_pointers(ssl);
390 mbedtls_ssl_reset_out_pointers(ssl);
391
Andrzej Kurek4a063792020-10-21 15:08:44 +0200392 /* Fields below might not be properly updated with record
393 * splitting or with CID, so they are manually updated here. */
394 ssl->out_msg = ssl->out_buf + written_out;
395 ssl->out_len = ssl->out_buf + len_offset_out;
396 ssl->out_iv = ssl->out_buf + iv_offset_out;
397
398 ssl->in_msg = ssl->in_buf + written_in;
399 ssl->in_len = ssl->in_buf + len_offset_in;
400 ssl->in_iv = ssl->in_buf + iv_offset_in;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +0000401 if (ssl->in_hshdr != NULL) {
402 ssl->in_hshdr = ssl->in_buf + hshdr_in;
403 }
Andrzej Kurek4a063792020-10-21 15:08:44 +0200404 }
405}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500406#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
407
Jerry Yudb8c48a2022-01-27 14:54:54 +0800408#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800409
410#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100411typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
412 const char *label,
413 const unsigned char *random, size_t rlen,
414 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800417
418#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
419
420/* Type for the TLS PRF */
421typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
422 const unsigned char *, size_t,
423 unsigned char *, size_t);
424
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200425MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
427 int ciphersuite,
428 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200429#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200431#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 ssl_tls_prf_t tls_prf,
433 const unsigned char randbytes[64],
434 mbedtls_ssl_protocol_version tls_version,
435 unsigned endpoint,
436 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800437
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100438#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200439MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100440static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300441 const char *label,
442 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100444static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
445static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100446
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100447#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100449#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100450MBEDTLS_CHECK_RETURN_CRITICAL
451static int tls_prf_sha384(const unsigned char *secret, size_t slen,
452 const char *label,
453 const unsigned char *random, size_t rlen,
454 unsigned char *dstbuf, size_t dlen);
455
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100456static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
457static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100458#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460MBEDTLS_CHECK_RETURN_CRITICAL
461static int ssl_tls12_session_load(mbedtls_ssl_session *session,
462 const unsigned char *buf,
463 size_t len);
464#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
465
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100466static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100467
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100468#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100469static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100470#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100471
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100472#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100473static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100474#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100475
476int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
477 const unsigned char *secret, size_t slen,
478 const char *label,
479 const unsigned char *random, size_t rlen,
480 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300481{
482 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300485#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100486#if defined(PSA_WANT_ALG_SHA_384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487 case MBEDTLS_SSL_TLS_PRF_SHA384:
488 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 break;
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100490#endif /* PSA_WANT_ALG_SHA_384*/
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100491#if defined(PSA_WANT_ALG_SHA_256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300492 case MBEDTLS_SSL_TLS_PRF_SHA256:
493 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 break;
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100495#endif /* PSA_WANT_ALG_SHA_256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300496#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 default:
498 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300499 }
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300502}
503
Jerry Yuc73c6182022-02-08 20:29:25 +0800504#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800506{
507#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (session->peer_cert != NULL) {
509 mbedtls_x509_crt_free(session->peer_cert);
510 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800511 session->peer_cert = NULL;
512 }
513#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800515 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800517 session->peer_cert_digest = NULL;
518 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
519 session->peer_cert_digest_len = 0;
520 }
521#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
522}
523#endif /* MBEDTLS_X509_CRT_PARSE_C */
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800526{
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800528 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800530
531 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800533
534 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800536
537 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800539
540 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800542
543 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800545
546 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800548
549 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800551
552 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800554
555 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800557
558 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800560
561 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800563
564 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800566
567 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800569
570 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800572
573 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800575
576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800578
579 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800581
582 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800584
585 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800587
588 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800590
591 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593
594 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800596
597 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800599
600 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800602
603 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800605
Jan Bruckner151f6422023-02-10 12:45:19 +0100606 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
607 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
608
Jerry Yu7a485c12022-10-31 13:08:18 +0800609 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800611
612 }
613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800615}
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800618{
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800620}
621
Jerry Yud25cab02022-10-31 12:48:30 +0800622#if defined(MBEDTLS_DEBUG_C)
623static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800624 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800625 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
626 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
627 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
628 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
629 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
630 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
631 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
632 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
633 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
634 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
635 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
636 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
637 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
638 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
639 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
640 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
641 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
642 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
643 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
644 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
645 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
646 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
647 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
649 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
650 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100651 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
652 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800653};
654
Chien Wong4e9683e2023-12-28 17:07:43 +0800655static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800656 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
657 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
658 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
659 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
660 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
661 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
662 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
663 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
664 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
665 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
666 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
667 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
668 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
669 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
670 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
671 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
672 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
673 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
674 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
675 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
676 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
677 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
678 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
679 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
680 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
681 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
682 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100683 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
684 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800685};
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800688{
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return extension_name_table[
690 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800691}
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800696 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800698 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800700 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800702 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800704 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800706 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800708 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800710 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800712}
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
715 int level, const char *file, int line,
716 int hs_msg_type, unsigned int extension_type,
717 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800718{
719 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800721 mbedtls_debug_print_msg(
722 ssl, level, file, line,
723 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 ssl_tls13_get_hs_msg_name(hs_msg_type),
725 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800726 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800728 return;
729 }
730
731 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800733 mbedtls_debug_print_msg(
734 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
736 mbedtls_ssl_get_extension_name(extension_type), extension_type,
737 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800738 return;
739 }
740
741 mbedtls_debug_print_msg(
742 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
744 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800745}
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
748 int level, const char *file, int line,
749 int hs_msg_type, uint32_t extensions_mask,
750 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800751{
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 for (unsigned i = 0;
754 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
755 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800756 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800757 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800759 }
760}
761
Pengyu Lvee455c02023-01-12 14:37:24 +0800762#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800763static const char *ticket_flag_name_table[] =
764{
765 [0] = "ALLOW_PSK_RESUMPTION",
766 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
767 [3] = "ALLOW_EARLY_DATA",
768};
769
Pengyu Lv4938a562023-01-16 11:28:49 +0800770void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
771 int level, const char *file, int line,
772 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800773{
774 size_t i;
775
776 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800777 "print ticket_flags (0x%02x)", flags);
778
779 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800780
781 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800782 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800783 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
784 ticket_flag_name_table[i]);
785 }
786 }
787}
788#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
789
Jerry Yud25cab02022-10-31 12:48:30 +0800790#endif /* MBEDTLS_DEBUG_C */
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
793 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800794{
795 ((void) ciphersuite_info);
796
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100797#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800799 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800801#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100802#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800804 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800806#endif
807 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800809 return;
810 }
811}
812
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100813int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100814 unsigned hs_type,
815 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100816{
817 unsigned char hs_hdr[4];
818
819 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
821 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
822 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
823 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100824
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100825 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100826}
827
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100828int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100829 unsigned hs_type,
830 unsigned char const *msg,
831 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100832{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100833 int ret;
834 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100835 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100836 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100837 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100838 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100839}
840
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100841int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800842{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100843#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100844 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845#if defined(MBEDTLS_USE_PSA_CRYPTO)
846 psa_status_t status;
847#else
848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
849#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100850#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800851 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100852#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100853#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800854#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100855 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
856 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200857 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100858 }
859 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
860 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200861 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100862 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800863#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100864 mbedtls_md_free(&ssl->handshake->fin_sha256);
865 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100866 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
867 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
868 0);
869 if (ret != 0) {
870 return ret;
871 }
872 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100873 if (ret != 0) {
874 return ret;
875 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800876#endif
877#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100878#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100880 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
881 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200882 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100883 }
884 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
885 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200886 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100887 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800888#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100889 mbedtls_md_free(&ssl->handshake->fin_sha384);
890 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100891 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
892 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
893 if (ret != 0) {
894 return ret;
895 }
896 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100897 if (ret != 0) {
898 return ret;
899 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800900#endif
901#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100902 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800903}
904
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100905static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100906 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800907{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100908#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100909 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100910#if defined(MBEDTLS_USE_PSA_CRYPTO)
911 psa_status_t status;
912#else
913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
914#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100915#else /* SHA-256 or SHA-384 */
916 ((void) ssl);
917 (void) buf;
918 (void) len;
919#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100920#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800921#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100922 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
923 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200924 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100925 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800926#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100927 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 if (ret != 0) {
929 return ret;
930 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800931#endif
932#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100933#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800934#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100935 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
936 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200937 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100938 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800939#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100940 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100941 if (ret != 0) {
942 return ret;
943 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800944#endif
945#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100946 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800947}
948
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100949#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100950static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100951 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800952{
953#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200954 return mbedtls_md_error_from_psa(psa_hash_update(
955 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800956#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100957 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800958#endif
959}
960#endif
961
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100962#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100963static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100964 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800965{
966#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200967 return mbedtls_md_error_from_psa(psa_hash_update(
968 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800969#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100970 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800971#endif
972}
973#endif
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200976{
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100979#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500980#if defined(MBEDTLS_USE_PSA_CRYPTO)
981 handshake->fin_sha256_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_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500985#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100986#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500987#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500988 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500989#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100990 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200991#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500992#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200993
994 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200998#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200999#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02001000 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001002#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001003#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02001004#if defined(MBEDTLS_USE_PSA_CRYPTO)
1005 handshake->psa_pake_ctx = psa_pake_operation_init();
1006 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
1007#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02001009#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001010#if defined(MBEDTLS_SSL_CLI_C)
1011 handshake->ecjpake_cache = NULL;
1012 handshake->ecjpake_cache_len = 0;
1013#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001014#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001015
Gilles Peskineeccd8882020-03-10 12:19:08 +01001016#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001018#endif
1019
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001020#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1021 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1022#endif
Hanno Becker75173122019-02-06 16:18:31 +00001023
1024#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1025 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001027#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001028}
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001031{
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001033
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001034#if defined(MBEDTLS_USE_PSA_CRYPTO)
1035 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1036 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001037#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1039 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001040#endif
1041
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001042#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001043#if defined(MBEDTLS_USE_PSA_CRYPTO)
1044 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1045 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001046#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_md_init(&transform->md_ctx_enc);
1048 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001049#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001050#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001051}
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056}
1057
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001059static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001060{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1062
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (ssl->transform_negotiate) {
1066 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1067 }
Jerry Yu2e199812022-12-01 18:57:19 +08001068#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->session_negotiate) {
1070 mbedtls_ssl_session_free(ssl->session_negotiate);
1071 }
1072 if (ssl->handshake) {
1073 mbedtls_ssl_handshake_free(ssl);
1074 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001075
Jerry Yu2e199812022-12-01 18:57:19 +08001076#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001077 /*
1078 * Either the pointers are now NULL or cleared properly and can be freed.
1079 * Now allocate missing structures.
1080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (ssl->transform_negotiate == NULL) {
1082 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001083 }
Jerry Yu2e199812022-12-01 18:57:19 +08001084#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (ssl->session_negotiate == NULL) {
1087 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001088 }
Paul Bakker48916f92012-09-16 19:57:18 +00001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (ssl->handshake == NULL) {
1091 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001092 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001093#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1094 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1097 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001098#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001099
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001100 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001103 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 ssl->session_negotiate == NULL) {
1106 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001109 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001110
1111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001113 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001114#endif
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001117 ssl->session_negotiate = NULL;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001120 }
1121
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001122#if defined(MBEDTLS_SSL_EARLY_DATA)
1123#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001124 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001125#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001126#if defined(MBEDTLS_SSL_SRV_C)
1127 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1128#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001129 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001130#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001131
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001132 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_ssl_session_init(ssl->session_negotiate);
1134 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001135
Jerry Yu2e199812022-12-01 18:57:19 +08001136#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001138#endif
1139
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001140 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001141 ret = mbedtls_ssl_reset_checksum(ssl);
1142 if (ret != 0) {
1143 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1144 return ret;
1145 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001146
Jerry Yud0766ec2022-09-22 10:46:57 +08001147#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1148 defined(MBEDTLS_SSL_SRV_C) && \
1149 defined(MBEDTLS_SSL_SESSION_TICKETS)
1150 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001152#endif
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001156 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001159 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001161 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001165 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001166#endif
1167
Brett Warrene0edc842021-08-17 09:53:13 +01001168/*
1169 * curve_list is translated to IANA TLS group identifiers here because
1170 * mbedtls_ssl_conf_curves returns void and so can't return
1171 * any error codes.
1172 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001173#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001174#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1175 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001177 size_t length;
1178 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1179
Thomas Daubney850a0792023-05-22 12:05:03 +01001180 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
Brett Warrene0edc842021-08-17 09:53:13 +01001182
1183 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1185 if (group_list == NULL) {
1186 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1187 }
Brett Warrene0edc842021-08-17 09:53:13 +01001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001190 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 curve_list[i]);
1192 if (tls_id == 0) {
1193 mbedtls_free(group_list);
1194 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001195 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001196 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001197 }
1198
1199 group_list[length] = 0;
1200
1201 ssl->handshake->group_list = group_list;
1202 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001204 ssl->handshake->group_list = ssl->conf->group_list;
1205 ssl->handshake->group_list_heap_allocated = 0;
1206 }
1207#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001208#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001209
Ronald Crone68ab4f2022-10-05 12:46:29 +02001210#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001211#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001212#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001213 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1214 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1216 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001217 const int *md;
1218 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001219 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001220 uint16_t *p;
1221
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001222 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1223 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1224 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1227 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001228 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001230#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001232#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001233
Jerry Yub476a442022-01-21 18:14:45 +08001234#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001236#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1238 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1239 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001240 }
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1243 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1244 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1247 sizeof(uint16_t));
1248 if (ssl->handshake->sig_algs == NULL) {
1249 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1250 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 p = (uint16_t *) ssl->handshake->sig_algs;
1253 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1254 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1255 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001256 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001258#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001260 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001261#endif
1262#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001264 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001265#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001266 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001267 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001268 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001271 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001272 ssl->handshake->sig_algs_heap_allocated = 0;
1273 }
Jerry Yucc539102022-06-27 16:27:35 +08001274#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001275#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001277}
1278
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001279#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001280/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001282static int ssl_cookie_write_dummy(void *ctx,
1283 unsigned char **p, unsigned char *end,
1284 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001285{
1286 ((void) ctx);
1287 ((void) p);
1288 ((void) end);
1289 ((void) cli_id);
1290 ((void) cli_id_len);
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001293}
1294
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001295MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001296static int ssl_cookie_check_dummy(void *ctx,
1297 const unsigned char *cookie, size_t cookie_len,
1298 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001299{
1300 ((void) ctx);
1301 ((void) cookie);
1302 ((void) cookie_len);
1303 ((void) cli_id);
1304 ((void) cli_id_len);
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001307}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001308#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001309
Paul Bakker5121ce52009-01-03 21:22:43 +00001310/*
1311 * Initialize an SSL context
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001314{
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001316}
1317
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001318MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001319static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001320{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001321 const mbedtls_ssl_config *conf = ssl->conf;
1322
Ronald Cron6f135e12021-12-08 16:57:54 +01001323#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1325 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1326 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1327 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001328 }
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1331 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001332 }
1333#endif
1334
1335#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1337 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1338 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001339 }
1340#endif
1341
Ronald Cron6f135e12021-12-08 16:57:54 +01001342#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1344 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1345 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1346 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001347 }
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1350 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001351 }
1352#endif
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1355 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001356}
1357
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001358MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001359static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1360{
1361 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 ret = ssl_conf_version_check(ssl);
1363 if (ret != 0) {
1364 return ret;
1365 }
Jerry Yu60835a82021-08-04 10:13:52 +08001366
Yanray Wangb422cab2023-12-01 16:18:10 +08001367 if (ssl->conf->f_rng == NULL) {
1368 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1369 return MBEDTLS_ERR_SSL_NO_RNG;
1370 }
1371
Jerry Yu60835a82021-08-04 10:13:52 +08001372 /* Space for further checks */
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001375}
1376
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001377/*
1378 * Setup an SSL context
1379 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1382 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Janos Follath865b3eb2019-12-16 11:46:15 +00001384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001385 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1386 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001388 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 if ((ret = ssl_conf_check(ssl)) != 0) {
1391 return ret;
1392 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001393 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001394
Paul Bakker62f2dee2012-09-28 07:31:51 +00001395 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001396 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001397 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001398
1399 /* Set to NULL in case of an error condition */
1400 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001401
Darryl Greenb33cc762019-11-28 14:29:44 +00001402#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1403 ssl->in_buf_len = in_buf_len;
1404#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1406 if (ssl->in_buf == NULL) {
1407 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001408 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001409 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001410 }
1411
Darryl Greenb33cc762019-11-28 14:29:44 +00001412#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1413 ssl->out_buf_len = out_buf_len;
1414#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1416 if (ssl->out_buf == NULL) {
1417 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001418 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001419 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 }
1421
Deomid rojer Ryabkov3dfe75e2025-01-26 10:43:42 +02001422 mbedtls_ssl_reset_in_pointers(ssl);
1423 mbedtls_ssl_reset_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001424
Johan Pascalb62bb512015-12-03 21:56:45 +01001425#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001427#endif
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001430 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001434
1435error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_free(ssl->in_buf);
1437 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001438
1439 ssl->conf = NULL;
1440
Darryl Greenb33cc762019-11-28 14:29:44 +00001441#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1442 ssl->in_buf_len = 0;
1443 ssl->out_buf_len = 0;
1444#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001445 ssl->in_buf = NULL;
1446 ssl->out_buf = NULL;
1447
1448 ssl->in_hdr = NULL;
1449 ssl->in_ctr = NULL;
1450 ssl->in_len = NULL;
1451 ssl->in_iv = NULL;
1452 ssl->in_msg = NULL;
1453
1454 ssl->out_hdr = NULL;
1455 ssl->out_ctr = NULL;
1456 ssl->out_len = NULL;
1457 ssl->out_iv = NULL;
1458 ssl->out_msg = NULL;
1459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001461}
1462
1463/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001464 * Reset an initialized and used SSL context for re-use while retaining
1465 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001466 *
1467 * If partial is non-zero, keep data in the input buffer and client ID.
1468 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1471 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001472{
Darryl Greenb33cc762019-11-28 14:29:44 +00001473#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1474 size_t in_buf_len = ssl->in_buf_len;
1475 size_t out_buf_len = ssl->out_buf_len;
1476#else
1477 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1478 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1479#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001480
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1482 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001483#endif
1484
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001485 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001487
Deomid rojer Ryabkov3dfe75e2025-01-26 10:43:42 +02001488 mbedtls_ssl_reset_in_pointers(ssl);
1489 mbedtls_ssl_reset_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490
1491 /* Reset incoming message parsing */
1492 ssl->in_offt = NULL;
1493 ssl->nb_zero = 0;
1494 ssl->in_msgtype = 0;
1495 ssl->in_msglen = 0;
1496 ssl->in_hslen = 0;
1497 ssl->keep_current_message = 0;
1498 ssl->transform_in = NULL;
Deomid rojer Ryabkovac2cf1f2024-03-10 02:11:03 +00001499 ssl->in_hshdr = NULL;
1500 ssl->in_hsfraglen = 0;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001501
1502#if defined(MBEDTLS_SSL_PROTO_DTLS)
1503 ssl->next_record_offset = 0;
1504 ssl->in_epoch = 0;
1505#endif
1506
1507 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001509 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001511 }
1512
Ronald Cronad8c17b2022-06-10 17:18:09 +02001513 ssl->send_alert = 0;
1514
Hanno Beckerb0302c42021-08-03 09:39:42 +01001515 /* Reset outgoing message writing */
1516 ssl->out_msgtype = 0;
1517 ssl->out_msglen = 0;
1518 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 memset(ssl->out_buf, 0, out_buf_len);
1520 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001521 ssl->transform_out = NULL;
1522
1523#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001525#endif
1526
XiaokangQian2b01dc32022-01-21 02:53:13 +00001527#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (ssl->transform) {
1529 mbedtls_ssl_transform_free(ssl->transform);
1530 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001531 ssl->transform = NULL;
1532 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001533#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1534
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_ssl_transform_free(ssl->transform_application);
1537 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001538 ssl->transform_application = NULL;
1539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001541#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1543 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001544 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001545#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1548 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001549 ssl->handshake->transform_handshake = NULL;
1550 }
1551
XiaokangQian2b01dc32022-01-21 02:53:13 +00001552#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001553}
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001556{
1557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1558
1559 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001560 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001563
1564 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_SSL_RENEGOTIATION)
1566 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001567 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001568
1569 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1571 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001574
Hanno Beckerb0302c42021-08-03 09:39:42 +01001575 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001576 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (ssl->session) {
1578 mbedtls_ssl_session_free(ssl->session);
1579 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001580 ssl->session = NULL;
1581 }
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001584 ssl->alpn_chosen = NULL;
1585#endif
1586
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001587#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001588 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001589#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001591#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (free_cli_id) {
1593 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001594 ssl->cli_id = NULL;
1595 ssl->cli_id_len = 0;
1596 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001597#endif
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if ((ret = ssl_handshake_init(ssl)) != 0) {
1600 return ret;
1601 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001604}
1605
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001606/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001607 * Reset an initialized and used SSL context for re-use while retaining
1608 * all application-set variables, function pointers and data.
1609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001611{
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001613}
1614
1615/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 * SSL set accessors
1617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001619{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001620 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001621}
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001624{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001625 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001626}
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001629void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001631 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001632}
1633#endif
1634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001636{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001637 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001638}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1643 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001644{
1645 ssl->disable_datagram_packing = !allow_packing;
1646}
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1649 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001651 conf->hs_timeout_min = min;
1652 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001653}
1654#endif
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001657{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001658 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659}
1660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001662void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1663 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1664 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001666 conf->f_vrfy = f_vrfy;
1667 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1672 int (*f_rng)(void *, unsigned char *, size_t),
1673 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001675 conf->f_rng = f_rng;
1676 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677}
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1680 void (*f_dbg)(void *, int, const char *, int, const char *),
1681 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001683 conf->f_dbg = f_dbg;
1684 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001685}
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1688 void *p_bio,
1689 mbedtls_ssl_send_t *f_send,
1690 mbedtls_ssl_recv_t *f_recv,
1691 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001692{
1693 ssl->p_bio = p_bio;
1694 ssl->f_send = f_send;
1695 ssl->f_recv = f_recv;
1696 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001697}
1698
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001700void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001701{
1702 ssl->mtu = mtu;
1703}
1704#endif
1705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001707{
1708 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001709}
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1712 void *p_timer,
1713 mbedtls_ssl_set_timer_t *f_set_timer,
1714 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001715{
1716 ssl->p_timer = p_timer;
1717 ssl->f_set_timer = f_set_timer;
1718 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001719
1720 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001722}
1723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1726 void *p_cache,
1727 mbedtls_ssl_cache_get_t *f_get_cache,
1728 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001729{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001730 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001731 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001732 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001738{
Janos Follath865b3eb2019-12-16 11:46:15 +00001739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001742 session == NULL ||
1743 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001746 }
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 if (ssl->handshake->resume == 1) {
1749 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1750 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001751
Jerry Yu21092062022-10-10 21:21:31 +08001752#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron233fcaa2024-04-04 14:05:21 +02001754#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001755 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001759 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1761 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1762 session->ciphersuite));
1763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001764 }
Ronald Cron233fcaa2024-04-04 14:05:21 +02001765#else
1766 /*
1767 * If session tickets are not enabled, it is not possible to resume a
1768 * TLS 1.3 session, thus do not make any change to the SSL context in
1769 * the first place.
1770 */
1771 return 0;
1772#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001773 }
Jerry Yu21092062022-10-10 21:21:31 +08001774#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1777 session)) != 0) {
1778 return ret;
1779 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001780
Paul Bakker0a597072012-09-25 21:55:46 +00001781 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1788 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001789{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001790 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001791}
1792
Ronald Cron6f135e12021-12-08 16:57:54 +01001793#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1795 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001796{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001797 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001798}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001799
1800#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001801void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1802 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001803{
1804 conf->early_data_enabled = early_data_enabled;
1805}
Jerry Yucc4e0072022-11-22 17:22:22 +08001806
1807#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001808void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001810{
Jerry Yu39da9852022-12-06 16:58:36 +08001811 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001812}
1813#endif /* MBEDTLS_SSL_SRV_C */
1814
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001815#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001816#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001819void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1820 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001821{
1822 conf->cert_profile = profile;
1823}
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001826{
1827 mbedtls_ssl_key_cert *cur = key_cert, *next;
1828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001830 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001832 cur = next;
1833 }
1834}
1835
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001836/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001838static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1839 mbedtls_x509_crt *cert,
1840 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001841{
niisato8ee24222018-06-25 19:05:48 +09001842 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001845 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001847 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1852 if (new_cert == NULL) {
1853 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1854 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001855
niisato8ee24222018-06-25 19:05:48 +09001856 new_cert->cert = cert;
1857 new_cert->key = key;
1858 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001859
Glenn Strauss36872db2022-01-22 05:06:31 -05001860 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001862 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001864 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001866 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 }
niisato8ee24222018-06-25 19:05:48 +09001868 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001872}
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001875 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001877{
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001879}
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001882 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001884{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001885 conf->ca_chain = ca_chain;
1886 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001887
1888#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1889 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1890 * cannot be used together. */
1891 conf->f_ca_cb = NULL;
1892 conf->p_ca_cb = NULL;
1893#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001894}
Hanno Becker5adaad92019-03-27 16:54:37 +00001895
1896#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001897void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1898 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1899 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001900{
1901 conf->f_ca_cb = f_ca_cb;
1902 conf->p_ca_cb = p_ca_cb;
1903
1904 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1905 * cannot be used together. */
1906 conf->ca_chain = NULL;
1907 conf->ca_crl = NULL;
1908}
1909#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001911
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001912#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001913const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1914 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001915{
1916 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001918}
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1921 mbedtls_x509_crt *own_cert,
1922 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001923{
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1925 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001926}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1929 mbedtls_x509_crt *ca_chain,
1930 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001931{
1932 ssl->handshake->sni_ca_chain = ca_chain;
1933 ssl->handshake->sni_ca_crl = ca_crl;
1934}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001935
Glenn Strauss999ef702022-03-11 01:37:23 -05001936#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001937void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1938 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001939{
1940 ssl->handshake->dn_hints = crt;
1941}
1942#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1945 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001946{
1947 ssl->handshake->sni_authmode = authmode;
1948}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001949#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1950
Hanno Becker8927c832019-04-03 12:52:50 +01001951#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001952void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1953 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1954 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001955{
1956 ssl->f_vrfy = f_vrfy;
1957 ssl->p_vrfy = p_vrfy;
1958}
1959#endif
1960
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001961#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001962
Valerio Setti016f6822022-12-09 14:17:50 +01001963#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001964static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1965static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1966
Valerio Setti016f6822022-12-09 14:17:50 +01001967static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 mbedtls_ssl_context *ssl,
1969 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001970{
1971 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001972 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001973 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001974 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001975 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001976 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1978 psa_pake_cs_set_primitive(&cipher_suite,
1979 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1980 PSA_ECC_FAMILY_SECP_R1,
1981 256));
1982 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1985 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001986 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001990 user = jpake_server_id;
1991 user_len = sizeof(jpake_server_id);
1992 peer = jpake_client_id;
1993 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001995 user = jpake_client_id;
1996 user_len = sizeof(jpake_client_id);
1997 peer = jpake_server_id;
1998 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02002000
Przemek Stekiel4cd20312023-03-01 11:11:28 +01002001 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
2002 if (status != PSA_SUCCESS) {
2003 return status;
2004 }
2005
2006 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002008 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 }
Valerio Setti016f6822022-12-09 14:17:50 +01002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2012 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002013 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 }
Valerio Setti016f6822022-12-09 14:17:50 +01002015
2016 ssl->handshake->psa_pake_ctx_is_ok = 1;
2017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002019}
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2022 const unsigned char *pw,
2023 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002024{
2025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2026 psa_status_t status;
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (ssl->handshake == NULL || ssl->conf == NULL) {
2029 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002030 }
2031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 /* Empty password is not valid */
2033 if ((pw == NULL) || (pw_len == 0)) {
2034 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2035 }
2036
2037 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2038 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2039 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2040
2041 status = psa_import_key(&attributes, pw, pw_len,
2042 &ssl->handshake->psa_pake_password);
2043 if (status != PSA_SUCCESS) {
2044 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2045 }
2046
2047 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2048 ssl->handshake->psa_pake_password);
2049 if (status != PSA_SUCCESS) {
2050 psa_destroy_key(ssl->handshake->psa_pake_password);
2051 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2052 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2053 }
2054
2055 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002056}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2059 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002060{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002061 psa_status_t status;
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (ssl->handshake == NULL || ssl->conf == NULL) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (mbedtls_svc_key_id_is_null(pwd)) {
2068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2069 }
2070
2071 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2072 if (status != PSA_SUCCESS) {
2073 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2074 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2075 }
2076
2077 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002078}
2079#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2081 const unsigned char *pw,
2082 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002083{
2084 mbedtls_ecjpake_role role;
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (ssl->handshake == NULL || ssl->conf == NULL) {
2087 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2088 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002089
Valerio Settic689ed82022-12-07 14:40:38 +01002090 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if ((pw == NULL) || (pw_len == 0)) {
2092 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2093 }
Valerio Settic689ed82022-12-07 14:40:38 +01002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002096 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002098 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2102 role,
2103 MBEDTLS_MD_SHA256,
2104 MBEDTLS_ECP_DP_SECP256R1,
2105 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002106}
Valerio Setti02c25b52022-11-15 14:08:42 +01002107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002108#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002109
Ronald Cron73fe8df2022-10-05 14:31:43 +02002110#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002111int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002112{
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk_identity == NULL ||
2114 conf->psk_identity_len == 0) {
2115 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002116 }
2117
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2120 return 1;
2121 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002122#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (conf->psk != NULL && conf->psk_len != 0) {
2125 return 1;
2126 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002129}
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132{
2133 /* Remove reference to existing PSK, if any. */
2134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002136 /* The maintenance of the PSK key slot is the
2137 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002138 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002139 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002142 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002143 conf->psk = NULL;
2144 conf->psk_len = 0;
2145 }
2146
2147 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (conf->psk_identity != NULL) {
2149 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002150 conf->psk_identity = NULL;
2151 conf->psk_identity_len = 0;
2152 }
2153}
2154
Hanno Becker7390c712018-11-15 13:33:04 +00002155/* This function assumes that PSK identity in the SSL config is unset.
2156 * It checks that the provided identity is well-formed and attempts
2157 * to make a copy of it in the SSL config.
2158 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002159MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002160static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2161 unsigned char const *psk_identity,
2162 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002163{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002164 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002166 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 (psk_identity_len >> 16) != 0 ||
2168 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2169 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002170 }
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2173 if (conf->psk_identity == NULL) {
2174 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2175 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002176
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002177 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002181}
2182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2184 const unsigned char *psk, size_t psk_len,
2185 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002186{
Janos Follath865b3eb2019-12-16 11:46:15 +00002187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002188
2189 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2191 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2192 }
Hanno Becker7390c712018-11-15 13:33:04 +00002193
2194 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (psk == NULL) {
2196 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2197 }
2198 if (psk_len == 0) {
2199 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2200 }
2201 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2203 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2206 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2207 }
Hanno Becker7390c712018-11-15 13:33:04 +00002208 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002210
2211 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2213 if (ret != 0) {
2214 ssl_conf_remove_psk(conf);
2215 }
Hanno Becker7390c712018-11-15 13:33:04 +00002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002218}
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002221{
2222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002224 /* The maintenance of the external PSK key slot is the
2225 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (ssl->handshake->psk_opaque_is_internal) {
2227 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002228 ssl->handshake->psk_opaque_is_internal = 0;
2229 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002230 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002231 }
Neil Armstronge952a302022-05-03 10:22:14 +02002232#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002234 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002236 ssl->handshake->psk_len = 0;
lhuang0454adeab2024-06-10 12:16:29 -07002237 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002238 }
Neil Armstronge952a302022-05-03 10:22:14 +02002239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002240}
2241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2243 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002244{
Neil Armstrong501c9322022-05-03 09:35:09 +02002245#if defined(MBEDTLS_USE_PSA_CRYPTO)
2246 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002248 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002250#endif /* MBEDTLS_USE_PSA_CRYPTO */
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (psk == NULL || ssl->handshake == NULL) {
2253 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2254 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2257 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2258 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002261
Neil Armstrong501c9322022-05-03 09:35:09 +02002262#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2265 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2266 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2267 } else {
2268 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2269 }
2270 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002271 }
2272#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002273
Jerry Yu568ec252022-07-22 21:27:34 +08002274#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2276 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2277 psa_set_key_usage_flags(&key_attributes,
2278 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002279 }
2280#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 psa_set_key_algorithm(&key_attributes, alg);
2283 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2286 if (status != PSA_SUCCESS) {
2287 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2288 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002289
2290 /* Allow calling psa_destroy_key() on psk remove */
2291 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002293#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2295 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2296 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002297
2298 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002302#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002303}
2304
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002305#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002306int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2307 mbedtls_svc_key_id_t psk,
2308 const unsigned char *psk_identity,
2309 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002310{
Janos Follath865b3eb2019-12-16 11:46:15 +00002311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002312
2313 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2315 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2316 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002317
Hanno Becker7390c712018-11-15 13:33:04 +00002318 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (mbedtls_svc_key_id_is_null(psk)) {
2320 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2321 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002322 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002323
2324 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2326 psk_identity_len);
2327 if (ret != 0) {
2328 ssl_conf_remove_psk(conf);
2329 }
Hanno Becker7390c712018-11-15 13:33:04 +00002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002332}
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2335 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002336{
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if ((mbedtls_svc_key_id_is_null(psk)) ||
2338 (ssl->handshake == NULL)) {
2339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2340 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002343 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002345}
2346#endif /* MBEDTLS_USE_PSA_CRYPTO */
2347
Jerry Yu8897c072022-08-12 13:56:53 +08002348#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002349void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2350 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2351 size_t),
2352 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002353{
2354 conf->f_psk = f_psk;
2355 conf->p_psk = p_psk;
2356}
Jerry Yu8897c072022-08-12 13:56:53 +08002357#endif /* MBEDTLS_SSL_SRV_C */
2358
Ronald Cron73fe8df2022-10-05 14:31:43 +02002359#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002360
2361#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002362static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002364{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (alg == PSA_ALG_CBC_NO_PADDING) {
2367 return MBEDTLS_SSL_MODE_CBC;
2368 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002369#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 if (PSA_ALG_IS_AEAD(alg)) {
2371 return MBEDTLS_SSL_MODE_AEAD;
2372 }
2373 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002374}
2375
2376#else /* MBEDTLS_USE_PSA_CRYPTO */
2377
2378static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002380{
2381#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (mode == MBEDTLS_MODE_CBC) {
2383 return MBEDTLS_SSL_MODE_CBC;
2384 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002385#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2386
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002387#if defined(MBEDTLS_GCM_C) || \
2388 defined(MBEDTLS_CCM_C) || \
2389 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002391 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 mode == MBEDTLS_MODE_CHACHAPOLY) {
2393 return MBEDTLS_SSL_MODE_AEAD;
2394 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002395#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002398}
Gilles Peskine301711e2022-04-26 16:57:05 +02002399#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002400
Gilles Peskinee108d982022-04-26 16:50:40 +02002401static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2402 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002404{
2405#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2407 base_mode == MBEDTLS_SSL_MODE_CBC) {
2408 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002409 }
2410#else
2411 (void) encrypt_then_mac;
2412#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002414}
2415
Neil Armstrongab555e02022-04-04 11:07:59 +02002416mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002418{
Gilles Peskinee108d982022-04-26 16:50:40 +02002419 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002420#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002422#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002424#endif
2425 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002426
Gilles Peskinee108d982022-04-26 16:50:40 +02002427 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002428#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002429 encrypt_then_mac = transform->encrypt_then_mac;
2430#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002432}
2433
Neil Armstrongab555e02022-04-04 11:07:59 +02002434mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002435#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002437#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002439{
Gilles Peskinee108d982022-04-26 16:50:40 +02002440 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2441
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002442#if defined(MBEDTLS_USE_PSA_CRYPTO)
2443 psa_status_t status;
2444 psa_algorithm_t alg;
2445 psa_key_type_t type;
2446 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002447 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2448 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (status == PSA_SUCCESS) {
2450 base_mode = mbedtls_ssl_get_base_mode(alg);
2451 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002452#else
2453 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002454 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002456 base_mode =
2457 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002459 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002460#endif /* MBEDTLS_USE_PSA_CRYPTO */
2461
Gilles Peskinee108d982022-04-26 16:50:40 +02002462#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2463 int encrypt_then_mac = 0;
2464#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002466}
2467
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002468#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002469
Ronald Cron8b592d22024-11-12 18:08:25 +01002470const mbedtls_error_pair_t psa_to_ssl_errors[] =
2471{
2472 { PSA_SUCCESS, 0 },
2473 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
2474 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
2475 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
2476 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
2477 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
2478 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
2479};
2480
Gilles Peskine449bd832023-01-11 14:50:10 +01002481psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2482 size_t taglen,
2483 psa_algorithm_t *alg,
2484 psa_key_type_t *key_type,
2485 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002486{
Elena Uziunaitec2561722024-07-05 11:37:33 +01002487#if !defined(PSA_WANT_ALG_CCM)
Yanray Wang19e4dc82023-11-16 18:02:05 +08002488 (void) taglen;
2489#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 switch (mbedtls_cipher_type) {
Elena Uziunaite74342c72024-07-05 11:31:29 +01002491#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002492 case MBEDTLS_CIPHER_AES_128_CBC:
2493 *alg = PSA_ALG_CBC_NO_PADDING;
2494 *key_type = PSA_KEY_TYPE_AES;
2495 *key_size = 128;
2496 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002497#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002498#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002499 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002501 *key_type = PSA_KEY_TYPE_AES;
2502 *key_size = 128;
2503 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002504#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002505#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002506 case MBEDTLS_CIPHER_AES_128_GCM:
2507 *alg = PSA_ALG_GCM;
2508 *key_type = PSA_KEY_TYPE_AES;
2509 *key_size = 128;
2510 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002511#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002512#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002513 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002515 *key_type = PSA_KEY_TYPE_AES;
2516 *key_size = 192;
2517 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002518#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002519#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002520 case MBEDTLS_CIPHER_AES_192_GCM:
2521 *alg = PSA_ALG_GCM;
2522 *key_type = PSA_KEY_TYPE_AES;
2523 *key_size = 192;
2524 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002525#endif
Elena Uziunaite74342c72024-07-05 11:31:29 +01002526#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002527 case MBEDTLS_CIPHER_AES_256_CBC:
2528 *alg = PSA_ALG_CBC_NO_PADDING;
2529 *key_type = PSA_KEY_TYPE_AES;
2530 *key_size = 256;
2531 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002532#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002533#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002534 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002536 *key_type = PSA_KEY_TYPE_AES;
2537 *key_size = 256;
2538 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002539#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002540#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002541 case MBEDTLS_CIPHER_AES_256_GCM:
2542 *alg = PSA_ALG_GCM;
2543 *key_type = PSA_KEY_TYPE_AES;
2544 *key_size = 256;
2545 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002546#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002547#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002548 case MBEDTLS_CIPHER_ARIA_128_CBC:
2549 *alg = PSA_ALG_CBC_NO_PADDING;
2550 *key_type = PSA_KEY_TYPE_ARIA;
2551 *key_size = 128;
2552 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002553#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002554#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002555 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002557 *key_type = PSA_KEY_TYPE_ARIA;
2558 *key_size = 128;
2559 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002560#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002561#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002562 case MBEDTLS_CIPHER_ARIA_128_GCM:
2563 *alg = PSA_ALG_GCM;
2564 *key_type = PSA_KEY_TYPE_ARIA;
2565 *key_size = 128;
2566 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002567#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002568#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002569 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002571 *key_type = PSA_KEY_TYPE_ARIA;
2572 *key_size = 192;
2573 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002574#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002575#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002576 case MBEDTLS_CIPHER_ARIA_192_GCM:
2577 *alg = PSA_ALG_GCM;
2578 *key_type = PSA_KEY_TYPE_ARIA;
2579 *key_size = 192;
2580 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002581#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002582#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002583 case MBEDTLS_CIPHER_ARIA_256_CBC:
2584 *alg = PSA_ALG_CBC_NO_PADDING;
2585 *key_type = PSA_KEY_TYPE_ARIA;
2586 *key_size = 256;
2587 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002588#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002589#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002590 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002592 *key_type = PSA_KEY_TYPE_ARIA;
2593 *key_size = 256;
2594 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002595#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002596#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002597 case MBEDTLS_CIPHER_ARIA_256_GCM:
2598 *alg = PSA_ALG_GCM;
2599 *key_type = PSA_KEY_TYPE_ARIA;
2600 *key_size = 256;
2601 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002602#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002603#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002604 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2605 *alg = PSA_ALG_CBC_NO_PADDING;
2606 *key_type = PSA_KEY_TYPE_CAMELLIA;
2607 *key_size = 128;
2608 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002609#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002610#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002611 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002613 *key_type = PSA_KEY_TYPE_CAMELLIA;
2614 *key_size = 128;
2615 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002616#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002617#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002618 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2619 *alg = PSA_ALG_GCM;
2620 *key_type = PSA_KEY_TYPE_CAMELLIA;
2621 *key_size = 128;
2622 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002623#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002624#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002625 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002627 *key_type = PSA_KEY_TYPE_CAMELLIA;
2628 *key_size = 192;
2629 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002630#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002631#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002632 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2633 *alg = PSA_ALG_GCM;
2634 *key_type = PSA_KEY_TYPE_CAMELLIA;
2635 *key_size = 192;
2636 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002637#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002638#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002639 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2640 *alg = PSA_ALG_CBC_NO_PADDING;
2641 *key_type = PSA_KEY_TYPE_CAMELLIA;
2642 *key_size = 256;
2643 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002644#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002645#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002646 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002648 *key_type = PSA_KEY_TYPE_CAMELLIA;
2649 *key_size = 256;
2650 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002651#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002652#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002653 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2654 *alg = PSA_ALG_GCM;
2655 *key_type = PSA_KEY_TYPE_CAMELLIA;
2656 *key_size = 256;
2657 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002658#endif
Elena Uziunaite5c70c302024-07-05 11:44:44 +01002659#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002660 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2661 *alg = PSA_ALG_CHACHA20_POLY1305;
2662 *key_type = PSA_KEY_TYPE_CHACHA20;
2663 *key_size = 256;
2664 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002665#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002666 case MBEDTLS_CIPHER_NULL:
2667 *alg = MBEDTLS_SSL_NULL_CIPHER;
2668 *key_type = 0;
2669 *key_size = 0;
2670 break;
2671 default:
2672 return PSA_ERROR_NOT_SUPPORTED;
2673 }
2674
2675 return PSA_SUCCESS;
2676}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002677#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002678
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002679#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002680int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2681 const unsigned char *dhm_P, size_t P_len,
2682 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002683{
Janos Follath865b3eb2019-12-16 11:46:15 +00002684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 mbedtls_mpi_free(&conf->dhm_P);
2687 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2690 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2691 mbedtls_mpi_free(&conf->dhm_P);
2692 mbedtls_mpi_free(&conf->dhm_G);
2693 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002694 }
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002697}
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002700{
Janos Follath865b3eb2019-12-16 11:46:15 +00002701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 mbedtls_mpi_free(&conf->dhm_P);
2704 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2707 &conf->dhm_P)) != 0 ||
2708 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2709 &conf->dhm_G)) != 0) {
2710 mbedtls_mpi_free(&conf->dhm_P);
2711 mbedtls_mpi_free(&conf->dhm_G);
2712 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002713 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002716}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002717#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002718
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002719#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2720/*
2721 * Set the minimum length for Diffie-Hellman parameters
2722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002723void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2724 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002725{
2726 conf->dhm_min_bitlen = bitlen;
2727}
2728#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2729
Ronald Crone68ab4f2022-10-05 12:46:29 +02002730#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002731#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002732/*
2733 * Set allowed/preferred hashes for handshake signatures
2734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002735void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2736 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002737{
2738 conf->sig_hashes = hashes;
2739}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002740#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002741
Jerry Yuf017ee42022-01-12 15:49:48 +08002742/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002743void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2744 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002745{
Jerry Yuf017ee42022-01-12 15:49:48 +08002746#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2747 conf->sig_hashes = NULL;
2748#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2749 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002750}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002751#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002752
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002753#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002754#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002755/*
2756 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002757 *
2758 * mbedtls_ssl_setup() takes the provided list
2759 * and translates it to a list of IANA TLS group identifiers,
2760 * stored in ssl->handshake->group_list.
2761 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2764 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002765{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002766 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002767 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002768}
Brett Warrene0edc842021-08-17 09:53:13 +01002769#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002770#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002771
Brett Warrene0edc842021-08-17 09:53:13 +01002772/*
2773 * Set the allowed groups
2774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002775void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2776 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002777{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002778#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01002779 conf->curve_list = NULL;
2780#endif
2781 conf->group_list = group_list;
2782}
2783
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002785int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002786{
Hanno Becker947194e2017-04-07 13:25:49 +01002787 /* Initialize to suppress unnecessary compiler warning */
2788 size_t hostname_len = 0;
2789
2790 /* Check if new hostname is valid before
2791 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if (hostname != NULL) {
2793 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2796 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2797 }
Hanno Becker947194e2017-04-07 13:25:49 +01002798 }
2799
2800 /* Now it's clear that we will overwrite the old hostname,
2801 * so we can free it safely */
2802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002804 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002805 }
2806
2807 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002810 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 } else {
2812 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2813 if (ssl->hostname == NULL) {
2814 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2815 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002818
Hanno Becker947194e2017-04-07 13:25:49 +01002819 ssl->hostname[hostname_len] = '\0';
2820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002823}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002824#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002826#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002827void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2828 int (*f_sni)(void *, mbedtls_ssl_context *,
2829 const unsigned char *, size_t),
2830 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002831{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002832 conf->f_sni = f_sni;
2833 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002838int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002839{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002840 size_t cur_len, tot_len;
2841 const char **p;
2842
2843 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002844 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2845 * MUST NOT be truncated."
2846 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002847 */
2848 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 for (p = protos; *p != NULL; p++) {
2850 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002851 tot_len += cur_len;
2852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if ((cur_len == 0) ||
2854 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2855 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2856 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2857 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002858 }
2859
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002860 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002863}
2864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002866{
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002870
Johan Pascalb62bb512015-12-03 21:56:45 +01002871#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002872void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2873 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002874{
2875 conf->dtls_srtp_mki_support = support_mki_value;
2876}
2877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2879 unsigned char *mki_value,
2880 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002881{
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2883 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002884 }
Ron Eldor591f1622018-01-22 12:30:04 +02002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2887 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002888 }
Ron Eldor591f1622018-01-22 12:30:04 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002891 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002893}
2894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2896 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002897{
Johan Pascal253d0262020-09-22 13:04:45 +02002898 const mbedtls_ssl_srtp_profile *p;
2899 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002900
Johan Pascal253d0262020-09-22 13:04:45 +02002901 /* check the profiles list: all entry must be valid,
2902 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2904 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2905 p++) {
2906 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002907 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002909 /* unsupported value, stop parsing and set the size to an error value */
2910 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002911 }
2912 }
2913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2915 conf->dtls_srtp_profile_list = NULL;
2916 conf->dtls_srtp_profile_list_len = 0;
2917 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002918 }
2919
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002920 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002921 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002922
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002924}
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2927 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002928{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002929 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2930 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002932 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002934 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2936 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002937 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002938}
Johan Pascalb62bb512015-12-03 21:56:45 +01002939#endif /* MBEDTLS_SSL_DTLS_SRTP */
2940
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302941#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002942void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002943{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002944 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002945}
2946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002948{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002949 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002950}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302951#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002952
Janos Follath088ce432017-04-10 12:42:31 +01002953#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002954void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2955 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002956{
2957 conf->cert_req_ca_list = cert_req_ca_list;
2958}
2959#endif
2960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002962void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002964 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002965}
2966#endif
2967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002969void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002970{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002971 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002972}
2973#endif
2974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002976int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002977{
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2979 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2980 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002981 }
2982
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002983 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002986}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002990{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002991 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002992}
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002995void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002996{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002997 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002998}
2999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003002 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003003}
3004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3006 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003007{
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003013#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003014void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003015{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003016 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003017}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003018#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003019
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003020#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003021
Jerry Yud0766ec2022-09-22 10:46:57 +08003022#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3024 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003025{
Jerry Yud0766ec2022-09-22 10:46:57 +08003026 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003027}
3028#endif
3029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3031 mbedtls_ssl_ticket_write_t *f_ticket_write,
3032 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3033 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003034{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003035 conf->f_ticket_write = f_ticket_write;
3036 conf->f_ticket_parse = f_ticket_parse;
3037 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003038}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3043 mbedtls_ssl_export_keys_t *f_export_keys,
3044 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003045{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003046 ssl->f_export_keys = f_export_keys;
3047 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003048}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003049
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003050#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003051void mbedtls_ssl_conf_async_private_cb(
3052 mbedtls_ssl_config *conf,
3053 mbedtls_ssl_async_sign_t *f_async_sign,
3054 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3055 mbedtls_ssl_async_resume_t *f_async_resume,
3056 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003058{
3059 conf->f_async_sign_start = f_async_sign;
3060 conf->f_async_decrypt_start = f_async_decrypt;
3061 conf->f_async_resume = f_async_resume;
3062 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003063 conf->p_async_config_data = async_config_data;
3064}
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003067{
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003069}
3070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003072{
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (ssl->handshake == NULL) {
3074 return NULL;
3075 } else {
3076 return ssl->handshake->user_async_ctx;
3077 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3081 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003082{
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003084 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003086}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003087#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089/*
3090 * SSL get accessors
3091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003093{
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (ssl->session != NULL) {
3095 return ssl->session->verify_result;
3096 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ssl->session_negotiate != NULL) {
3099 return ssl->session_negotiate->verify_result;
3100 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103}
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003106{
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (ssl == NULL || ssl->session == NULL) {
3108 return 0;
3109 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003112}
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003115{
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ssl == NULL || ssl->session == NULL) {
3117 return NULL;
3118 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003121}
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3127 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003128 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003130 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003132 }
3133 }
3134#endif
3135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003137 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003139 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003143 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003144}
3145
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003146#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3147
3148size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3149{
3150 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3151 size_t record_size_limit = max_len;
3152
3153 if (ssl->session != NULL &&
3154 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3155 ssl->session->record_size_limit < max_len) {
3156 record_size_limit = ssl->session->record_size_limit;
3157 }
3158
3159 // TODO: this is currently untested
3160 /* During a handshake, use the value being negotiated */
3161 if (ssl->session_negotiate != NULL &&
3162 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3163 ssl->session_negotiate->record_size_limit < max_len) {
3164 record_size_limit = ssl->session_negotiate->record_size_limit;
3165 }
3166
3167 return record_size_limit;
3168}
3169#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3170
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003171#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003172size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003173{
David Horstmann95d516f2021-05-04 18:36:56 +01003174 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003175 size_t read_mfl;
3176
Jerry Yuddda0502022-12-01 19:43:12 +08003177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003178 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3180 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3181 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003182 }
Jerry Yuddda0502022-12-01 19:43:12 +08003183#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003184
3185 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if (ssl->session_out != NULL) {
3187 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3188 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003189 max_len = read_mfl;
3190 }
3191 }
3192
Jerry Yuddda0502022-12-01 19:43:12 +08003193 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 if (ssl->session_negotiate != NULL) {
3195 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3196 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003197 max_len = read_mfl;
3198 }
3199 }
3200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003205{
3206 size_t max_len;
3207
3208 /*
3209 * Assume mfl_code is correct since it was checked when set
3210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003212
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003213 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (ssl->session_out != NULL &&
3215 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3216 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003217 }
3218
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003219 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 if (ssl->session_negotiate != NULL &&
3221 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3222 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003223 }
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003226}
3227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3228
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003230size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003231{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003232 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3234 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3235 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3236 return 0;
3237 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3240 return ssl->mtu;
3241 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (ssl->mtu == 0) {
3244 return ssl->handshake->mtu;
3245 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return ssl->mtu < ssl->handshake->mtu ?
3248 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003249}
3250#endif /* MBEDTLS_SSL_PROTO_DTLS */
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003253{
3254 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3255
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003256#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003257 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003258 !defined(MBEDTLS_SSL_PROTO_DTLS)
3259 (void) ssl;
3260#endif
3261
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003262#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003266 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003268#endif
3269
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003270#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3271 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3272
3273 if (max_len > record_size_limit) {
3274 max_len = record_size_limit;
3275 }
3276#endif
3277
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003278 if (ssl->transform_out != NULL &&
3279 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003280 /*
3281 * In TLS 1.3 case, when records are protected, `max_len` as computed
3282 * above is the maximum length of the TLSInnerPlaintext structure that
3283 * along the plaintext payload contains the inner content type (one byte)
3284 * and some zero padding. Given the algorithm used for padding
3285 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3286 * the plaintext payload. Round down to a multiple of
3287 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3288 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003289 */
3290 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3291 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3292 }
3293
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003294#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3296 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3297 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003298 const size_t overhead = (size_t) ret;
3299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 if (ret < 0) {
3301 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003302 }
3303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 if (mtu <= overhead) {
3305 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3307 }
3308
3309 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003310 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003312 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003313#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003314
Hanno Becker0defedb2018-08-10 12:35:02 +01003315#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003316 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3317 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003318 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319#endif
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322}
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003325{
3326 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3327
3328#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3329 (void) ssl;
3330#endif
3331
3332#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003336 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003338#endif
3339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003341}
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003344const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003345{
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl == NULL || ssl->session == NULL) {
3347 return NULL;
3348 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003349
Hanno Beckere6824572019-02-07 13:18:46 +00003350#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003352#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003354#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003359int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3360 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003361{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003362 int ret;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003365 dst == NULL ||
3366 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003369 }
3370
Hanno Beckere810bbc2021-05-14 16:01:05 +01003371 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3372 * idempotent: Each session can only be exported once.
3373 *
3374 * (This is in preparation for TLS 1.3 support where we will
3375 * need the ability to export multiple sessions (aka tickets),
3376 * which will be achieved by calling mbedtls_ssl_get_session()
3377 * multiple times until it fails.)
3378 *
3379 * Check whether we have already exported the current session,
3380 * and fail if so.
3381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (ssl->session->exported == 1) {
3383 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3384 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3387 if (ret != 0) {
3388 return ret;
3389 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003390
3391 /* Remember that we've exported the session. */
3392 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003396
David Horstmanncb01b362024-02-29 17:31:46 +00003397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3398
3399/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003400 *
David Horstmanncb01b362024-02-29 17:31:46 +00003401 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003402 */
3403static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3404 unsigned char *buf,
3405 size_t buf_len)
3406{
3407 unsigned char *p = buf;
3408 size_t used = 0;
3409
3410#if defined(MBEDTLS_HAVE_TIME)
3411 uint64_t start;
3412#endif
3413#if defined(MBEDTLS_X509_CRT_PARSE_C)
3414#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3415 size_t cert_len;
3416#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3417#endif /* MBEDTLS_X509_CRT_PARSE_C */
3418
3419 /*
3420 * Time
3421 */
3422#if defined(MBEDTLS_HAVE_TIME)
3423 used += 8;
3424
3425 if (used <= buf_len) {
3426 start = (uint64_t) session->start;
3427
3428 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3429 p += 8;
3430 }
3431#endif /* MBEDTLS_HAVE_TIME */
3432
3433 /*
3434 * Basic mandatory fields
3435 */
3436 used += 1 /* id_len */
3437 + sizeof(session->id)
3438 + sizeof(session->master)
3439 + 4; /* verify_result */
3440
3441 if (used <= buf_len) {
3442 *p++ = MBEDTLS_BYTE_0(session->id_len);
3443 memcpy(p, session->id, 32);
3444 p += 32;
3445
3446 memcpy(p, session->master, 48);
3447 p += 48;
3448
3449 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3450 p += 4;
3451 }
3452
3453 /*
3454 * Peer's end-entity certificate
3455 */
3456#if defined(MBEDTLS_X509_CRT_PARSE_C)
3457#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3458 if (session->peer_cert == NULL) {
3459 cert_len = 0;
3460 } else {
3461 cert_len = session->peer_cert->raw.len;
3462 }
3463
3464 used += 3 + cert_len;
3465
3466 if (used <= buf_len) {
3467 *p++ = MBEDTLS_BYTE_2(cert_len);
3468 *p++ = MBEDTLS_BYTE_1(cert_len);
3469 *p++ = MBEDTLS_BYTE_0(cert_len);
3470
3471 if (session->peer_cert != NULL) {
3472 memcpy(p, session->peer_cert->raw.p, cert_len);
3473 p += cert_len;
3474 }
3475 }
3476#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3477 if (session->peer_cert_digest != NULL) {
3478 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3479 if (used <= buf_len) {
3480 *p++ = (unsigned char) session->peer_cert_digest_type;
3481 *p++ = (unsigned char) session->peer_cert_digest_len;
3482 memcpy(p, session->peer_cert_digest,
3483 session->peer_cert_digest_len);
3484 p += session->peer_cert_digest_len;
3485 }
3486 } else {
3487 used += 2;
3488 if (used <= buf_len) {
3489 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3490 *p++ = 0;
3491 }
3492 }
3493#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3494#endif /* MBEDTLS_X509_CRT_PARSE_C */
3495
3496 /*
3497 * Session ticket if any, plus associated data
3498 */
3499#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3500#if defined(MBEDTLS_SSL_CLI_C)
3501 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3502 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3503
3504 if (used <= buf_len) {
3505 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3506 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3507 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3508
3509 if (session->ticket != NULL) {
3510 memcpy(p, session->ticket, session->ticket_len);
3511 p += session->ticket_len;
3512 }
3513
3514 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3515 p += 4;
3516 }
3517 }
3518#endif /* MBEDTLS_SSL_CLI_C */
3519#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3520 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3521 used += 8;
3522
3523 if (used <= buf_len) {
3524 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3525 p += 8;
3526 }
3527 }
3528#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3529#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3530
3531 /*
3532 * Misc extension-related info
3533 */
3534#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3535 used += 1;
3536
3537 if (used <= buf_len) {
3538 *p++ = session->mfl_code;
3539 }
3540#endif
3541
3542#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3543 used += 1;
3544
3545 if (used <= buf_len) {
3546 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3547 }
3548#endif
3549
3550 return used;
3551}
3552
3553MBEDTLS_CHECK_RETURN_CRITICAL
3554static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3555 const unsigned char *buf,
3556 size_t len)
3557{
3558#if defined(MBEDTLS_HAVE_TIME)
3559 uint64_t start;
3560#endif
3561#if defined(MBEDTLS_X509_CRT_PARSE_C)
3562#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3563 size_t cert_len;
3564#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3565#endif /* MBEDTLS_X509_CRT_PARSE_C */
3566
3567 const unsigned char *p = buf;
3568 const unsigned char * const end = buf + len;
3569
3570 /*
3571 * Time
3572 */
3573#if defined(MBEDTLS_HAVE_TIME)
3574 if (8 > (size_t) (end - p)) {
3575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3576 }
3577
3578 start = MBEDTLS_GET_UINT64_BE(p, 0);
3579 p += 8;
3580
3581 session->start = (time_t) start;
3582#endif /* MBEDTLS_HAVE_TIME */
3583
3584 /*
3585 * Basic mandatory fields
3586 */
3587 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3589 }
3590
3591 session->id_len = *p++;
3592 memcpy(session->id, p, 32);
3593 p += 32;
3594
3595 memcpy(session->master, p, 48);
3596 p += 48;
3597
3598 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3599 p += 4;
3600
3601 /* Immediately clear invalid pointer values that have been read, in case
3602 * we exit early before we replaced them with valid ones. */
3603#if defined(MBEDTLS_X509_CRT_PARSE_C)
3604#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3605 session->peer_cert = NULL;
3606#else
3607 session->peer_cert_digest = NULL;
3608#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3609#endif /* MBEDTLS_X509_CRT_PARSE_C */
3610#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3611 session->ticket = NULL;
3612#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3613
3614 /*
3615 * Peer certificate
3616 */
3617#if defined(MBEDTLS_X509_CRT_PARSE_C)
3618#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3619 /* Deserialize CRT from the end of the ticket. */
3620 if (3 > (size_t) (end - p)) {
3621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3622 }
3623
3624 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3625 p += 3;
3626
3627 if (cert_len != 0) {
3628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3629
3630 if (cert_len > (size_t) (end - p)) {
3631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3632 }
3633
3634 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3635
3636 if (session->peer_cert == NULL) {
3637 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3638 }
3639
3640 mbedtls_x509_crt_init(session->peer_cert);
3641
3642 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3643 p, cert_len)) != 0) {
3644 mbedtls_x509_crt_free(session->peer_cert);
3645 mbedtls_free(session->peer_cert);
3646 session->peer_cert = NULL;
3647 return ret;
3648 }
3649
3650 p += cert_len;
3651 }
3652#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3653 /* Deserialize CRT digest from the end of the ticket. */
3654 if (2 > (size_t) (end - p)) {
3655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3656 }
3657
3658 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3659 session->peer_cert_digest_len = (size_t) *p++;
3660
3661 if (session->peer_cert_digest_len != 0) {
3662 const mbedtls_md_info_t *md_info =
3663 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3664 if (md_info == NULL) {
3665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3666 }
3667 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3669 }
3670
3671 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3673 }
3674
3675 session->peer_cert_digest =
3676 mbedtls_calloc(1, session->peer_cert_digest_len);
3677 if (session->peer_cert_digest == NULL) {
3678 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3679 }
3680
3681 memcpy(session->peer_cert_digest, p,
3682 session->peer_cert_digest_len);
3683 p += session->peer_cert_digest_len;
3684 }
3685#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3686#endif /* MBEDTLS_X509_CRT_PARSE_C */
3687
3688 /*
3689 * Session ticket and associated data
3690 */
3691#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3692#if defined(MBEDTLS_SSL_CLI_C)
3693 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3694 if (3 > (size_t) (end - p)) {
3695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3696 }
3697
3698 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3699 p += 3;
3700
3701 if (session->ticket_len != 0) {
3702 if (session->ticket_len > (size_t) (end - p)) {
3703 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3704 }
3705
3706 session->ticket = mbedtls_calloc(1, session->ticket_len);
3707 if (session->ticket == NULL) {
3708 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3709 }
3710
3711 memcpy(session->ticket, p, session->ticket_len);
3712 p += session->ticket_len;
3713 }
3714
3715 if (4 > (size_t) (end - p)) {
3716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3717 }
3718
3719 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3720 p += 4;
3721 }
3722#endif /* MBEDTLS_SSL_CLI_C */
3723#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3724 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3725 if (8 > (size_t) (end - p)) {
3726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3727 }
3728 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3729 p += 8;
3730 }
3731#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3732#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3733
3734 /*
3735 * Misc extension-related info
3736 */
3737#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3738 if (1 > (size_t) (end - p)) {
3739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3740 }
3741
3742 session->mfl_code = *p++;
3743#endif
3744
3745#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3746 if (1 > (size_t) (end - p)) {
3747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3748 }
3749
3750 session->encrypt_then_mac = *p++;
3751#endif
3752
3753 /* Done, should have consumed entire buffer */
3754 if (p != end) {
3755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3756 }
3757
3758 return 0;
3759}
3760
3761#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3762
3763#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3764/* Serialization of TLS 1.3 sessions:
3765 *
David Horstmanncb01b362024-02-29 17:31:46 +00003766 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003767 */
3768#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3769MBEDTLS_CHECK_RETURN_CRITICAL
3770static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3771 unsigned char *buf,
3772 size_t buf_len,
3773 size_t *olen)
3774{
3775 unsigned char *p = buf;
3776#if defined(MBEDTLS_SSL_CLI_C) && \
3777 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3778 size_t hostname_len = (session->hostname == NULL) ?
3779 0 : strlen(session->hostname) + 1;
3780#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003781
3782#if defined(MBEDTLS_SSL_SRV_C) && \
3783 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003784 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003785 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003786#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003787 size_t needed = 4 /* ticket_age_add */
3788 + 1 /* ticket_flags */
3789 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003790
David Horstmanne59f9702024-02-29 16:08:57 +00003791 *olen = 0;
3792
3793 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3794 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3795 }
3796 needed += session->resumption_key_len; /* resumption_key */
3797
3798#if defined(MBEDTLS_SSL_EARLY_DATA)
3799 needed += 4; /* max_early_data_size */
3800#endif
3801#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3802 needed += 2; /* record_size_limit */
3803#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3804
3805#if defined(MBEDTLS_HAVE_TIME)
3806 needed += 8; /* ticket_creation_time or ticket_reception_time */
3807#endif
3808
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003809#if defined(MBEDTLS_SSL_SRV_C)
3810 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3811#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003812 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003813 + alpn_len; /* alpn */
3814#endif
3815 }
3816#endif /* MBEDTLS_SSL_SRV_C */
3817
David Horstmanne59f9702024-02-29 16:08:57 +00003818#if defined(MBEDTLS_SSL_CLI_C)
3819 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3821 needed += 2 /* hostname_len */
3822 + hostname_len; /* hostname */
3823#endif
3824
3825 needed += 4 /* ticket_lifetime */
3826 + 2; /* ticket_len */
3827
3828 /* Check size_t overflow */
3829 if (session->ticket_len > SIZE_MAX - needed) {
3830 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3831 }
3832
3833 needed += session->ticket_len; /* ticket */
3834 }
3835#endif /* MBEDTLS_SSL_CLI_C */
3836
3837 *olen = needed;
3838 if (needed > buf_len) {
3839 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3840 }
3841
3842 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3843 p[4] = session->ticket_flags;
3844
3845 /* save resumption_key */
3846 p[5] = session->resumption_key_len;
3847 p += 6;
3848 memcpy(p, session->resumption_key, session->resumption_key_len);
3849 p += session->resumption_key_len;
3850
3851#if defined(MBEDTLS_SSL_EARLY_DATA)
3852 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3853 p += 4;
3854#endif
3855#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3856 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3857 p += 2;
3858#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3859
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003860#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003861 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003862#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003863 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3864 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003865#endif /* MBEDTLS_HAVE_TIME */
3866
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003867#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003868 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3869 p += 2;
3870
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003871 if (alpn_len > 0) {
3872 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003873 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003874 p += alpn_len;
3875 }
3876#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3877 }
3878#endif /* MBEDTLS_SSL_SRV_C */
3879
David Horstmanne59f9702024-02-29 16:08:57 +00003880#if defined(MBEDTLS_SSL_CLI_C)
3881 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3882#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3883 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3884 p += 2;
3885 if (hostname_len > 0) {
3886 /* save host name */
3887 memcpy(p, session->hostname, hostname_len);
3888 p += hostname_len;
3889 }
3890#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3891
3892#if defined(MBEDTLS_HAVE_TIME)
3893 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3894 p += 8;
3895#endif
3896 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3897 p += 4;
3898
3899 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3900 p += 2;
3901
3902 if (session->ticket != NULL && session->ticket_len > 0) {
3903 memcpy(p, session->ticket, session->ticket_len);
3904 p += session->ticket_len;
3905 }
3906 }
3907#endif /* MBEDTLS_SSL_CLI_C */
3908 return 0;
3909}
3910
3911MBEDTLS_CHECK_RETURN_CRITICAL
3912static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3913 const unsigned char *buf,
3914 size_t len)
3915{
3916 const unsigned char *p = buf;
3917 const unsigned char *end = buf + len;
3918
3919 if (end - p < 6) {
3920 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3921 }
3922 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3923 session->ticket_flags = p[4];
3924
3925 /* load resumption_key */
3926 session->resumption_key_len = p[5];
3927 p += 6;
3928
3929 if (end - p < session->resumption_key_len) {
3930 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3931 }
3932
3933 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3934 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3935 }
3936 memcpy(session->resumption_key, p, session->resumption_key_len);
3937 p += session->resumption_key_len;
3938
3939#if defined(MBEDTLS_SSL_EARLY_DATA)
3940 if (end - p < 4) {
3941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3942 }
3943 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3944 p += 4;
3945#endif
3946#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3947 if (end - p < 2) {
3948 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3949 }
3950 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3951 p += 2;
3952#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3953
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003954#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003955 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003956#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003957 if (end - p < 8) {
3958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3959 }
3960 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3961 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003962#endif /* MBEDTLS_HAVE_TIME */
3963
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003964#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003965 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003966
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003967 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3969 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003970
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003971 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3972 p += 2;
3973
3974 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003975 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3976 }
3977
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003978 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003979 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3980 if (ret != 0) {
3981 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003982 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003983 p += alpn_len;
3984 }
3985#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3986 }
3987#endif /* MBEDTLS_SSL_SRV_C */
3988
David Horstmanne59f9702024-02-29 16:08:57 +00003989#if defined(MBEDTLS_SSL_CLI_C)
3990 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3991#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3992 size_t hostname_len;
3993 /* load host name */
3994 if (end - p < 2) {
3995 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3996 }
3997 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3998 p += 2;
3999
4000 if (end - p < (long int) hostname_len) {
4001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4002 }
4003 if (hostname_len > 0) {
4004 session->hostname = mbedtls_calloc(1, hostname_len);
4005 if (session->hostname == NULL) {
4006 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4007 }
4008 memcpy(session->hostname, p, hostname_len);
4009 p += hostname_len;
4010 }
4011#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4012
4013#if defined(MBEDTLS_HAVE_TIME)
4014 if (end - p < 8) {
4015 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4016 }
4017 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
4018 p += 8;
4019#endif
4020 if (end - p < 4) {
4021 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4022 }
4023 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
4024 p += 4;
4025
4026 if (end - p < 2) {
4027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4028 }
4029 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
4030 p += 2;
4031
4032 if (end - p < (long int) session->ticket_len) {
4033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4034 }
4035 if (session->ticket_len > 0) {
4036 session->ticket = mbedtls_calloc(1, session->ticket_len);
4037 if (session->ticket == NULL) {
4038 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4039 }
4040 memcpy(session->ticket, p, session->ticket_len);
4041 p += session->ticket_len;
4042 }
4043 }
4044#endif /* MBEDTLS_SSL_CLI_C */
4045
4046 return 0;
4047
4048}
4049#else /* MBEDTLS_SSL_SESSION_TICKETS */
4050MBEDTLS_CHECK_RETURN_CRITICAL
4051static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
4052 unsigned char *buf,
4053 size_t buf_len,
4054 size_t *olen)
4055{
4056 ((void) session);
4057 ((void) buf);
4058 ((void) buf_len);
4059 *olen = 0;
4060 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4061}
4062
4063static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritiusd36913a2023-01-24 17:48:29 +01004064 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00004065 size_t buf_len)
4066{
4067 ((void) session);
4068 ((void) buf);
4069 ((void) buf_len);
4070 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
4071}
4072#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
4073#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4074
Paul Bakker5121ce52009-01-03 21:22:43 +00004075/*
Hanno Beckera835da52019-05-16 12:39:07 +01004076 * Define ticket header determining Mbed TLS version
4077 * and structure of the ticket.
4078 */
4079
Hanno Becker94ef3b32019-05-16 12:50:45 +01004080/*
Hanno Becker50b59662019-05-28 14:30:45 +01004081 * Define bitflag determining compile-time settings influencing
4082 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004083 */
4084
Hanno Becker50b59662019-05-28 14:30:45 +01004085#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004086#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004087#else
Hanno Becker3e088662019-05-29 11:10:18 +01004088#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004089#endif /* MBEDTLS_HAVE_TIME */
4090
4091#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004092#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004093#else
Hanno Becker3e088662019-05-29 11:10:18 +01004094#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004095#endif /* MBEDTLS_X509_CRT_PARSE_C */
4096
David Horstmann5c5a32f2024-02-13 17:53:35 +00004097#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004098#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004099#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004100#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004101#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4102
Hanno Becker94ef3b32019-05-16 12:50:45 +01004103#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004104#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004105#else
Hanno Becker3e088662019-05-29 11:10:18 +01004106#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004107#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4108
4109#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004110#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004111#else
Hanno Becker3e088662019-05-29 11:10:18 +01004112#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004113#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4114
Hanno Becker94ef3b32019-05-16 12:50:45 +01004115#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004116#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004117#else
Hanno Becker3e088662019-05-29 11:10:18 +01004118#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004119#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4120
Hanno Becker94ef3b32019-05-16 12:50:45 +01004121#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4122#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4123#else
4124#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4125#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4126
David Horstmann92b258b2024-02-13 17:23:34 +00004127#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4128#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4129#else
4130#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4131#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4132
4133#if defined(MBEDTLS_SSL_EARLY_DATA)
4134#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4135#else
4136#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4137#endif /* MBEDTLS_SSL_EARLY_DATA */
4138
4139#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4140#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4141#else
4142#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4143#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4144
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004145#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4146 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004147#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4148#else
4149#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4150#endif /* MBEDTLS_SSL_ALPN */
4151
Hanno Becker3e088662019-05-29 11:10:18 +01004152#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4153#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4154#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4155#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004156#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4157#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004158#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004159#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4160#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4161#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004162#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004163
Hanno Becker50b59662019-05-28 14:30:45 +01004164#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 ((uint16_t) ( \
4166 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4167 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4168 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4169 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4170 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4171 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004172 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004173 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4174 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004175 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4176 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4177 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4178 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004179 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004180 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004181 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004182
Chien Wong4e9683e2023-12-28 17:07:43 +08004183static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004184 MBEDTLS_VERSION_MAJOR,
4185 MBEDTLS_VERSION_MINOR,
4186 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4188 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004189};
Hanno Beckera835da52019-05-16 12:39:07 +01004190
4191/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004192 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004193 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004194 *
David Horstmanncb01b362024-02-29 17:31:46 +00004195 * TLS 1.2 session:
4196 *
4197 * struct {
4198 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4199 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4200 * uint32 ticket_lifetime;
4201 * #endif
4202 * } ClientOnlyData;
4203 *
4204 * struct {
4205 * #if defined(MBEDTLS_HAVE_TIME)
4206 * uint64 start_time;
4207 * #endif
4208 * uint8 session_id_len; // at most 32
4209 * opaque session_id[32];
4210 * opaque master[48]; // fixed length in the standard
4211 * uint32 verify_result;
4212 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4213 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4214 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004215 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004216 * opaque peer_cert_digest<0..2^8-1>
4217 * #endif
4218 * select (endpoint) {
4219 * case client: ClientOnlyData;
4220 * case server: uint64 ticket_creation_time;
4221 * };
4222 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4223 * uint8 mfl_code; // up to 255 according to standard
4224 * #endif
4225 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4226 * uint8 encrypt_then_mac; // 0 or 1
4227 * #endif
4228 * } serialized_session_tls12;
4229 *
4230 *
4231 * TLS 1.3 Session:
4232 *
4233 * struct {
4234 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4235 * opaque hostname<0..2^16-1>;
4236 * #endif
4237 * #if defined(MBEDTLS_HAVE_TIME)
4238 * uint64 ticket_reception_time;
4239 * #endif
4240 * uint32 ticket_lifetime;
4241 * opaque ticket<1..2^16-1>;
4242 * } ClientOnlyData;
4243 *
4244 * struct {
4245 * uint32 ticket_age_add;
4246 * uint8 ticket_flags;
4247 * opaque resumption_key<0..255>;
4248 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4249 * uint32 max_early_data_size;
4250 * #endif
4251 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4252 * uint16 record_size_limit;
4253 * #endif
4254 * select ( endpoint ) {
4255 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004256 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004257 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004258 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004259 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004260 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004261 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004262 * #endif
4263 * };
4264 * } serialized_session_tls13;
4265 *
4266 *
4267 * SSL session:
4268 *
4269 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004270 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004271 * opaque mbedtls_version[3]; // library version: major, minor, patch
4272 * opaque session_format[2]; // library-version specific 16-bit field
4273 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004274 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004275 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004276 * Note: When updating the format, remember to keep
4277 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004278 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004279 * // In this version, `session_format` determines
4280 * // the setting of those compile-time
4281 * // configuration options which influence
4282 * // the structure of mbedtls_ssl_session.
4283 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004284 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004285 * // - TLS 1.2 (0x0303)
4286 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004287 * uint8_t endpoint;
4288 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004289 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004290 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004291 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004292 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004293 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004294 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004295 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004296 *
4297 * };
4298 *
4299 * } serialized_session;
4300 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004301 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004302
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004303MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004304static int ssl_session_save(const mbedtls_ssl_session *session,
4305 unsigned char omit_header,
4306 unsigned char *buf,
4307 size_t buf_len,
4308 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004309{
4310 unsigned char *p = buf;
4311 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004312 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004313#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4314 size_t out_len;
4315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4316#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (session == NULL) {
4318 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4319 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004322 /*
4323 * Add Mbed TLS version identifier
4324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004325 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (used <= buf_len) {
4328 memcpy(p, ssl_serialized_session_header,
4329 sizeof(ssl_serialized_session_header));
4330 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004331 }
4332 }
4333
4334 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004335 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004336 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004337 used += 1 /* TLS version */
4338 + 1 /* endpoint */
4339 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (used <= buf_len) {
4341 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004342 *p++ = session->endpoint;
4343 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4344 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004345 }
4346
4347 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004348 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004350#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 case MBEDTLS_SSL_VERSION_TLS1_2:
4352 used += ssl_tls12_session_save(session, p, remaining_len);
4353 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004354#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4355
Jerry Yu251a12e2022-07-13 15:15:48 +08004356#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 case MBEDTLS_SSL_VERSION_TLS1_3:
4358 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4359 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4360 return ret;
4361 }
4362 used += out_len;
4363 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004364#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 default:
4367 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004368 }
4369
4370 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (used > buf_len) {
4372 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4373 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004376}
4377
4378/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004379 * Public wrapper for ssl_session_save()
4380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004381int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4382 unsigned char *buf,
4383 size_t buf_len,
4384 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004385{
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004387}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004388
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004389/*
4390 * Deserialize session, see mbedtls_ssl_session_save() for format.
4391 *
4392 * This internal version is wrapped by a public function that cleans up in
4393 * case of error, and has an extra option omit_header.
4394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004396static int ssl_session_load(mbedtls_ssl_session *session,
4397 unsigned char omit_header,
4398 const unsigned char *buf,
4399 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004400{
4401 const unsigned char *p = buf;
4402 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004403 size_t remaining_len;
4404
4405
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (session == NULL) {
4407 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4408 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004411 /*
4412 * Check Mbed TLS version identifier
4413 */
4414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4416 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004417 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004418
4419 if (memcmp(p, ssl_serialized_session_header,
4420 sizeof(ssl_serialized_session_header)) != 0) {
4421 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4422 }
4423 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004424 }
4425
4426 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004427 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004428 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004429 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4431 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004432 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004433 session->endpoint = *p++;
4434 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4435 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004436
4437 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004438 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004440#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 case MBEDTLS_SSL_VERSION_TLS1_2:
4442 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4444
Jerry Yu438ddd82022-07-07 06:55:50 +00004445#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 case MBEDTLS_SSL_VERSION_TLS1_3:
4447 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004448#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 default:
4451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004452 }
4453}
4454
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004455/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004456 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004458int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4459 const unsigned char *buf,
4460 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004461{
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (ret != 0) {
4465 mbedtls_ssl_session_free(session);
4466 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004467
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004469}
4470
4471/*
Paul Bakker1961b702013-01-25 14:49:24 +01004472 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004476{
4477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4478
Ronald Cron66dbf912022-02-02 15:33:46 +01004479 /*
4480 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004481 * that were written into the output buffer by the previous handshake step,
4482 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004483 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4484 * We proceed to the next handshake step only when all data from the
4485 * previous one have been sent to the peer, thus we make sure that this is
4486 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4487 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4488 * we have to wait before to go ahead.
4489 * In the case of TLS 1.3, handshake step handlers do not send data to the
4490 * peer. Data are only sent here and through
4491 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004492 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4495 return ret;
4496 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004497
4498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4500 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4501 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4502 return ret;
4503 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004504 }
4505#endif /* MBEDTLS_SSL_PROTO_DTLS */
4506
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004508}
4509
Gilles Peskine449bd832023-01-11 14:50:10 +01004510int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004511{
Hanno Becker41934dd2021-08-07 19:13:43 +01004512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004515 ssl->conf == NULL ||
4516 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004519 }
4520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 ret = ssl_prepare_handshake_step(ssl);
4522 if (ret != 0) {
4523 return ret;
4524 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 ret = mbedtls_ssl_handle_pending_alert(ssl);
4527 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004528 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 }
Jerry Yue7047812021-09-13 19:26:39 +08004530
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004531 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4532 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4533 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004536 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4537 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004538 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004541 case MBEDTLS_SSL_HELLO_REQUEST:
4542 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004543 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004544 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004545
Ronald Cron9f0fba32022-02-10 16:45:15 +01004546 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004548 break;
4549
4550 default:
4551#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4553 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4554 } else {
4555 ret = mbedtls_ssl_handshake_client_step(ssl);
4556 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004557#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004559#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004561#endif
4562 }
Jerry Yub9930e72021-08-06 17:11:51 +08004563 }
Ronald Cron6291b232023-03-08 15:51:25 +01004564#endif /* MBEDTLS_SSL_CLI_C */
4565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004568#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4569 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004571 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 ret = mbedtls_ssl_handshake_server_step(ssl);
4573 }
Ronald Cron6291b232023-03-08 15:51:25 +01004574#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4575 ret = mbedtls_ssl_handshake_server_step(ssl);
4576#else
4577 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004578#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004579 }
4580#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004583 /* handshake_step return error. And it is same
4584 * with alert_reason.
4585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if (ssl->send_alert) {
4587 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004588 goto cleanup;
4589 }
4590 }
4591
4592cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004594}
4595
4596/*
4597 * Perform the SSL handshake
4598 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004599int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004600{
4601 int ret = 0;
4602
Hanno Beckera817ea42020-10-20 15:20:23 +01004603 /* Sanity checks */
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (ssl == NULL || ssl->conf == NULL) {
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4607 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004608
Hanno Beckera817ea42020-10-20 15:20:23 +01004609#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4611 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4612 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4613 "mbedtls_ssl_set_timer_cb() for DTLS"));
4614 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004615 }
4616#endif /* MBEDTLS_SSL_PROTO_DTLS */
4617
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004619
Hanno Beckera817ea42020-10-20 15:20:23 +01004620 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4622 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004625 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 }
Paul Bakker1961b702013-01-25 14:49:24 +01004627 }
4628
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004632}
4633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634#if defined(MBEDTLS_SSL_RENEGOTIATION)
4635#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004636/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004637 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004638 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004639MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004640static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004641{
Janos Follath865b3eb2019-12-16 11:46:15 +00004642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004645
4646 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4648 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4651 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4652 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004653 }
4654
Gilles Peskine449bd832023-01-11 14:50:10 +01004655 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004656
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004660
4661/*
4662 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004663 * - any side: calling mbedtls_ssl_renegotiate(),
4664 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4665 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004666 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004667 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004669 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004670int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004671{
Janos Follath865b3eb2019-12-16 11:46:15 +00004672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if ((ret = ssl_handshake_init(ssl)) != 0) {
4677 return ret;
4678 }
Paul Bakker48916f92012-09-16 19:57:18 +00004679
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004680 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4681 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004682#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4684 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4685 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004686 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004688 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004690 }
4691#endif
4692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4694 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4697 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4698 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004699 }
4700
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004702
Gilles Peskine449bd832023-01-11 14:50:10 +01004703 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004704}
4705
4706/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004707 * Renegotiate current connection on client,
4708 * or request renegotiation on server
4709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004710int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004713
Gilles Peskine449bd832023-01-11 14:50:10 +01004714 if (ssl == NULL || ssl->conf == NULL) {
4715 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4716 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004719 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4721 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4722 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4723 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004725 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004726
4727 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 if (ssl->out_left != 0) {
4729 return mbedtls_ssl_flush_output(ssl);
4730 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004733 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004737 /*
4738 * On client, either start the renegotiation process or,
4739 * if already in progress, continue the handshake
4740 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4742 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004745
4746 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4747 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4748 return ret;
4749 }
4750 } else {
4751 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4752 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4753 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004754 }
4755 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004757
Gilles Peskine449bd832023-01-11 14:50:10 +01004758 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004763{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004764 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4765
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004767 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004769
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004770#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Brett Warrene0edc842021-08-17 09:53:13 +01004771#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if (ssl->handshake->group_list_heap_allocated) {
4773 mbedtls_free((void *) handshake->group_list);
4774 }
Brett Warrene0edc842021-08-17 09:53:13 +01004775 handshake->group_list = NULL;
4776#endif /* MBEDTLS_DEPRECATED_REMOVED */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004777#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Brett Warrene0edc842021-08-17 09:53:13 +01004778
Ronald Crone68ab4f2022-10-05 12:46:29 +02004779#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004780#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 if (ssl->handshake->sig_algs_heap_allocated) {
4782 mbedtls_free((void *) handshake->sig_algs);
4783 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004784 handshake->sig_algs = NULL;
4785#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004786#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 if (ssl->handshake->certificate_request_context) {
4788 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004789 }
4790#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004791#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004792
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004793#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4795 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004796 handshake->async_in_progress = 0;
4797 }
4798#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4799
Elena Uziunaite0916cd72024-05-23 17:01:07 +01004800#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004801#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004803#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004804 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004805#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004806#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01004807#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004808#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004810#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004811 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004812#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004813#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004817#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004818#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004819 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004821#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004822
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004823#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004824#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004825 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004826 /*
4827 * Opaque keys are not stored in the handshake's data and it's the user
4828 * responsibility to destroy them. Clear ones, instead, are created by
4829 * the TLS library and should be destroyed at the same level
4830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4832 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004833 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004834 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4835#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004838#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004840 handshake->ecjpake_cache = NULL;
4841 handshake->ecjpake_cache_len = 0;
4842#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004843#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004844
Valerio Setti7aeec542023-07-05 18:57:21 +02004845#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004846 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004847 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004848 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004850#endif
4851
Ronald Cron73fe8df2022-10-05 14:31:43 +02004852#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004853#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004855 /* The maintenance of the external PSK key slot is the
4856 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 if (ssl->handshake->psk_opaque_is_internal) {
4858 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004859 ssl->handshake->psk_opaque_is_internal = 0;
4860 }
4861 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4862 }
Neil Armstronge952a302022-05-03 10:22:14 +02004863#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004865 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004866 }
Neil Armstronge952a302022-05-03 10:22:14 +02004867#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004868#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4871 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004872 /*
4873 * Free only the linked list wrapper, not the keys themselves
4874 * since the belong to the SNI callback
4875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004878
Gilles Peskineeccd8882020-03-10 12:19:08 +01004879#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4881 if (handshake->ecrs_peer_cert != NULL) {
4882 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4883 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004884 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004885#endif
4886
Hanno Becker75173122019-02-06 16:18:31 +00004887#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4888 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004890#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4891
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004892#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004893 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4894 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004895#endif /* MBEDTLS_SSL_CLI_C &&
4896 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004897
4898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 mbedtls_ssl_flight_free(handshake->flight);
4900 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004902
Valerio Settiea59c432023-07-25 11:14:03 +02004903#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004904 if (handshake->xxdh_psa_privkey_is_external == 0) {
4905 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 }
Valerio Settiea59c432023-07-25 11:14:03 +02004907#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004908
Ronald Cron6f135e12021-12-08 16:57:54 +01004909#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 mbedtls_ssl_transform_free(handshake->transform_handshake);
4911 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004912#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4914 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004915#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004916#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004917
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004918
4919#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4920 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4921 * processes datagrams and the fact that a datagram is allowed to have
4922 * several records in it, it is possible that the I/O buffers are not
4923 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4925 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004926#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004927
Jerry Yuba9c7272021-10-30 11:54:10 +08004928 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 mbedtls_platform_zeroize(handshake,
4930 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004931}
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004934{
Gilles Peskine449bd832023-01-11 14:50:10 +01004935 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004936 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004940 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004941#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004942
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004943#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004944#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4945 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004946 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004947#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004949#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004950
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004951#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4952 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004953 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004954#endif
4955
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004957}
4958
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004959#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004960
4961#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4962#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4963#else
4964#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4965#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4966
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004967#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004968
4969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4970#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4971#else
4972#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4973#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4974
4975#if defined(MBEDTLS_SSL_ALPN)
4976#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4977#else
4978#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4979#endif /* MBEDTLS_SSL_ALPN */
4980
4981#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4982#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4983#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4984#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4985
4986#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 ((uint32_t) ( \
4988 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4989 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4990 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4991 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4992 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4993 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4994 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4995 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004996
Chien Wong4e9683e2023-12-28 17:07:43 +08004997static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004998 MBEDTLS_VERSION_MAJOR,
4999 MBEDTLS_VERSION_MINOR,
5000 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5002 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5003 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5004 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
5005 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005006};
5007
Paul Bakker5121ce52009-01-03 21:22:43 +00005008/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005009 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005010 *
5011 * The format of the serialized data is:
5012 * (in the presentation language of TLS, RFC 8446 section 3)
5013 *
5014 * // header
5015 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005016 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005017 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005018 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02005019 * Note: When updating the format, remember to keep these
5020 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02005021 *
5022 * // session sub-structure
5023 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
5024 * // transform sub-structure
5025 * uint8 random[64]; // ServerHello.random+ClientHello.random
5026 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
5027 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
5028 * // fields from ssl_context
5029 * uint32 badmac_seen; // DTLS: number of records with failing MAC
5030 * uint64 in_window_top; // DTLS: last validated record seq_num
5031 * uint64 in_window; // DTLS: bitmask for replay protection
5032 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
5033 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
5034 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
5035 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
5036 *
5037 * Note that many fields of the ssl_context or sub-structures are not
5038 * serialized, as they fall in one of the following categories:
5039 *
5040 * 1. forced value (eg in_left must be 0)
5041 * 2. pointer to dynamically-allocated memory (eg session, transform)
5042 * 3. value can be re-derived from other data (eg session keys from MS)
5043 * 4. value was temporary (eg content of input buffer)
5044 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005046int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
5047 unsigned char *buf,
5048 size_t buf_len,
5049 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005050{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005051 unsigned char *p = buf;
5052 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005053 size_t session_len;
5054 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005055
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005056 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005057 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
5058 * this function's documentation.
5059 *
5060 * These are due to assumptions/limitations in the implementation. Some of
5061 * them are likely to stay (no handshake in progress) some might go away
5062 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02005063 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005064 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
5066 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
5067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 if (ssl->handshake != NULL) {
5070 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
5071 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005072 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005073 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (ssl->transform == NULL || ssl->session == NULL) {
5075 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
5076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005077 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005078 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 if (mbedtls_ssl_check_pending(ssl) != 0) {
5080 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005082 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005083 if (ssl->out_left != 0) {
5084 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5085 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005086 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005087 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5089 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5090 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005091 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005092 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5094 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5095 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005096 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005097 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5099 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005101 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005102 /* Renegotiation must not be enabled */
5103#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5105 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005107 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005108#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005109
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005110 /*
5111 * Version and format identifier
5112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (used <= buf_len) {
5116 memcpy(p, ssl_serialized_context_header,
5117 sizeof(ssl_serialized_context_header));
5118 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005119 }
5120
5121 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005122 * Session (length + data)
5123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5125 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5126 return ret;
5127 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005128
5129 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 if (used <= buf_len) {
5131 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005132 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005133
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 ret = ssl_session_save(ssl->session, 1,
5135 p, session_len, &session_len);
5136 if (ret != 0) {
5137 return ret;
5138 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005139
5140 p += session_len;
5141 }
5142
5143 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005144 * Transform
5145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 used += sizeof(ssl->transform->randbytes);
5147 if (used <= buf_len) {
5148 memcpy(p, ssl->transform->randbytes,
5149 sizeof(ssl->transform->randbytes));
5150 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005151 }
5152
5153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005154 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005156 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005158 p += ssl->transform->in_cid_len;
5159
5160 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005162 p += ssl->transform->out_cid_len;
5163 }
5164#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5165
5166 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005167 * Saved fields from top-level ssl_context structure
5168 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005169 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 if (used <= buf_len) {
5171 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005172 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005173 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005174
5175#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5176 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005177 if (used <= buf_len) {
5178 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005179 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005182 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005183 }
5184#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5185
5186#if defined(MBEDTLS_SSL_PROTO_DTLS)
5187 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005189 *p++ = ssl->disable_datagram_packing;
5190 }
5191#endif /* MBEDTLS_SSL_PROTO_DTLS */
5192
Jerry Yuae0b2e22021-10-08 15:21:19 +08005193 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (used <= buf_len) {
5195 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005196 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005197 }
5198
5199#if defined(MBEDTLS_SSL_PROTO_DTLS)
5200 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 if (used <= buf_len) {
5202 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005203 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005204 }
5205#endif /* MBEDTLS_SSL_PROTO_DTLS */
5206
5207#if defined(MBEDTLS_SSL_ALPN)
5208 {
5209 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005211 : 0;
5212
5213 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005215 *p++ = alpn_len;
5216
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 if (ssl->alpn_chosen != NULL) {
5218 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005219 p += alpn_len;
5220 }
5221 }
5222 }
5223#endif /* MBEDTLS_SSL_ALPN */
5224
5225 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005226 * Done
5227 */
5228 *olen = used;
5229
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (used > buf_len) {
5231 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5232 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005237}
5238
5239/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005240 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005241 *
5242 * This internal version is wrapped by a public function that cleans up in
5243 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005244 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005245MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005246static int ssl_context_load(mbedtls_ssl_context *ssl,
5247 const unsigned char *buf,
5248 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005249{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005250 const unsigned char *p = buf;
5251 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005252 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005255 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005256#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005257
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005258 /*
5259 * The context should have been freshly setup or reset.
5260 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005261 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005262 * renegotiating, or if the user mistakenly loaded a session first.)
5263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5265 ssl->session != NULL) {
5266 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005267 }
5268
5269 /*
5270 * We can't check that the config matches the initial one, but we can at
5271 * least check it matches the requirements for serializing.
5272 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005273 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005274#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005275 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005276#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005277 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5278 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5279 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5280 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005285
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005286 /*
5287 * Check version identifier
5288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005291 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005292
5293 if (memcmp(p, ssl_serialized_context_header,
5294 sizeof(ssl_serialized_context_header)) != 0) {
5295 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5296 }
5297 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005298
5299 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005300 * Session
5301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 if ((size_t) (end - p) < 4) {
5303 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5304 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005305
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005306 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005307 p += 4;
5308
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005309 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005310 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005311 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005312 ssl->session_in = ssl->session;
5313 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005314 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if ((size_t) (end - p) < session_len) {
5317 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5318 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005319
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 ret = ssl_session_load(ssl->session, 1, p, session_len);
5321 if (ret != 0) {
5322 mbedtls_ssl_session_free(ssl->session);
5323 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005324 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005325
5326 p += session_len;
5327
5328 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005329 * Transform
5330 */
5331
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005332 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005333 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005334#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005335 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005336 ssl->transform_in = ssl->transform;
5337 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005338 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005339#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005340
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005341#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5343 if (prf_func == NULL) {
5344 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5345 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005346
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005347 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5349 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5350 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 ret = ssl_tls12_populate_transform(ssl->transform,
5353 ssl->session->ciphersuite,
5354 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005357#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 prf_func,
5359 p, /* currently pointing to randbytes */
5360 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5361 ssl->conf->endpoint,
5362 ssl);
5363 if (ret != 0) {
5364 return ret;
5365 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005366#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005368
5369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5370 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if ((size_t) (end - p) < 1) {
5372 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5373 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005374
5375 ssl->transform->in_cid_len = *p++;
5376
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5378 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5379 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005380
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005382 p += ssl->transform->in_cid_len;
5383
5384 ssl->transform->out_cid_len = *p++;
5385
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5387 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5388 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005391 p += ssl->transform->out_cid_len;
5392#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5393
5394 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005395 * Saved fields from top-level ssl_context structure
5396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 if ((size_t) (end - p) < 4) {
5398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5399 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005400
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005401 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005402 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005403
5404#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 if ((size_t) (end - p) < 16) {
5406 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5407 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005408
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005409 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005410 p += 8;
5411
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005412 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005413 p += 8;
5414#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5415
5416#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 if ((size_t) (end - p) < 1) {
5418 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5419 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005420
5421 ssl->disable_datagram_packing = *p++;
5422#endif /* MBEDTLS_SSL_PROTO_DTLS */
5423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5425 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5426 }
5427 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5428 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005429
5430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 if ((size_t) (end - p) < 2) {
5432 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5433 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005434
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005435 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005436 p += 2;
5437#endif /* MBEDTLS_SSL_PROTO_DTLS */
5438
5439#if defined(MBEDTLS_SSL_ALPN)
5440 {
5441 uint8_t alpn_len;
5442 const char **cur;
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if ((size_t) (end - p) < 1) {
5445 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5446 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005447
5448 alpn_len = *p++;
5449
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005451 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5453 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005454 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005455 ssl->alpn_chosen = *cur;
5456 break;
5457 }
5458 }
5459 }
5460
5461 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5463 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5464 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005465
5466 p += alpn_len;
5467 }
5468#endif /* MBEDTLS_SSL_ALPN */
5469
5470 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005471 * Forced fields from top-level ssl_context structure
5472 *
5473 * Most of them already set to the correct value by mbedtls_ssl_init() and
5474 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5475 */
5476 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005477 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005478
Hanno Becker361b10d2019-08-30 10:42:49 +01005479 /* Adjust pointers for header fields of outgoing records to
5480 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005482
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005483#if defined(MBEDTLS_SSL_PROTO_DTLS)
5484 ssl->in_epoch = 1;
5485#endif
5486
5487 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5488 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005489 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5490 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 if (ssl->handshake != NULL) {
5492 mbedtls_ssl_handshake_free(ssl);
5493 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005494 ssl->handshake = NULL;
5495 }
5496
5497 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005498 * Done - should have consumed entire buffer
5499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 if (p != end) {
5501 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5502 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005503
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005505}
5506
5507/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005508 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005510int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5511 const unsigned char *buf,
5512 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005513{
Gilles Peskine449bd832023-01-11 14:50:10 +01005514 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 if (ret != 0) {
5517 mbedtls_ssl_free(context);
5518 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005521}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005522#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005523
5524/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 * Free an SSL context
5526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005527void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005528{
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005530 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005532
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005534
Gilles Peskine449bd832023-01-11 14:50:10 +01005535 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005536#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5537 size_t out_buf_len = ssl->out_buf_len;
5538#else
5539 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5540#endif
5541
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005542 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005543 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005544 }
5545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005547#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5548 size_t in_buf_len = ssl->in_buf_len;
5549#else
5550 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5551#endif
5552
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005553 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005554 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 }
5556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (ssl->transform) {
5558 mbedtls_ssl_transform_free(ssl->transform);
5559 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005560 }
5561
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 if (ssl->handshake) {
5563 mbedtls_ssl_handshake_free(ssl);
5564 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005565
5566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5568 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005569#endif
5570
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 mbedtls_ssl_session_free(ssl->session_negotiate);
5572 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005573 }
5574
Ronald Cron6f135e12021-12-08 16:57:54 +01005575#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 mbedtls_ssl_transform_free(ssl->transform_application);
5577 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005578#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005579
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 if (ssl->session) {
5581 mbedtls_ssl_session_free(ssl->session);
5582 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005583 }
5584
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005585#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005587 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005589#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005590
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005593#endif
5594
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005596
Paul Bakker86f04f42013-02-14 11:20:09 +01005597 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005599}
5600
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005601/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005602 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005604void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005605{
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005607}
5608
Gilles Peskineae270bf2021-06-02 00:05:29 +02005609/* The selection should be the same as mbedtls_x509_crt_profile_default in
5610 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005611 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02005612 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005613 * about this list.
5614 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005615static const uint16_t ssl_preset_default_groups[] = {
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005616#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Brett Warrene0edc842021-08-17 09:53:13 +01005617 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005618#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005619#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005620 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005621#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005622#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005623 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005624#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005625#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Brett Warrene0edc842021-08-17 09:53:13 +01005626 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005627#endif
Elena Uziunaite24e24f22024-07-04 17:53:40 +01005628#if defined(PSA_WANT_ECC_SECP_R1_521)
Brett Warrene0edc842021-08-17 09:53:13 +01005629 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005630#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005631#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005632 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005633#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005634#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005635 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005636#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005637#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Brett Warrene0edc842021-08-17 09:53:13 +01005638 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005639#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005640#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005641 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5642 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5643 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5644 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5645 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5646#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005647 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005648};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005649
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005650static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005651 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5652 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5653 0
5654};
5655
Ronald Crone68ab4f2022-10-05 12:46:29 +02005656#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005657
Jerry Yu909df7b2022-01-22 11:56:27 +08005658/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005659 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005660 * rules SHOULD be upheld.
5661 * - No duplicate entries.
5662 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005663 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005664 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005665 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005666static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005667
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005668#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005669 defined(PSA_WANT_ALG_SHA_256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005670 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005671 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005672 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5673#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005674
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005675#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005676 defined(PSA_WANT_ALG_SHA_384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005677 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005678 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005679 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5680#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005681
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005682#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +02005683 defined(PSA_WANT_ALG_SHA_512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005684 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005685 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005686 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5687#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005688
Gabor Mezeic15ef932024-06-13 12:53:54 +02005689#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005690 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005691#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005692
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005693#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005694 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005695#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005696
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005697#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005698 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005699#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005700
Gabor Mezeic15ef932024-06-13 12:53:54 +02005701#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005702 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Gabor Mezeic15ef932024-06-13 12:53:54 +02005703#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005704
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005705#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005706 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005707#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005708
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005709#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005710 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005711#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005712
Gabor Mezei15b95a62022-05-09 16:37:58 +02005713 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005714};
5715
5716/* NOTICE: see above */
5717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5718static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005719
Gabor Mezeic15ef932024-06-13 12:53:54 +02005720#if defined(PSA_WANT_ALG_SHA_512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005721#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005723#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005724#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5725 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005726#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005727#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005729#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005730#endif /* PSA_WANT_ALG_SHA_512 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005731
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005732#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005733#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005735#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005736#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5737 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005738#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005739#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005741#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005742#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005743
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005744#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005745#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005747#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005748#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5749 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005750#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005751#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005753#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005754#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005755
Gabor Mezei15b95a62022-05-09 16:37:58 +02005756 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005757};
5758#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005759
Jerry Yu909df7b2022-01-22 11:56:27 +08005760/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005761static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005762
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005763#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005764 defined(PSA_WANT_ALG_SHA_256) && \
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005765 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005766 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005767 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5768#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005769
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005770#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005771 defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005772 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu53037892022-01-25 11:02:06 +08005773 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005774 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5775#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005776
Gabor Mezei15b95a62022-05-09 16:37:58 +02005777 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005778};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005779
Jerry Yu909df7b2022-01-22 11:56:27 +08005780/* NOTICE: see above */
5781#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5782static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005783
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005784#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005785#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005787#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005788#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005789
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005790#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005791#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005793#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005794#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005795
Gabor Mezei15b95a62022-05-09 16:37:58 +02005796 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005797};
Jerry Yu909df7b2022-01-22 11:56:27 +08005798#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5799
Ronald Crone68ab4f2022-10-05 12:46:29 +02005800#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005801
Chien Wong4e9683e2023-12-28 17:07:43 +08005802static const uint16_t ssl_preset_suiteb_groups[] = {
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005803#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005804 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005805#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005806#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005807 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005808#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005809 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005810};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005811
Ronald Crone68ab4f2022-10-05 12:46:29 +02005812#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005813/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005814 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005815MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005816static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005817{
5818 size_t i, j;
5819 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5822 for (j = 0; j < i; j++) {
5823 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005824 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 }
5826 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5827 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5828 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005829 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005830 }
5831 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005833}
5834
Ronald Crone68ab4f2022-10-05 12:46:29 +02005835#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005836
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005837/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005838 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005840int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5841 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005842{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005843#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005845#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005846
Ronald Crone68ab4f2022-10-05 12:46:29 +02005847#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5849 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5850 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005851 }
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5854 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5855 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005856 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005857
5858#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5860 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5861 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005862 }
5863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5865 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5866 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005867 }
5868#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005869#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005870
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005871 /* Use the functions here so that they are covered in tests,
5872 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 mbedtls_ssl_conf_endpoint(conf, endpoint);
5874 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005875
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005876 /*
5877 * Things that are common to all presets
5878 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005879#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005881 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5882#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5883 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5884#endif
5885 }
5886#endif
5887
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005888#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5889 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5890#endif
5891
5892#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5893 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5894#endif
5895
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005896#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005897 conf->f_cookie_write = ssl_cookie_write_dummy;
5898 conf->f_cookie_check = ssl_cookie_check_dummy;
5899#endif
5900
5901#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5902 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5903#endif
5904
Janos Follath088ce432017-04-10 12:42:31 +01005905#if defined(MBEDTLS_SSL_SRV_C)
5906 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005907 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005908#endif
5909
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005910#if defined(MBEDTLS_SSL_PROTO_DTLS)
5911 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5912 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5913#endif
5914
5915#if defined(MBEDTLS_SSL_RENEGOTIATION)
5916 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 memset(conf->renego_period, 0x00, 2);
5918 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005919#endif
5920
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005921#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005923 const unsigned char dhm_p[] =
5924 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5925 const unsigned char dhm_g[] =
5926 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5929 dhm_p, sizeof(dhm_p),
5930 dhm_g, sizeof(dhm_g))) != 0) {
5931 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005932 }
5933 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005934#endif
5935
Ronald Cron6f135e12021-12-08 16:57:54 +01005936#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005937
5938#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005939 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005940#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005941 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005942#endif
5943#endif /* MBEDTLS_SSL_EARLY_DATA */
5944
Jerry Yud0766ec2022-09-22 10:46:57 +08005945#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005946 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005947 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005948#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005949 /*
5950 * Allow all TLS 1.3 key exchange modes by default.
5951 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005952 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005953#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005954
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005956#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005957 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005958 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005959#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005961#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005963#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005964 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5965 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005966#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5967 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5968 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5969#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5970 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5971 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5972#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005974#endif
5975 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005976
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005977 /*
5978 * Preset-specific defaults
5979 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005981 /*
5982 * NSA Suite B
5983 */
5984 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005985
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005986 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005987
5988#if defined(MBEDTLS_X509_CRT_PARSE_C)
5989 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005990#endif
5991
Ronald Crone68ab4f2022-10-05 12:46:29 +02005992#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005993#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005995 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005997#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005999#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006000
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006001#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006002 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006003#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006004 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02006005 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006006
6007 /*
6008 * Default
6009 */
6010 default:
Ronald Cronf6606552022-03-15 11:23:25 +01006011
Hanno Beckerd60b6c62021-04-29 12:04:11 +01006012 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006013
6014#if defined(MBEDTLS_X509_CRT_PARSE_C)
6015 conf->cert_profile = &mbedtls_x509_crt_profile_default;
6016#endif
6017
Ronald Crone68ab4f2022-10-05 12:46:29 +02006018#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08006019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08006021 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08006023#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02006025#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006026
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02006027#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01006028 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006029#endif
Brett Warrene0edc842021-08-17 09:53:13 +01006030 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006031
6032#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
6033 conf->dhm_min_bitlen = 1024;
6034#endif
6035 }
6036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006038}
6039
6040/*
6041 * Free mbedtls_ssl_config
6042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006043void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006044{
Troy-Butlerda73abc2024-04-02 13:37:31 -04006045 if (conf == NULL) {
6046 return;
6047 }
6048
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006049#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 mbedtls_mpi_free(&conf->dhm_P);
6051 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006052#endif
6053
Ronald Cron73fe8df2022-10-05 14:31:43 +02006054#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02006055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02006057 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
6058 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02006059#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006061 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006062 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006063 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09006064 }
6065
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006067 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00006068 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006069 conf->psk_identity_len = 0;
6070 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02006071#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006072
6073#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006075#endif
6076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006078}
6079
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02006080#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02006081 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006082/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006085unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006086{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6089 return MBEDTLS_SSL_SIG_RSA;
6090 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006091#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006092#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6094 return MBEDTLS_SSL_SIG_ECDSA;
6095 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006096#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006098}
6099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006101{
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006103 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006105 case MBEDTLS_PK_ECDSA:
6106 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006108 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006110 }
6111}
6112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006114{
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#if defined(MBEDTLS_RSA_C)
6117 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006119#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006120#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006123#endif
6124 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006126 }
6127}
Valerio Settie9646ec2023-08-02 20:02:28 +02006128#endif /* MBEDTLS_PK_C &&
6129 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006130
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006131/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006132 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006134mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006135{
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 switch (hash) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006137#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006140#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006141#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006144#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006145#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006148#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006149#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006150 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006152#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006153#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006154 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006156#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006157#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006160#endif
6161 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006163 }
6164}
6165
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006166/*
6167 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006169unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006170{
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 switch (md) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006172#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006173 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006175#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006176#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006177 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006179#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006180#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006181 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006183#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006184#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006185 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006187#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006188#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006189 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006191#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006192#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006193 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006195#endif
6196 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006198 }
6199}
6200
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006201/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006202 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006203 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006205int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006206{
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006208
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 if (group_list == NULL) {
6210 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006211 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 for (; *group_list != 0; group_list++) {
6214 if (*group_list == tls_id) {
6215 return 0;
6216 }
6217 }
6218
6219 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006220}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006221
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006222#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006223/*
6224 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006226int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006227{
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006231 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006232 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006235}
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006236#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Valerio Setti67419f02023-01-04 16:12:42 +01006237
Valerio Setti18c9fed2022-12-30 17:44:24 +01006238static const struct {
6239 uint16_t tls_id;
6240 mbedtls_ecp_group_id ecp_group_id;
6241 psa_ecc_family_t psa_family;
6242 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006243} tls_id_match_table[] =
6244{
Elena Uziunaite24e24f22024-07-04 17:53:40 +01006245#if defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settiacd32c02023-06-29 18:06:29 +02006246 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006247#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006248#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settiacd32c02023-06-29 18:06:29 +02006249 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006250#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01006251#if defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006252 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006253#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006254#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006255 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006256#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01006257#if defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006258 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006259#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006260#if defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006261 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006262#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006263#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006264 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006265#endif
Elena Uziunaiteeaa0cf02024-07-04 17:41:25 +01006266#if defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02006267 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006268#endif
Elena Uziunaite63cb13e2024-09-05 12:43:14 +01006269#if defined(PSA_WANT_ECC_SECP_K1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02006270 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006271#endif
Elena Uziunaitea3632862024-07-04 13:52:43 +01006272#if defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006273 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006274#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006275#if defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006276 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006277#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006278#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settiacd32c02023-06-29 18:06:29 +02006279 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006280#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006281#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006282 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006283#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006284 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006285};
6286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006288 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006290{
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6292 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006293 if (type != NULL) {
6294 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 }
6296 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006297 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006299 return PSA_SUCCESS;
6300 }
6301 }
6302
6303 return PSA_ERROR_NOT_SUPPORTED;
6304}
6305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006307{
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6309 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006310 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006312 }
6313
6314 return MBEDTLS_ECP_DP_NONE;
6315}
6316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006318{
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6320 i++) {
6321 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006322 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006324 }
6325
6326 return 0;
6327}
6328
Valerio Setti67419f02023-01-04 16:12:42 +01006329#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006330static const struct {
6331 uint16_t tls_id;
6332 const char *name;
6333} tls_id_curve_name_table[] =
6334{
Valerio Setti54e23792023-07-07 10:49:27 +02006335 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6336 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6337 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6338 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6339 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6340 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6341 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6342 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6343 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6344 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6345 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6346 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6347 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006348 { 0, NULL },
6349};
6350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006352{
Valerio Settiacd32c02023-06-29 18:06:29 +02006353 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6354 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6355 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006356 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006357 }
6358
6359 return NULL;
6360}
Valerio Setti67419f02023-01-04 16:12:42 +01006361#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006362
Jerry Yu148165c2021-09-24 23:20:59 +08006363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006364int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6365 const mbedtls_md_type_t md,
6366 unsigned char *dst,
6367 size_t dst_len,
6368 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006369{
Ronald Cronf6893e12022-01-07 22:09:01 +01006370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6371 psa_hash_operation_t *hash_operation_to_clone;
6372 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6373
Jerry Yu148165c2021-09-24 23:20:59 +08006374 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006377#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 case MBEDTLS_MD_SHA384:
6379 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6380 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006381#endif
6382
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006383#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 case MBEDTLS_MD_SHA256:
6385 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6386 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006387#endif
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 default:
6390 goto exit;
6391 }
6392
6393 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6394 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006395 goto exit;
6396 }
6397
Gilles Peskine449bd832023-01-11 14:50:10 +01006398 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6399 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006400 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006402
6403exit:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006404#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006405 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006406 (void) ssl;
6407#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006408 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006409}
6410#else /* MBEDTLS_USE_PSA_CRYPTO */
6411
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006412#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006413MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006414static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6415 unsigned char *dst,
6416 size_t dst_len,
6417 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006418{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006419 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006420 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (dst_len < 48) {
6423 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6424 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006425
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006426 mbedtls_md_init(&sha384);
6427 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006428 if (ret != 0) {
6429 goto exit;
6430 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006431 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006432 if (ret != 0) {
6433 goto exit;
6434 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006435
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006436 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006437 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006438 goto exit;
6439 }
6440
6441 *olen = 48;
6442
6443exit:
6444
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006445 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006447}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006448#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006449
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006450#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006451MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006452static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6453 unsigned char *dst,
6454 size_t dst_len,
6455 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006456{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006457 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006458 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (dst_len < 32) {
6461 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6462 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006463
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006464 mbedtls_md_init(&sha256);
6465 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6466 if (ret != 0) {
6467 goto exit;
6468 }
6469 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6470 if (ret != 0) {
6471 goto exit;
6472 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006473
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006474 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6475 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006476 goto exit;
6477 }
6478
6479 *olen = 32;
6480
6481exit:
6482
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006483 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006485}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006486#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6489 const mbedtls_md_type_t md,
6490 unsigned char *dst,
6491 size_t dst_len,
6492 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006493{
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006495
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006496#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 case MBEDTLS_MD_SHA384:
6498 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006499#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006500
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006501#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 case MBEDTLS_MD_SHA256:
6503 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006504#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 default:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006507#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006508 !defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 (void) ssl;
6510 (void) dst;
6511 (void) dst_len;
6512 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006513#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006515 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006517}
XiaokangQian647719a2021-12-07 09:16:29 +00006518
Jerry Yu148165c2021-09-24 23:20:59 +08006519#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006520
Ronald Crone68ab4f2022-10-05 12:46:29 +02006521#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006522/* mbedtls_ssl_parse_sig_alg_ext()
6523 *
6524 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6525 * value (TLS 1.3 RFC8446):
6526 * enum {
6527 * ....
6528 * ecdsa_secp256r1_sha256( 0x0403 ),
6529 * ecdsa_secp384r1_sha384( 0x0503 ),
6530 * ecdsa_secp521r1_sha512( 0x0603 ),
6531 * ....
6532 * } SignatureScheme;
6533 *
6534 * struct {
6535 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6536 * } SignatureSchemeList;
6537 *
6538 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6539 * value (TLS 1.2 RFC5246):
6540 * enum {
6541 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6542 * sha512(6), (255)
6543 * } HashAlgorithm;
6544 *
6545 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6546 * SignatureAlgorithm;
6547 *
6548 * struct {
6549 * HashAlgorithm hash;
6550 * SignatureAlgorithm signature;
6551 * } SignatureAndHashAlgorithm;
6552 *
6553 * SignatureAndHashAlgorithm
6554 * supported_signature_algorithms<2..2^16-2>;
6555 *
6556 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6557 * generalization of the TLS 1.2 signature algorithm extension.
6558 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6559 * `SignatureScheme` field of TLS 1.3
6560 *
6561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006562int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6563 const unsigned char *buf,
6564 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006565{
6566 const unsigned char *p = buf;
6567 size_t supported_sig_algs_len = 0;
6568 const unsigned char *supported_sig_algs_end;
6569 uint16_t sig_alg;
6570 uint32_t common_idx = 0;
6571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6573 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006574 p += 2;
6575
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 memset(ssl->handshake->received_sig_algs, 0,
6577 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006580 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 while (p < supported_sig_algs_end) {
6582 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6583 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006584 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6586 sig_alg,
6587 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006588#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6590 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6591 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006592 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006593 }
6594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6597 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006600 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6601 common_idx += 1;
6602 }
6603 }
6604 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 if (p != end) {
6606 MBEDTLS_SSL_DEBUG_MSG(1,
6607 ("Signature algorithms extension length misaligned"));
6608 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6609 MBEDTLS_ERR_SSL_DECODE_ERROR);
6610 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006611 }
6612
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 if (common_idx == 0) {
6614 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6615 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6616 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6617 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006618 }
6619
Gabor Mezei15b95a62022-05-09 16:37:58 +02006620 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006622}
6623
Ronald Crone68ab4f2022-10-05 12:46:29 +02006624#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006625
Jerry Yudc7bd172022-02-17 13:44:15 +08006626#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6627
6628#if defined(MBEDTLS_USE_PSA_CRYPTO)
6629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6631 mbedtls_svc_key_id_t key,
6632 psa_algorithm_t alg,
6633 const unsigned char *raw_psk, size_t raw_psk_length,
6634 const unsigned char *seed, size_t seed_length,
6635 const unsigned char *label, size_t label_length,
6636 const unsigned char *other_secret,
6637 size_t other_secret_length,
6638 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006639{
6640 psa_status_t status;
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 status = psa_key_derivation_setup(derivation, alg);
6643 if (status != PSA_SUCCESS) {
6644 return status;
6645 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6648 status = psa_key_derivation_input_bytes(derivation,
6649 PSA_KEY_DERIVATION_INPUT_SEED,
6650 seed, seed_length);
6651 if (status != PSA_SUCCESS) {
6652 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006653 }
6654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 if (other_secret != NULL) {
6656 status = psa_key_derivation_input_bytes(derivation,
6657 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6658 other_secret, other_secret_length);
6659 if (status != PSA_SUCCESS) {
6660 return status;
6661 }
6662 }
6663
6664 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006665 status = psa_key_derivation_input_bytes(
6666 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 raw_psk, raw_psk_length);
6668 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006669 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 if (status != PSA_SUCCESS) {
6673 return status;
6674 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 status = psa_key_derivation_input_bytes(derivation,
6677 PSA_KEY_DERIVATION_INPUT_LABEL,
6678 label, label_length);
6679 if (status != PSA_SUCCESS) {
6680 return status;
6681 }
6682 } else {
6683 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006684 }
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 status = psa_key_derivation_set_capacity(derivation, capacity);
6687 if (status != PSA_SUCCESS) {
6688 return status;
6689 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006692}
6693
Andrzej Kurek57d10632022-10-24 10:32:01 -04006694#if defined(PSA_WANT_ALG_SHA_384) || \
6695 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006697static int tls_prf_generic(mbedtls_md_type_t md_type,
6698 const unsigned char *secret, size_t slen,
6699 const char *label,
6700 const unsigned char *random, size_t rlen,
6701 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006702{
6703 psa_status_t status;
6704 psa_algorithm_t alg;
6705 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6706 psa_key_derivation_operation_t derivation =
6707 PSA_KEY_DERIVATION_OPERATION_INIT;
6708
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006710 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006711 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006712 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006714
6715 /* Normally a "secret" should be long enough to be impossible to
6716 * find by brute force, and in particular should not be empty. But
6717 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6718 * and for this use case it makes sense to have a 0-length "secret".
6719 * Since the key API doesn't allow importing a key of length 0,
6720 * keep master_key=0, which setup_psa_key_derivation() understands
6721 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006723 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6725 psa_set_key_algorithm(&key_attributes, alg);
6726 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6729 if (status != PSA_SUCCESS) {
6730 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6731 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006732 }
6733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 status = setup_psa_key_derivation(&derivation,
6735 master_key, alg,
6736 NULL, 0,
6737 random, rlen,
6738 (unsigned char const *) label,
6739 (size_t) strlen(label),
6740 NULL, 0,
6741 dlen);
6742 if (status != PSA_SUCCESS) {
6743 psa_key_derivation_abort(&derivation);
6744 psa_destroy_key(master_key);
6745 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006746 }
6747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6749 if (status != PSA_SUCCESS) {
6750 psa_key_derivation_abort(&derivation);
6751 psa_destroy_key(master_key);
6752 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006753 }
6754
Gilles Peskine449bd832023-01-11 14:50:10 +01006755 status = psa_key_derivation_abort(&derivation);
6756 if (status != PSA_SUCCESS) {
6757 psa_destroy_key(master_key);
6758 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006759 }
6760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 if (!mbedtls_svc_key_id_is_null(master_key)) {
6762 status = psa_destroy_key(master_key);
6763 }
6764 if (status != PSA_SUCCESS) {
6765 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6766 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006767
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006769}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006770#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006771#else /* MBEDTLS_USE_PSA_CRYPTO */
6772
Andrzej Kurek57d10632022-10-24 10:32:01 -04006773#if defined(MBEDTLS_MD_C) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006774 (defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006775 defined(PSA_WANT_ALG_SHA_384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006776MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006777static int tls_prf_generic(mbedtls_md_type_t md_type,
6778 const unsigned char *secret, size_t slen,
6779 const char *label,
6780 const unsigned char *random, size_t rlen,
6781 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006782{
6783 size_t nb;
6784 size_t i, j, k, md_len;
6785 unsigned char *tmp;
6786 size_t tmp_len = 0;
6787 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6788 const mbedtls_md_info_t *md_info;
6789 mbedtls_md_context_t md_ctx;
6790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6795 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6796 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006797
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006799
Gilles Peskine449bd832023-01-11 14:50:10 +01006800 tmp_len = md_len + strlen(label) + rlen;
6801 tmp = mbedtls_calloc(1, tmp_len);
6802 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006803 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6804 goto exit;
6805 }
6806
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 nb = strlen(label);
6808 memcpy(tmp + md_len, label, nb);
6809 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006810 nb += rlen;
6811
6812 /*
6813 * Compute P_<hash>(secret, label + random)[0..dlen]
6814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006816 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6820 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006821 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 }
6823 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6824 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006825 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 }
6827 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6828 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006829 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006831
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 for (i = 0; i < dlen; i += md_len) {
6833 ret = mbedtls_md_hmac_reset(&md_ctx);
6834 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006835 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 }
6837 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6838 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006839 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 }
6841 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6842 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006843 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 ret = mbedtls_md_hmac_reset(&md_ctx);
6847 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006848 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 }
6850 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6851 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006852 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 }
6854 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6855 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006856 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006860
Gilles Peskine449bd832023-01-11 14:50:10 +01006861 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006862 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006864 }
6865
6866exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 if (tmp != NULL) {
6870 mbedtls_platform_zeroize(tmp, tmp_len);
6871 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006878}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006879#endif /* MBEDTLS_MD_C && ( PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006880#endif /* MBEDTLS_USE_PSA_CRYPTO */
6881
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006882#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006884static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6885 const char *label,
6886 const unsigned char *random, size_t rlen,
6887 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006888{
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6890 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006891}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006892#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006893
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006894#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006895MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006896static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6897 const char *label,
6898 const unsigned char *random, size_t rlen,
6899 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006900{
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6902 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006903}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006904#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006905
Jerry Yuf009d862022-02-17 14:01:37 +08006906/*
6907 * Set appropriate PRF function and other SSL / TLS1.2 functions
6908 *
6909 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006910 * - hash associated with the ciphersuite (only used by TLS 1.2)
6911 *
6912 * Outputs:
6913 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6914 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006915MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006916static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6917 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006918{
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006919#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006921 handshake->tls_prf = tls_prf_sha384;
6922 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6923 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006925#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006926#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuf009d862022-02-17 14:01:37 +08006927 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006928 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006929 handshake->tls_prf = tls_prf_sha256;
6930 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6931 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6932 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006933#else
Jerry Yuf009d862022-02-17 14:01:37 +08006934 {
Jerry Yuf009d862022-02-17 14:01:37 +08006935 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006936 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006938 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006939#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006942}
Jerry Yud6ab2352022-02-17 14:03:43 +08006943
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006944/*
6945 * Compute master secret if needed
6946 *
6947 * Parameters:
6948 * [in/out] handshake
6949 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6950 * (PSA-PSK) ciphersuite_info, psk_opaque
6951 * [out] premaster (cleared)
6952 * [out] master
6953 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6954 * debug: conf->f_dbg, conf->p_dbg
6955 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006956 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006957 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006958MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006959static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6960 unsigned char *master,
6961 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006962{
6963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6964
6965 /* cf. RFC 5246, Section 8.1:
6966 * "The master secret is always exactly 48 bytes in length." */
6967 size_t const master_secret_len = 48;
6968
6969#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6970 unsigned char session_hash[48];
6971#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6972
6973 /* The label for the KDF used for key expansion.
6974 * This is either "master secret" or "extended master secret"
6975 * depending on whether the Extended Master Secret extension
6976 * is used. */
6977 char const *lbl = "master secret";
6978
Przemek Stekielae4ed302022-04-05 17:15:55 +02006979 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006980 * - If the Extended Master Secret extension is not used,
6981 * this is ClientHello.Random + ServerHello.Random
6982 * (see Sect. 8.1 in RFC 5246).
6983 * - If the Extended Master Secret extension is used,
6984 * this is the transcript of the handshake so far.
6985 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006986 unsigned char const *seed = handshake->randbytes;
6987 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006988
6989#if !defined(MBEDTLS_DEBUG_C) && \
6990 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6991 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006993 ssl = NULL; /* make sure we don't use it except for those cases */
6994 (void) ssl;
6995#endif
6996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 if (handshake->resume != 0) {
6998 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6999 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007000 }
7001
7002#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007004 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02007005 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007006 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
7007 if (ret != 0) {
7008 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
7009 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
7012 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007013 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02007014#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007015
Przemek Stekiel99114f32022-04-22 11:20:09 +02007016#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02007017 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007019 /* Perform PSK-to-MS expansion in a single step. */
7020 psa_status_t status;
7021 psa_algorithm_t alg;
7022 mbedtls_svc_key_id_t psk;
7023 psa_key_derivation_operation_t derivation =
7024 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01007025 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007026
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007028
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007032 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01007033 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007034 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007036
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007037 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02007039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007041 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02007042 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007043 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02007044 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007045 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007046 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02007047 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
7048 other_secret = handshake->premaster + 2;
7049 break;
7050 default:
7051 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02007052 }
7053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 status = setup_psa_key_derivation(&derivation, psk, alg,
7055 ssl->conf->psk, ssl->conf->psk_len,
7056 seed, seed_len,
7057 (unsigned char const *) lbl,
7058 (size_t) strlen(lbl),
7059 other_secret, other_secret_len,
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_output_bytes(&derivation,
7067 master,
7068 master_secret_len);
7069 if (status != PSA_SUCCESS) {
7070 psa_key_derivation_abort(&derivation);
7071 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007072 }
7073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 status = psa_key_derivation_abort(&derivation);
7075 if (status != PSA_SUCCESS) {
7076 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7077 }
7078 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007079#endif
7080 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007081#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
7083 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02007084 psa_status_t status;
7085 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7086 psa_key_derivation_operation_t derivation =
7087 PSA_KEY_DERIVATION_OPERATION_INIT;
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007090
7091 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7092
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 status = psa_key_derivation_setup(&derivation, alg);
7094 if (status != PSA_SUCCESS) {
7095 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007096 }
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 status = psa_key_derivation_set_capacity(&derivation,
7099 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7100 if (status != PSA_SUCCESS) {
7101 psa_key_derivation_abort(&derivation);
7102 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007103 }
7104
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7106 &derivation);
7107 if (status != PSA_SUCCESS) {
7108 psa_key_derivation_abort(&derivation);
7109 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007110 }
7111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 status = psa_key_derivation_output_bytes(&derivation,
7113 handshake->premaster,
7114 handshake->pmslen);
7115 if (status != PSA_SUCCESS) {
7116 psa_key_derivation_abort(&derivation);
7117 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7118 }
7119
7120 status = psa_key_derivation_abort(&derivation);
7121 if (status != PSA_SUCCESS) {
7122 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007123 }
7124 }
7125#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7127 lbl, seed, seed_len,
7128 master,
7129 master_secret_len);
7130 if (ret != 0) {
7131 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7132 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007133 }
7134
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7136 handshake->premaster,
7137 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 mbedtls_platform_zeroize(handshake->premaster,
7140 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007141 }
7142
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007144}
7145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007147{
7148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7149 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7150 ssl->handshake->ciphersuite_info;
7151
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007153
7154 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007156 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 if (ret != 0) {
7158 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7159 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007160 }
7161
7162 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007163 ret = ssl_compute_master(ssl->handshake,
7164 ssl->session_negotiate->master,
7165 ssl);
7166 if (ret != 0) {
7167 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7168 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007169 }
7170
7171 /* Swap the client and server random values:
7172 * - MS derivation wanted client+server (RFC 5246 8.1)
7173 * - key derivation wants server+client (RFC 5246 6.3) */
7174 {
7175 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 memcpy(tmp, ssl->handshake->randbytes, 64);
7177 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7178 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7179 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007180 }
7181
7182 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7184 ssl->session_negotiate->ciphersuite,
7185 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007186#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007188#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 ssl->handshake->tls_prf,
7190 ssl->handshake->randbytes,
7191 ssl->tls_version,
7192 ssl->conf->endpoint,
7193 ssl);
7194 if (ret != 0) {
7195 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7196 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007197 }
7198
7199 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7201 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007202
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007204
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007206}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007207
Gilles Peskine449bd832023-01-11 14:50:10 +01007208int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007209{
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007211#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007212 case MBEDTLS_SSL_HASH_SHA384:
7213 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7214 break;
7215#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007216#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007217 case MBEDTLS_SSL_HASH_SHA256:
7218 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7219 break;
7220#endif
7221 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007223 }
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007224#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007225 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007226 (void) ssl;
7227#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007229}
7230
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007231#if defined(MBEDTLS_USE_PSA_CRYPTO)
7232static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7233 const psa_hash_operation_t *hs_op,
7234 size_t buffer_size,
7235 unsigned char *hash,
7236 size_t *hlen)
7237{
7238 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007239 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007240
7241#if !defined(MBEDTLS_DEBUG_C)
7242 (void) ssl;
7243#endif
7244 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007245 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007246 if (status != PSA_SUCCESS) {
7247 goto exit;
7248 }
7249
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007250 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007251 if (status != PSA_SUCCESS) {
7252 goto exit;
7253 }
7254
7255 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7256 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7257
7258exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007259 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007260 return mbedtls_md_error_from_psa(status);
7261}
7262#else
7263static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7264 const mbedtls_md_context_t *hs_ctx,
7265 unsigned char *hash,
7266 size_t *hlen)
7267{
7268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007269 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007270
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007271 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007272
7273#if !defined(MBEDTLS_DEBUG_C)
7274 (void) ssl;
7275#endif
7276 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7277
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007278 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007279 if (ret != 0) {
7280 goto exit;
7281 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007282 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007283 if (ret != 0) {
7284 goto exit;
7285 }
7286
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007287 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007288 if (ret != 0) {
7289 goto exit;
7290 }
7291
7292 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7293
7294 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7295 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7296
7297exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007298 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007299 return ret;
7300}
7301#endif /* MBEDTLS_USE_PSA_CRYPTO */
7302
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007303#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007304int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7305 unsigned char *hash,
7306 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007307{
7308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007309 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7310 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007311#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007312 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7313 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007314#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007315}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007316#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007317
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007318#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007319int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7320 unsigned char *hash,
7321 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007322{
7323#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007324 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7325 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007326#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007327 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7328 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007329#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007330}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007331#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007332
Neil Armstrong80f6f322022-05-03 17:56:38 +02007333#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7334 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007335int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007336{
7337 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007339 const unsigned char *psk = NULL;
7340 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007344 /*
7345 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007346 * checked before calling this function.
7347 *
7348 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007349 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7352 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7353 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007354 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007355 }
7356
7357 /*
7358 * PMS = struct {
7359 * opaque other_secret<0..2^16-1>;
7360 * opaque psk<0..2^16-1>;
7361 * };
7362 * with "other_secret" depending on the particular key exchange
7363 */
7364#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7366 if (end - p < 2) {
7367 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7368 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007369
Gilles Peskine449bd832023-01-11 14:50:10 +01007370 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007371 p += 2;
7372
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 if (end < p || (size_t) (end - p) < psk_len) {
7374 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7375 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007378 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007380#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007381#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7384 size_t len;
7385
7386 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007388 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7390 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7391 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007392 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007394 p += 2 + len;
7395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7397 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007398#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007399#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7402 size_t zlen;
7403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007405 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7407 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7408 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007409 }
7410
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007412 p += 2 + zlen;
7413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7415 MBEDTLS_DEBUG_ECDH_Z);
7416 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007417#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007418 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7420 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007421 }
7422
7423 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 if (end - p < 2) {
7425 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7426 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007427
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007429 p += 2;
7430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if (end < p || (size_t) (end - p) < psk_len) {
7432 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7433 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007434
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007436 p += psk_len;
7437
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007438 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007439
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007441}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007442#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007443
7444#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007445MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007446static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007447
7448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007449int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007450{
7451 /* If renegotiation is not enforced, retransmit until we would reach max
7452 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007454 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7455 unsigned char doublings = 1;
7456
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007458 ++doublings;
7459 ratio >>= 1;
7460 }
7461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 if (++ssl->renego_records_seen > doublings) {
7463 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7464 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007465 }
7466 }
7467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007469}
7470#endif
7471#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007472
Jerry Yud9526692022-02-17 14:23:47 +08007473/*
7474 * Handshake functions
7475 */
7476#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7477/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007478int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007479{
7480 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7481 ssl->handshake->ciphersuite_info;
7482
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007484
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7486 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007487 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007489 }
7490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7492 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007493}
7494
Gilles Peskine449bd832023-01-11 14:50:10 +01007495int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007496{
7497 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7498 ssl->handshake->ciphersuite_info;
7499
Gilles Peskine449bd832023-01-11 14:50:10 +01007500 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007501
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7503 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007504 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007506 }
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7509 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007510}
7511
7512#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7513/* Some certificate support -> implement write and parse */
7514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007516{
7517 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7518 size_t i, n;
7519 const mbedtls_x509_crt *crt;
7520 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7521 ssl->handshake->ciphersuite_info;
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
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#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7533 if (ssl->handshake->client_auth == 0) {
7534 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007535 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007537 }
7538 }
7539#endif /* MBEDTLS_SSL_CLI_C */
7540#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7542 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007543 /* Should never happen because we shouldn't have picked the
7544 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007546 }
7547 }
7548#endif
7549
Gilles Peskine449bd832023-01-11 14:50:10 +01007550 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007551
7552 /*
7553 * 0 . 0 handshake type
7554 * 1 . 3 handshake length
7555 * 4 . 6 length of all certs
7556 * 7 . 9 length of cert. 1
7557 * 10 . n-1 peer certificate
7558 * n . n+2 length of cert. 2
7559 * n+3 . ... upper level cert, etc.
7560 */
7561 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007565 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7567 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7568 " > %" MBEDTLS_PRINTF_SIZET,
7569 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7570 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007571 }
7572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7574 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7575 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007578 i += n; crt = crt->next;
7579 }
7580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7582 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7583 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007584
7585 ssl->out_msglen = i;
7586 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7587 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7588
7589 ssl->state++;
7590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7592 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7593 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007594 }
7595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007599}
7600
7601#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7602
7603#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007604MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007605static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7606 unsigned char *crt_buf,
7607 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007608{
7609 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 if (peer_crt == NULL) {
7612 return -1;
7613 }
Jerry Yud9526692022-02-17 14:23:47 +08007614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (peer_crt->raw.len != crt_buf_len) {
7616 return -1;
7617 }
Jerry Yud9526692022-02-17 14:23:47 +08007618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007620}
7621#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007623static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7624 unsigned char *crt_buf,
7625 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007626{
7627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7628 unsigned char const * const peer_cert_digest =
7629 ssl->session->peer_cert_digest;
7630 mbedtls_md_type_t const peer_cert_digest_type =
7631 ssl->session->peer_cert_digest_type;
7632 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007634 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7635 size_t digest_len;
7636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 if (peer_cert_digest == NULL || digest_info == NULL) {
7638 return -1;
7639 }
Jerry Yud9526692022-02-17 14:23:47 +08007640
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 digest_len = mbedtls_md_get_size(digest_info);
7642 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7643 return -1;
7644 }
Jerry Yud9526692022-02-17 14:23:47 +08007645
Gilles Peskine449bd832023-01-11 14:50:10 +01007646 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7647 if (ret != 0) {
7648 return -1;
7649 }
Jerry Yud9526692022-02-17 14:23:47 +08007650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007652}
7653#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7654#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7655
7656/*
7657 * Once the certificate message is read, parse it into a cert chain and
7658 * perform basic checks, but leave actual verification to the caller
7659 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007660MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007661static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7662 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007663{
7664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7665#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007667#endif
7668 size_t i, n;
7669 uint8_t alert;
7670
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7672 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7673 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7674 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7675 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007676 }
7677
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7679 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7680 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7681 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007682 }
7683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7685 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7686 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7687 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7688 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007689 }
7690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007692
7693 /*
7694 * Same message structure as in mbedtls_ssl_write_certificate()
7695 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007696 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 if (ssl->in_msg[i] != 0 ||
7699 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7700 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7701 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7702 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007704 }
7705
7706 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7707 i += 3;
7708
7709 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007711 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 if (i + 3 > ssl->in_hslen) {
7713 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7714 mbedtls_ssl_send_alert_message(ssl,
7715 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7716 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7717 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007718 }
7719 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7720 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 if (ssl->in_msg[i] != 0) {
7722 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7723 mbedtls_ssl_send_alert_message(ssl,
7724 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7725 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7726 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007727 }
7728
7729 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007730 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007731 i += 3;
7732
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 if (n < 128 || i + n > ssl->in_hslen) {
7734 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7735 mbedtls_ssl_send_alert_message(ssl,
7736 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7737 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7738 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007739 }
7740
7741 /* Check if we're handling the first CRT in the chain. */
7742#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007744 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007746 /* During client-side renegotiation, check that the server's
7747 * end-CRTs hasn't changed compared to the initial handshake,
7748 * mitigating the triple handshake attack. On success, reuse
7749 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7751 if (ssl_check_peer_crt_unchanged(ssl,
7752 &ssl->in_msg[i],
7753 n) != 0) {
7754 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7755 mbedtls_ssl_send_alert_message(ssl,
7756 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7757 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7758 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007759 }
7760
7761 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007763 }
7764#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7765
7766 /* Parse the next certificate in the chain. */
7767#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007769#else
7770 /* If we don't need to store the CRT chain permanently, parse
7771 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007773#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007775 case 0: /*ok*/
7776 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7777 /* Ignore certificate with an unknown algorithm: maybe a
7778 prior certificate was already trusted. */
7779 break;
7780
7781 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7782 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7783 goto crt_parse_der_failed;
7784
7785 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7786 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7787 goto crt_parse_der_failed;
7788
7789 default:
7790 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007791crt_parse_der_failed:
7792 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7793 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7794 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007795 }
7796
7797 i += n;
7798 }
7799
Gilles Peskine449bd832023-01-11 14:50:10 +01007800 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7801 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007802}
7803
7804#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007805MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007806static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007807{
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7809 return -1;
7810 }
Jerry Yud9526692022-02-17 14:23:47 +08007811
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007813 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7814 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7816 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7817 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007820}
7821#endif /* MBEDTLS_SSL_SRV_C */
7822
7823/* Check if a certificate message is expected.
7824 * Return either
7825 * - SSL_CERTIFICATE_EXPECTED, or
7826 * - SSL_CERTIFICATE_SKIP
7827 * indicating whether a Certificate message is expected or not.
7828 */
7829#define SSL_CERTIFICATE_EXPECTED 0
7830#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007832static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7833 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007834{
7835 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7836 ssl->handshake->ciphersuite_info;
7837
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7839 return SSL_CERTIFICATE_SKIP;
7840 }
Jerry Yud9526692022-02-17 14:23:47 +08007841
7842#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007845 ssl->session_negotiate->verify_result =
7846 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007848 }
7849 }
7850#else
7851 ((void) authmode);
7852#endif /* MBEDTLS_SSL_SRV_C */
7853
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007855}
7856
Jerry Yud9526692022-02-17 14:23:47 +08007857#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007859static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7860 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007861{
7862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7863 /* Remember digest of the peer's end-CRT. */
7864 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007865 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7866 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7867 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7868 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7869 mbedtls_ssl_send_alert_message(ssl,
7870 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7871 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007872
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007874 }
7875
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 ret = mbedtls_md(mbedtls_md_info_from_type(
7877 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7878 start, len,
7879 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007880
7881 ssl->session_negotiate->peer_cert_digest_type =
7882 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7883 ssl->session_negotiate->peer_cert_digest_len =
7884 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7885
Gilles Peskine449bd832023-01-11 14:50:10 +01007886 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007887}
7888
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007889MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007890static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7891 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007892{
7893 unsigned char *end = start + len;
7894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7895
7896 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7898 ret = mbedtls_pk_parse_subpubkey(&start, end,
7899 &ssl->handshake->peer_pubkey);
7900 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007901 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007903 }
7904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007906}
7907#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7908
Gilles Peskine449bd832023-01-11 14:50:10 +01007909int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007910{
7911 int ret = 0;
7912 int crt_expected;
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +02007913 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007914#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7915 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7916 ? ssl->handshake->sni_authmode
7917 : ssl->conf->authmode;
7918#else
7919 const int authmode = ssl->conf->authmode;
7920#endif
7921 void *rs_ctx = NULL;
7922 mbedtls_x509_crt *chain = NULL;
7923
Gilles Peskine449bd832023-01-11 14:50:10 +01007924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007925
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7927 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7928 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007929 goto exit;
7930 }
7931
7932#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007933 if (ssl->handshake->ecrs_enabled &&
7934 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007935 chain = ssl->handshake->ecrs_peer_cert;
7936 ssl->handshake->ecrs_peer_cert = NULL;
7937 goto crt_verify;
7938 }
7939#endif
7940
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007942 /* mbedtls_ssl_read_record may have sent an alert already. We
7943 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007945 goto exit;
7946 }
7947
7948#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007950 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7951
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007953 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 }
Jerry Yud9526692022-02-17 14:23:47 +08007955
7956 goto exit;
7957 }
7958#endif /* MBEDTLS_SSL_SRV_C */
7959
7960 /* Clear existing peer CRT structure in case we tried to
7961 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007963
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7965 if (chain == NULL) {
7966 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7967 sizeof(mbedtls_x509_crt)));
7968 mbedtls_ssl_send_alert_message(ssl,
7969 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7970 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007971
7972 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7973 goto exit;
7974 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007976
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 ret = ssl_parse_certificate_chain(ssl, chain);
7978 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007979 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 }
Jerry Yud9526692022-02-17 14:23:47 +08007981
7982#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007984 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 }
Jerry Yud9526692022-02-17 14:23:47 +08007986
7987crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007989 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 }
Jerry Yud9526692022-02-17 14:23:47 +08007991#endif
7992
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +02007993 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
7994 ssl->handshake->ciphersuite_info,
7995 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01007996 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007997 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 }
Jerry Yud9526692022-02-17 14:23:47 +08007999
8000#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8001 {
8002 unsigned char *crt_start, *pk_start;
8003 size_t crt_len, pk_len;
8004
8005 /* We parse the CRT chain without copying, so
8006 * these pointers point into the input buffer,
8007 * and are hence still valid after freeing the
8008 * CRT chain. */
8009
8010 crt_start = chain->raw.p;
8011 crt_len = chain->raw.len;
8012
8013 pk_start = chain->pk_raw.p;
8014 pk_len = chain->pk_raw.len;
8015
8016 /* Free the CRT structures before computing
8017 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 mbedtls_x509_crt_free(chain);
8019 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008020 chain = NULL;
8021
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
8023 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008024 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 }
Jerry Yud9526692022-02-17 14:23:47 +08008026
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
8028 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008029 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 }
Jerry Yud9526692022-02-17 14:23:47 +08008031 }
8032#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8033 /* Pass ownership to session structure. */
8034 ssl->session_negotiate->peer_cert = chain;
8035 chain = NULL;
8036#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8037
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08008039
8040exit:
8041
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08008043 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 }
Jerry Yud9526692022-02-17 14:23:47 +08008045
8046#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08008048 ssl->handshake->ecrs_peer_cert = chain;
8049 chain = NULL;
8050 }
8051#endif
8052
Gilles Peskine449bd832023-01-11 14:50:10 +01008053 if (chain != NULL) {
8054 mbedtls_x509_crt_free(chain);
8055 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08008056 }
8057
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08008059}
8060#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8061
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008062static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
8063 unsigned char *padbuf, size_t hlen,
8064 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008065{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008066 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008067 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008068#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008069 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008070 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008071 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008072 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008073#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008075 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008076 mbedtls_md_context_t cloned_ctx;
8077 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008078#endif
8079
8080 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08008082 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01008083 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008084
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08008086 ? "client finished"
8087 : "server finished";
8088
8089#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008090 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008091
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008092 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008094 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008095 }
8096
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008097 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008099 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008100 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008101 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008102#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008103 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008104
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008105 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008106 if (ret != 0) {
8107 goto exit;
8108 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008109 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008110 if (ret != 0) {
8111 goto exit;
8112 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008113
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008114 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008115 if (ret != 0) {
8116 goto exit;
8117 }
8118#endif /* MBEDTLS_USE_PSA_CRYPTO */
8119
8120 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8121
Jerry Yu615bd6f2022-02-17 14:25:15 +08008122 /*
8123 * TLSv1.2:
8124 * hash = PRF( master, finished_label,
8125 * Hash( handshake ) )[0.11]
8126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008128 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008129
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008131
Paul Elliottecb95be2023-08-11 16:41:04 +01008132 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008133
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008134 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008135
8136exit:
8137#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008138 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008139 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008140#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008141 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008142 return ret;
8143#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008144}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008145
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008146#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008147static int ssl_calc_finished_tls_sha256(
8148 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8149{
8150 unsigned char padbuf[32];
8151 return ssl_calc_finished_tls_generic(ssl,
8152#if defined(MBEDTLS_USE_PSA_CRYPTO)
8153 &ssl->handshake->fin_sha256_psa,
8154#else
8155 &ssl->handshake->fin_sha256,
8156#endif
8157 padbuf, sizeof(padbuf),
8158 buf, from);
8159}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008160#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008161
Jerry Yub7ba49e2022-02-17 14:25:53 +08008162
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008163#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008164static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008166{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008167 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008168 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008170 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008171#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008172 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008173#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008174 padbuf, sizeof(padbuf),
8175 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008176}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008177#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008178
Gilles Peskine449bd832023-01-11 14:50:10 +01008179void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008180{
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008182
8183 /*
8184 * Free our handshake params
8185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 mbedtls_ssl_handshake_free(ssl);
8187 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008188 ssl->handshake = NULL;
8189
8190 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008191 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 if (ssl->transform) {
8194 mbedtls_ssl_transform_free(ssl->transform);
8195 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008196 }
8197 ssl->transform = ssl->transform_negotiate;
8198 ssl->transform_negotiate = NULL;
8199
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008201}
8202
Gilles Peskine449bd832023-01-11 14:50:10 +01008203void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008204{
8205 int resume = ssl->handshake->resume;
8206
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008208
8209#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008211 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8212 ssl->renego_records_seen = 0;
8213 }
8214#endif
8215
8216 /*
8217 * Free the previous session and switch in the current one
8218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008220#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8221 /* RFC 7366 3.1: keep the EtM state */
8222 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008224#endif
8225
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 mbedtls_ssl_session_free(ssl->session);
8227 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008228 }
8229 ssl->session = ssl->session_negotiate;
8230 ssl->session_negotiate = NULL;
8231
8232 /*
8233 * Add cache entry
8234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008236 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 resume == 0) {
8238 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8239 ssl->session->id,
8240 ssl->session->id_len,
8241 ssl->session) != 0) {
8242 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8243 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008244 }
8245
8246#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8248 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008249 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008251
8252 /* Keep last flight around in case we need to resend it:
8253 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8255 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008256#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008258
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008259 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008260
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008262}
8263
Gilles Peskine449bd832023-01-11 14:50:10 +01008264int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008265{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008266 int ret;
8267 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008268
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008270
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008272
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008273 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8274 if (ret != 0) {
8275 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8276 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008277
8278 /*
8279 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8280 * may define some other value. Currently (early 2016), no defined
8281 * ciphersuite does this (and this is unlikely to change as activity has
8282 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8283 */
8284 hash_len = 12;
8285
8286#if defined(MBEDTLS_SSL_RENEGOTIATION)
8287 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008289#endif
8290
8291 ssl->out_msglen = 4 + hash_len;
8292 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8293 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8294
8295 /*
8296 * In case of session resuming, invert the client and server
8297 * ChangeCipherSpec messages order.
8298 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008300#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008302 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008304#endif
8305#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008307 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008309#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008311 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008313
8314 /*
8315 * Switch to our negotiated transform and session parameters for outbound
8316 * data.
8317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008319
8320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008322 unsigned char i;
8323
8324 /* Remember current epoch settings for resending */
8325 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8327 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008328
8329 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008331
8332
8333 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 for (i = 2; i > 0; i--) {
8335 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008336 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 }
8338 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008339
8340 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 if (i == 0) {
8342 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8343 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008344 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008348
8349 ssl->transform_out = ssl->transform_negotiate;
8350 ssl->session_out = ssl->session_negotiate;
8351
8352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8354 mbedtls_ssl_send_flight_completed(ssl);
8355 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008356#endif
8357
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8359 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8360 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008361 }
8362
8363#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8365 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8366 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8367 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008368 }
8369#endif
8370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008372
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008374}
8375
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008376#define SSL_MAX_HASH_LEN 12
8377
Gilles Peskine449bd832023-01-11 14:50:10 +01008378int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008379{
8380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8381 unsigned int hash_len = 12;
8382 unsigned char buf[SSL_MAX_HASH_LEN];
8383
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008385
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008386 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8387 if (ret != 0) {
8388 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8389 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008390
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8392 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008393 goto exit;
8394 }
8395
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8397 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8398 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8399 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008400 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8401 goto exit;
8402 }
8403
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8405 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8406 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008407 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8408 goto exit;
8409 }
8410
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8413 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8414 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008415 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8416 goto exit;
8417 }
8418
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8420 buf, hash_len) != 0) {
8421 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8422 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8423 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008424 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8425 goto exit;
8426 }
8427
8428#if defined(MBEDTLS_SSL_RENEGOTIATION)
8429 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008431#endif
8432
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008434#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008436 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008438#endif
8439#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008441 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008443#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008445 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008447
8448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8450 mbedtls_ssl_recv_flight_completed(ssl);
8451 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008452#endif
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008455
8456exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 mbedtls_platform_zeroize(buf, hash_len);
8458 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008459}
8460
Jerry Yu392112c2022-02-17 14:34:10 +08008461#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8462/*
8463 * Helper to get TLS 1.2 PRF from ciphersuite
8464 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008466static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008467{
Jerry Yu392112c2022-02-17 14:34:10 +08008468 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008470#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8472 return tls_prf_sha384;
8473 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008474#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008475#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008476 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8478 return tls_prf_sha256;
8479 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008480 }
8481#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008482#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008483 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008484 (void) ciphersuite_info;
8485#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008486
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008488}
8489#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008490
Gilles Peskine449bd832023-01-11 14:50:10 +01008491static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008492{
8493 ((void) tls_prf);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008494#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 if (tls_prf == tls_prf_sha384) {
8496 return MBEDTLS_SSL_TLS_PRF_SHA384;
8497 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008498#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008499#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 if (tls_prf == tls_prf_sha256) {
8501 return MBEDTLS_SSL_TLS_PRF_SHA256;
8502 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008503#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008505}
8506
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008507/*
8508 * Populate a transform structure with session keys and all the other
8509 * necessary information.
8510 *
8511 * Parameters:
8512 * - [in/out]: transform: structure to populate
8513 * [in] must be just initialised with mbedtls_ssl_transform_init()
8514 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8515 * - [in] ciphersuite
8516 * - [in] master
8517 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008518 * - [in] tls_prf: pointer to PRF to use for key derivation
8519 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008520 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008521 * - [in] endpoint: client or server
8522 * - [in] ssl: used for:
8523 * - ssl->conf->{f,p}_export_keys
8524 * [in] optionally used for:
8525 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8526 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008527MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008528static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8529 int ciphersuite,
8530 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008531#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008533#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 ssl_tls_prf_t tls_prf,
8535 const unsigned char randbytes[64],
8536 mbedtls_ssl_protocol_version tls_version,
8537 unsigned endpoint,
8538 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008539{
8540 int ret = 0;
8541 unsigned char keyblk[256];
8542 unsigned char *key1;
8543 unsigned char *key2;
8544 unsigned char *mac_enc;
8545 unsigned char *mac_dec;
8546 size_t mac_key_len = 0;
8547 size_t iv_copy_len;
8548 size_t keylen;
8549 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008550 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008551#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008552 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008553 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008554#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008555
8556#if defined(MBEDTLS_USE_PSA_CRYPTO)
8557 psa_key_type_t key_type;
8558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8559 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008560 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008561 size_t key_bits;
8562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8563#endif
8564
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008565 /*
8566 * Some data just needs copying into the structure
8567 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008568#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008569 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008570#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008571 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008572
8573#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008575#endif
8576
8577#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008579 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8580 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008582 }
8583#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8584
8585 /*
8586 * Get various info structures
8587 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008588 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8589 if (ciphersuite_info == NULL) {
8590 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8591 ciphersuite));
8592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008593 }
8594
Neil Armstrongab555e02022-04-04 11:07:59 +02008595 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008596#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008598#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008602 transform->taglen =
8603 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008605
8606#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008607 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 transform->taglen,
8609 &alg,
8610 &key_type,
8611 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008612 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008614 goto end;
8615 }
8616#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008617 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 if (cipher_info == NULL) {
8619 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8620 ciphersuite_info->cipher));
8621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008622 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008623#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008624
Neil Armstronge4512952022-03-08 09:08:22 +01008625#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008626 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008628 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 (unsigned) ciphersuite_info->mac));
8630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008631 }
8632#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008633 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if (md_info == NULL) {
8635 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8636 (unsigned) ciphersuite_info->mac));
8637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008638 }
Neil Armstronge4512952022-03-08 09:08:22 +01008639#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008640
8641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8642 /* Copy own and peer's CID if the use of the CID
8643 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8645 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008646
8647 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8649 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8650 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008651
8652 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8654 ssl->handshake->peer_cid_len);
8655 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8656 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008657 }
8658#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8659
8660 /*
8661 * Compute key block using the PRF
8662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8664 if (ret != 0) {
8665 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8666 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008667 }
8668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8670 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8671 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8672 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8673 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008674
8675 /*
8676 * Determine the appropriate key, IV and MAC length.
8677 */
8678
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008679#if defined(MBEDTLS_USE_PSA_CRYPTO)
8680 keylen = PSA_BITS_TO_BYTES(key_bits);
8681#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008682 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008683#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008684
Valerio Settie5707042023-10-11 11:54:42 +02008685#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008687 size_t explicit_ivlen;
8688
8689 transform->maclen = 0;
8690 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008691
8692 /* All modes haves 96-bit IVs, but the length of the static parts vary
8693 * with mode and version:
8694 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8695 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8696 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8697 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8698 * sequence number).
8699 */
8700 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008701
8702 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008705#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8707 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008708#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008709
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008711 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008713 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008714 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008715
8716 /* Minimum length of encrypted record */
8717 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8718 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 } else
Valerio Settie5707042023-10-11 11:54:42 +02008720#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008721#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008723 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008725#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008727#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008728 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008729#endif /* MBEDTLS_USE_PSA_CRYPTO */
8730
8731#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008732 /* Get MAC length */
8733 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8734#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008735 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8737 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8738 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008739 goto end;
8740 }
8741
8742 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008745 transform->maclen = mac_key_len;
8746
8747 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008750#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008751 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008752#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008753
8754 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008756 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008758 /*
8759 * GenericBlockCipher:
8760 * 1. if EtM is in use: one block plus MAC
8761 * otherwise: * first multiple of blocklen greater than maclen
8762 * 2. IV
8763 */
8764#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008766 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 + block_size;
8768 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008769#endif
8770 {
8771 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 + block_size
8773 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008774 }
8775
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008777 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 } else {
8779 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008780 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8781 goto end;
8782 }
8783 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008785#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8786 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8788 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008789 }
8790
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8792 (unsigned) keylen,
8793 (unsigned) transform->minlen,
8794 (unsigned) transform->ivlen,
8795 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008796
8797 /*
8798 * Finally setup the cipher contexts, IVs and MAC secrets.
8799 */
8800#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008802 key1 = keyblk + mac_key_len * 2;
8803 key2 = keyblk + mac_key_len * 2 + keylen;
8804
8805 mac_enc = keyblk;
8806 mac_dec = keyblk + mac_key_len;
8807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 iv_copy_len = (transform->fixed_ivlen) ?
8809 transform->fixed_ivlen : transform->ivlen;
8810 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8811 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8812 iv_copy_len);
8813 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008814#endif /* MBEDTLS_SSL_CLI_C */
8815#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008817 key1 = keyblk + mac_key_len * 2 + keylen;
8818 key2 = keyblk + mac_key_len * 2;
8819
8820 mac_enc = keyblk + mac_key_len;
8821 mac_dec = keyblk;
8822
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 iv_copy_len = (transform->fixed_ivlen) ?
8824 transform->fixed_ivlen : transform->ivlen;
8825 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8826 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8827 iv_copy_len);
8828 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008829#endif /* MBEDTLS_SSL_SRV_C */
8830 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008832 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8833 goto end;
8834 }
8835
Paul Elliott4fb19552023-10-18 12:15:30 +01008836 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 ssl->f_export_keys(ssl->p_export_keys,
8838 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8839 master, 48,
8840 randbytes + 32,
8841 randbytes,
8842 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008843 }
8844
8845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008846 transform->psa_alg = alg;
8847
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8849 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8850 psa_set_key_algorithm(&attributes, alg);
8851 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 if ((status = psa_import_key(&attributes,
8854 key1,
8855 PSA_BITS_TO_BYTES(key_bits),
8856 &transform->psa_key_enc)) != PSA_SUCCESS) {
8857 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008858 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008860 goto end;
8861 }
8862
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008864
Gilles Peskine449bd832023-01-11 14:50:10 +01008865 if ((status = psa_import_key(&attributes,
8866 key2,
8867 PSA_BITS_TO_BYTES(key_bits),
8868 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008869 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008871 goto end;
8872 }
8873 }
8874#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8876 cipher_info)) != 0) {
8877 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008878 goto end;
8879 }
8880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8882 cipher_info)) != 0) {
8883 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008884 goto end;
8885 }
8886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8888 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8889 MBEDTLS_ENCRYPT)) != 0) {
8890 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008891 goto end;
8892 }
8893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8895 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8896 MBEDTLS_DECRYPT)) != 0) {
8897 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008898 goto end;
8899 }
8900
8901#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008902 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8903 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8904 MBEDTLS_PADDING_NONE)) != 0) {
8905 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008906 goto end;
8907 }
8908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8910 MBEDTLS_PADDING_NONE)) != 0) {
8911 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008912 goto end;
8913 }
8914 }
8915#endif /* MBEDTLS_CIPHER_MODE_CBC */
8916#endif /* MBEDTLS_USE_PSA_CRYPTO */
8917
Neil Armstrong29c0c042022-03-17 17:47:28 +01008918#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8919 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8920 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008924
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8926 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8927 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if ((status = psa_import_key(&attributes,
8930 mac_enc, mac_key_len,
8931 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008932 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008933 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008934 goto end;
8935 }
8936
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8938 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008939#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008941#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008943 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008944 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8945 PSA_KEY_USAGE_VERIFY_HASH);
8946 } else {
8947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8948 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 if ((status = psa_import_key(&attributes,
8951 mac_dec, mac_key_len,
8952 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008953 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008955 goto end;
8956 }
8957#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8959 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008960 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008961 }
8962 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8963 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008964 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008966#endif /* MBEDTLS_USE_PSA_CRYPTO */
8967 }
8968#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8969
8970 ((void) mac_dec);
8971 ((void) mac_enc);
8972
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008973end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8975 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008976}
8977
Valerio Settia08b1a42022-11-17 15:10:02 +01008978#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8979 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008980int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 psa_pake_operation_t *pake_ctx,
8982 const unsigned char *buf,
8983 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008984{
8985 psa_status_t status;
8986 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008987 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008988 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8989 * At round two perform a single cycle
8990 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008992
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 for (; remaining_steps > 0; remaining_steps--) {
8994 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008995 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008996 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008997 /* Length is stored at the first byte */
8998 size_t length = buf[input_offset];
8999 input_offset += 1;
9000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01009002 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
9003 }
9004
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 status = psa_pake_input(pake_ctx, step,
9006 buf + input_offset, length);
9007 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009008 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009009 }
9010
9011 input_offset += length;
9012 }
9013 }
9014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01009016 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 }
Valerio Setti30ebe112022-11-17 16:23:34 +01009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009020}
9021
Valerio Setti6b3dab02022-11-17 17:14:54 +01009022int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 psa_pake_operation_t *pake_ctx,
9024 unsigned char *buf,
9025 size_t len, size_t *olen,
9026 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01009027{
9028 psa_status_t status;
9029 size_t output_offset = 0;
9030 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01009031 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01009032 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
9033 * At round two perform a single cycle
9034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009036
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 for (; remaining_steps > 0; remaining_steps--) {
9038 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
9039 step <= PSA_PAKE_STEP_ZK_PROOF;
9040 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009041 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01009042 * For each step, prepend 1 byte with the length of the data as
9043 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045 status = psa_pake_output(pake_ctx, step,
9046 buf + output_offset + 1,
9047 len - output_offset - 1,
9048 &output_len);
9049 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05009050 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01009051 }
9052
Valerio Setti99d88c12022-11-22 16:03:43 +01009053 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01009054
9055 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01009056 }
9057 }
9058
9059 *olen = output_offset;
9060
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01009062}
Valerio Settia08b1a42022-11-17 15:10:02 +01009063#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
9064
Jerry Yuee40f9d2022-02-17 14:55:16 +08009065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009066int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9067 unsigned char *hash, size_t *hashlen,
9068 unsigned char *data, size_t data_len,
9069 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009070{
9071 psa_status_t status;
9072 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02009073 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009076
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 if ((status = psa_hash_setup(&hash_operation,
9078 hash_alg)) != PSA_SUCCESS) {
9079 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009080 goto exit;
9081 }
9082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
9084 64)) != PSA_SUCCESS) {
9085 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009086 goto exit;
9087 }
9088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 if ((status = psa_hash_update(&hash_operation,
9090 data, data_len)) != PSA_SUCCESS) {
9091 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009092 goto exit;
9093 }
9094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9096 hashlen)) != PSA_SUCCESS) {
9097 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9098 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009099 }
9100
9101exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 if (status != PSA_SUCCESS) {
9103 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9104 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9105 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009106 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009108 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9109 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009111 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009113 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009114 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009115 }
9116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009118}
9119
9120#else
9121
Gilles Peskine449bd832023-01-11 14:50:10 +01009122int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9123 unsigned char *hash, size_t *hashlen,
9124 unsigned char *data, size_t data_len,
9125 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009126{
9127 int ret = 0;
9128 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9130 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009131
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009133
Gilles Peskine449bd832023-01-11 14:50:10 +01009134 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009135
9136 /*
9137 * digitally-signed struct {
9138 * opaque client_random[32];
9139 * opaque server_random[32];
9140 * ServerDHParams params;
9141 * };
9142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9144 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009145 goto exit;
9146 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9148 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009149 goto exit;
9150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9152 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009153 goto exit;
9154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9156 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009157 goto exit;
9158 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9160 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009161 goto exit;
9162 }
9163
9164exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009166
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 if (ret != 0) {
9168 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9169 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9170 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009173}
9174#endif /* MBEDTLS_USE_PSA_CRYPTO */
9175
Jerry Yud9d91da2022-02-17 14:57:06 +08009176#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9177
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009178/* Find the preferred hash for a given signature algorithm. */
9179unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 mbedtls_ssl_context *ssl,
9181 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009182{
Gabor Mezei078e8032022-04-27 21:17:56 +02009183 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009184 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009185
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9187 return MBEDTLS_SSL_HASH_NONE;
9188 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009189
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009191 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9193 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009194 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9196 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009197
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009198 mbedtls_md_type_t md_alg =
9199 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9200 if (md_alg == MBEDTLS_MD_NONE) {
9201 continue;
9202 }
9203
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009207 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009208 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009209
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9211 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9212 PSA_ALG_ECDSA(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
Gilles Peskine449bd832023-01-11 14:50:10 +01009217 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9218 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9219 PSA_ALG_RSA_PKCS1V15_SIGN(
9220 psa_hash_alg),
9221 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009222 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009224 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009225#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009228 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009229 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009230
Gilles Peskine449bd832023-01-11 14:50:10 +01009231 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009232}
9233
9234#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9235
Jerry Yudc7bd172022-02-17 13:44:15 +08009236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9237
XiaokangQian75d40ef2022-04-20 11:05:24 +00009238int mbedtls_ssl_validate_ciphersuite(
9239 const mbedtls_ssl_context *ssl,
9240 const mbedtls_ssl_ciphersuite_t *suite_info,
9241 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009243{
9244 (void) ssl;
9245
Gilles Peskine449bd832023-01-11 14:50:10 +01009246 if (suite_info == NULL) {
9247 return -1;
9248 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009249
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if ((suite_info->min_tls_version > max_tls_version) ||
9251 (suite_info->max_tls_version < min_tls_version)) {
9252 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009253 }
9254
XiaokangQian060d8672022-04-21 09:24:56 +00009255#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009256#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009257#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9259 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009260#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9262 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009263#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009264 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009266 }
9267#endif
9268
9269 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9270#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9272 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9273 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009274 }
9275#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9276#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009279}
9280
Ronald Crone68ab4f2022-10-05 12:46:29 +02009281#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009282/*
9283 * Function for writing a signature algorithm extension.
9284 *
9285 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9286 * value (TLS 1.3 RFC8446):
9287 * enum {
9288 * ....
9289 * ecdsa_secp256r1_sha256( 0x0403 ),
9290 * ecdsa_secp384r1_sha384( 0x0503 ),
9291 * ecdsa_secp521r1_sha512( 0x0603 ),
9292 * ....
9293 * } SignatureScheme;
9294 *
9295 * struct {
9296 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9297 * } SignatureSchemeList;
9298 *
9299 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9300 * value (TLS 1.2 RFC5246):
9301 * enum {
9302 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9303 * sha512(6), (255)
9304 * } HashAlgorithm;
9305 *
9306 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9307 * SignatureAlgorithm;
9308 *
9309 * struct {
9310 * HashAlgorithm hash;
9311 * SignatureAlgorithm signature;
9312 * } SignatureAndHashAlgorithm;
9313 *
9314 * SignatureAndHashAlgorithm
9315 * supported_signature_algorithms<2..2^16-2>;
9316 *
9317 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9318 * generalization of the TLS 1.2 signature algorithm extension.
9319 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9320 * `SignatureScheme` field of TLS 1.3
9321 *
9322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009323int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9324 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009325{
9326 unsigned char *p = buf;
9327 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9328 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9329
9330 *out_len = 0;
9331
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009333
9334 /* Check if we have space for header and length field:
9335 * - extension_type (2 bytes)
9336 * - extension_data_length (2 bytes)
9337 * - supported_signature_algorithms_length (2 bytes)
9338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009340 p += 6;
9341
9342 /*
9343 * Write supported_signature_algorithms
9344 */
9345 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9347 if (sig_alg == NULL) {
9348 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9349 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009350
Gilles Peskine449bd832023-01-11 14:50:10 +01009351 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9352 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9353 *sig_alg,
9354 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9355 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009356 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009357 }
9358 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9359 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009360 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9362 *sig_alg,
9363 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009364 }
9365
9366 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009367 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 if (supported_sig_alg_len == 0) {
9369 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9370 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009371 }
9372
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9374 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9375 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009376
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009377 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009378
9379#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009381#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009382
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009384}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009385#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009386
XiaokangQian40a35232022-05-07 09:02:40 +00009387#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009388/*
9389 * mbedtls_ssl_parse_server_name_ext
9390 *
9391 * Structure of server_name extension:
9392 *
9393 * enum {
9394 * host_name(0), (255)
9395 * } NameType;
9396 * opaque HostName<1..2^16-1>;
9397 *
9398 * struct {
9399 * NameType name_type;
9400 * select (name_type) {
9401 * case host_name: HostName;
9402 * } name;
9403 * } ServerName;
9404 * struct {
9405 * ServerName server_name_list<1..2^16-1>
9406 * } ServerNameList;
9407 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009408MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009409int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9410 const unsigned char *buf,
9411 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009412{
9413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9414 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009415 size_t server_name_list_len, hostname_len;
9416 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9421 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009422 p += 2;
9423
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009425 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 while (p < server_name_list_end) {
9427 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9428 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9429 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9430 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009431
Gilles Peskine449bd832023-01-11 14:50:10 +01009432 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009433 /* sni_name is intended to be used only during the parsing of the
9434 * ClientHello message (it is reset to NULL before the end of
9435 * the message parsing). Thus it is ok to just point to the
9436 * reception buffer and not make a copy of it.
9437 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009438 ssl->handshake->sni_name = p + 3;
9439 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 if (ssl->conf->f_sni == NULL) {
9441 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009443 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9444 ssl, p + 3, hostname_len);
9445 if (ret != 0) {
9446 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9447 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9448 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9449 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9450 }
9451 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009452 }
9453
9454 p += hostname_len + 3;
9455 }
9456
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009458}
9459#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9460
XiaokangQianacb39922022-06-17 10:18:48 +00009461#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009463int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9464 const unsigned char *buf,
9465 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009466{
9467 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009468 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009469 const unsigned char *protocol_name_list;
9470 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009471 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009472
9473 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 if (ssl->conf->alpn_list == NULL) {
9475 return 0;
9476 }
XiaokangQianacb39922022-06-17 10:18:48 +00009477
9478 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009479 * RFC7301, section 3.1
9480 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009481 *
XiaokangQianc7403452022-06-23 03:24:12 +00009482 * struct {
9483 * ProtocolName protocol_name_list<2..2^16-1>
9484 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009485 */
9486
XiaokangQianc7403452022-06-23 03:24:12 +00009487 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009488 * protocol_name_list_len 2 bytes
9489 * protocol_name_len 1 bytes
9490 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009491 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009493
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009495 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009497 protocol_name_list = p;
9498 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009499
9500 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009502 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9504 protocol_name_len);
9505 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009506 MBEDTLS_SSL_PEND_FATAL_ALERT(
9507 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9509 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 }
9511
9512 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009513 }
9514
9515 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9517 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009518 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009520 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009521 if (protocol_name_len == alpn_len &&
9522 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009523 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009525 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009526
9527 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009528 }
9529 }
9530
XiaokangQian95d5f542022-06-24 02:29:26 +00009531 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009532 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9534 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9535 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009536}
9537
Gilles Peskine449bd832023-01-11 14:50:10 +01009538int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9539 unsigned char *buf,
9540 unsigned char *end,
9541 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009542{
9543 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009544 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009545 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 if (ssl->alpn_chosen == NULL) {
9548 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009549 }
9550
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 protocol_name_len = strlen(ssl->alpn_chosen);
9552 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009553
Gilles Peskine449bd832023-01-11 14:50:10 +01009554 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009555 /*
9556 * 0 . 1 ext identifier
9557 * 2 . 3 ext length
9558 * 4 . 5 protocol list length
9559 * 6 . 6 protocol name length
9560 * 7 . 7+n protocol name
9561 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009563
XiaokangQian95d5f542022-06-24 02:29:26 +00009564 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9567 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009568 /* Note: the length of the chosen protocol has been checked to be less
9569 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9570 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009571 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009572
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009574
9575#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009576 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009577#endif
9578
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009580}
9581#endif /* MBEDTLS_SSL_ALPN */
9582
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009583#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009584 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009585 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009586 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009587int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9588 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009589{
9590 /* Initialize to suppress unnecessary compiler warning */
9591 size_t hostname_len = 0;
9592
9593 /* Check if new hostname is valid before
9594 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 if (hostname != NULL) {
9596 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009597
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9599 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9600 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009601 }
9602
9603 /* Now it's clear that we will overwrite the old hostname,
9604 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009606 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009608 }
9609
9610 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009612 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 } else {
9614 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9615 if (session->hostname == NULL) {
9616 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9617 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009618
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009620 }
9621
Gilles Peskine449bd832023-01-11 14:50:10 +01009622 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009623}
Xiaokang Qian03409292022-10-12 02:49:52 +00009624#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9625 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009626 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009627 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009628
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009629#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9630 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009631int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9632 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009633{
9634 size_t alpn_len = 0;
9635
9636 if (alpn != NULL) {
9637 alpn_len = strlen(alpn);
9638
9639 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9640 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9641 }
9642 }
9643
9644 if (session->ticket_alpn != NULL) {
9645 mbedtls_zeroize_and_free(session->ticket_alpn,
9646 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009647 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009648 }
9649
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009650 if (alpn != NULL) {
9651 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009652 if (session->ticket_alpn == NULL) {
9653 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9654 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009655 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009656 }
9657
9658 return 0;
9659}
9660#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009661
9662/*
9663 * The following functions are used by 1.2 and 1.3, client and server.
9664 */
9665#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9666int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9667 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9668 int recv_endpoint,
9669 mbedtls_ssl_protocol_version tls_version,
9670 uint32_t *flags)
9671{
9672 int ret = 0;
9673 unsigned int usage = 0;
9674 const char *ext_oid;
9675 size_t ext_len;
9676
9677 /*
9678 * keyUsage
9679 */
9680
9681 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9682 * to check what a compliant client will think while choosing which cert
9683 * to send to the client. */
9684#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9685 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9686 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9687 /* TLS 1.2 server part of the key exchange */
9688 switch (ciphersuite->key_exchange) {
9689 case MBEDTLS_KEY_EXCHANGE_RSA:
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009690 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9691 break;
9692
9693 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9694 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9695 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9696 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9697 break;
9698
9699 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9700 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9701 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9702 break;
9703
9704 /* Don't use default: we want warnings when adding new values */
9705 case MBEDTLS_KEY_EXCHANGE_NONE:
9706 case MBEDTLS_KEY_EXCHANGE_PSK:
9707 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9708 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9709 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9710 usage = 0;
9711 }
9712 } else
9713#endif
9714 {
9715 /* This is either TLS 1.3 authentication, which always uses signatures,
9716 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9717 * options we implement, both using signatures. */
9718 (void) tls_version;
9719 (void) ciphersuite;
9720 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9721 }
9722
9723 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9724 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9725 ret = -1;
9726 }
9727
9728 /*
9729 * extKeyUsage
9730 */
9731
9732 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9733 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9734 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9735 } else {
9736 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9737 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9738 }
9739
9740 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9741 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9742 ret = -1;
9743 }
9744
9745 return ret;
9746}
9747
9748int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9749 int authmode,
9750 mbedtls_x509_crt *chain,
9751 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9752 void *rs_ctx)
9753{
9754 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9755 return 0;
9756 }
9757
9758 /*
9759 * Primary check: use the appropriate X.509 verification function
9760 */
9761 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9762 void *p_vrfy;
9763 if (ssl->f_vrfy != NULL) {
9764 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9765 f_vrfy = ssl->f_vrfy;
9766 p_vrfy = ssl->p_vrfy;
9767 } else {
9768 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9769 f_vrfy = ssl->conf->f_vrfy;
9770 p_vrfy = ssl->conf->p_vrfy;
9771 }
9772
9773 int ret = 0;
9774 int have_ca_chain_or_callback = 0;
9775#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9776 if (ssl->conf->f_ca_cb != NULL) {
9777 ((void) rs_ctx);
9778 have_ca_chain_or_callback = 1;
9779
9780 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9781 ret = mbedtls_x509_crt_verify_with_ca_cb(
9782 chain,
9783 ssl->conf->f_ca_cb,
9784 ssl->conf->p_ca_cb,
9785 ssl->conf->cert_profile,
9786 ssl->hostname,
9787 &ssl->session_negotiate->verify_result,
9788 f_vrfy, p_vrfy);
9789 } else
9790#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9791 {
9792 mbedtls_x509_crt *ca_chain;
9793 mbedtls_x509_crl *ca_crl;
9794#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9795 if (ssl->handshake->sni_ca_chain != NULL) {
9796 ca_chain = ssl->handshake->sni_ca_chain;
9797 ca_crl = ssl->handshake->sni_ca_crl;
9798 } else
9799#endif
9800 {
9801 ca_chain = ssl->conf->ca_chain;
9802 ca_crl = ssl->conf->ca_crl;
9803 }
9804
9805 if (ca_chain != NULL) {
9806 have_ca_chain_or_callback = 1;
9807 }
9808
9809 ret = mbedtls_x509_crt_verify_restartable(
9810 chain,
9811 ca_chain, ca_crl,
9812 ssl->conf->cert_profile,
9813 ssl->hostname,
9814 &ssl->session_negotiate->verify_result,
9815 f_vrfy, p_vrfy, rs_ctx);
9816 }
9817
9818 if (ret != 0) {
9819 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9820 }
9821
9822#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9823 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9824 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9825 }
9826#endif
9827
9828 /*
9829 * Secondary checks: always done, but change 'ret' only if it was 0
9830 */
9831
9832 /* With TLS 1.2 and ECC certs, check that the curve used by the
9833 * certificate is on our list of acceptable curves.
9834 *
9835 * With TLS 1.3 this is not needed because the curve is part of the
9836 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9837 * we validate the signature made with the key associated to this cert.
9838 */
9839#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9840 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
9841 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9842 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9843 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9844 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9845 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9846 if (ret == 0) {
9847 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9848 }
9849 }
9850 }
9851#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9852
9853 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9854 if (mbedtls_ssl_check_cert_usage(chain,
9855 ciphersuite_info,
9856 ssl->conf->endpoint,
9857 ssl->tls_version,
9858 &ssl->session_negotiate->verify_result) != 0) {
9859 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9860 if (ret == 0) {
9861 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9862 }
9863 }
9864
9865 /* With authmode optional, we want to keep going if the certificate was
9866 * unacceptable, but still fail on other errors (out of memory etc),
9867 * including fatal errors from the f_vrfy callback.
9868 *
9869 * The only acceptable errors are:
9870 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9871 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9872 * Anything else is a fatal error. */
9873 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9874 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9875 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9876 ret = 0;
9877 }
9878
9879 /* Return a specific error as this is a user error: inconsistent
9880 * configuration - can't verify without trust anchors. */
9881 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9882 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9883 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9884 }
9885
9886 if (ret != 0) {
9887 uint8_t alert;
9888
9889 /* The certificate may have been rejected for several reasons.
9890 Pick one and send the corresponding alert. Which alert to send
9891 may be a subject of debate in some cases. */
9892 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9893 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9894 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9895 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9896 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9897 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9898 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9899 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9900 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9901 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9902 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9903 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9904 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9905 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9906 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9907 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9908 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9909 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9910 } else {
9911 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9912 }
9913 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9914 alert);
9915 }
9916
9917#if defined(MBEDTLS_DEBUG_C)
9918 if (ssl->session_negotiate->verify_result != 0) {
9919 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9920 (unsigned int) ssl->session_negotiate->verify_result));
9921 } else {
9922 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9923 }
9924#endif /* MBEDTLS_DEBUG_C */
9925
9926 return ret;
9927}
9928#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#endif /* MBEDTLS_SSL_TLS_C */