blob: 9ea58330be5a6e5db1096fd7a7b90f3305280192 [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;
346 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
347 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200349 written_in = ssl->in_msg - ssl->in_buf;
350 iv_offset_in = ssl->in_iv - ssl->in_buf;
351 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200353 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 ssl->in_buf_len < in_buf_new_len) {
355 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
356 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
357 } else {
358 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
359 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200360 modified = 1;
361 }
362 }
363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 written_out = ssl->out_msg - ssl->out_buf;
367 iv_offset_out = ssl->out_iv - ssl->out_buf;
368 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 ssl->out_buf_len < out_buf_new_len) {
372 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
373 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
374 } else {
375 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
376 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200377 modified = 1;
378 }
379 }
380 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200382 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200384 /* Fields below might not be properly updated with record
385 * splitting or with CID, so they are manually updated here. */
386 ssl->out_msg = ssl->out_buf + written_out;
387 ssl->out_len = ssl->out_buf + len_offset_out;
388 ssl->out_iv = ssl->out_buf + iv_offset_out;
389
390 ssl->in_msg = ssl->in_buf + written_in;
391 ssl->in_len = ssl->in_buf + len_offset_in;
392 ssl->in_iv = ssl->in_buf + iv_offset_in;
393 }
394}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500395#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
396
Jerry Yudb8c48a2022-01-27 14:54:54 +0800397#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800398
399#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
401 const char *label,
402 const unsigned char *random, size_t rlen,
403 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800406
407#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
408
409/* Type for the TLS PRF */
410typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
411 const unsigned char *, size_t,
412 unsigned char *, size_t);
413
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200414MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
416 int ciphersuite,
417 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200418#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200420#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 ssl_tls_prf_t tls_prf,
422 const unsigned char randbytes[64],
423 mbedtls_ssl_protocol_version tls_version,
424 unsigned endpoint,
425 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800426
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100427#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300430 const char *label,
431 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100436#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100437
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100438#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439MBEDTLS_CHECK_RETURN_CRITICAL
440static int tls_prf_sha384(const unsigned char *secret, size_t slen,
441 const char *label,
442 const unsigned char *random, size_t rlen,
443 unsigned char *dstbuf, size_t dlen);
444
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100445static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
446static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100447#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449MBEDTLS_CHECK_RETURN_CRITICAL
450static int ssl_tls12_session_load(mbedtls_ssl_session *session,
451 const unsigned char *buf,
452 size_t len);
453#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
454
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100455static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100456
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100457#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100458static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100459#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100460
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100461#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100462static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100463#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100464
465int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
466 const unsigned char *secret, size_t slen,
467 const char *label,
468 const unsigned char *random, size_t rlen,
469 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300470{
471 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100475#if defined(PSA_WANT_ALG_SHA_384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300476 case MBEDTLS_SSL_TLS_PRF_SHA384:
477 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 break;
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100479#endif /* PSA_WANT_ALG_SHA_384*/
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100480#if defined(PSA_WANT_ALG_SHA_256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300481 case MBEDTLS_SSL_TLS_PRF_SHA256:
482 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 break;
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100484#endif /* PSA_WANT_ALG_SHA_256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300485#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 default:
487 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300491}
492
Jerry Yuc73c6182022-02-08 20:29:25 +0800493#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100494static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800495{
496#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (session->peer_cert != NULL) {
498 mbedtls_x509_crt_free(session->peer_cert);
499 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800500 session->peer_cert = NULL;
501 }
502#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800504 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800506 session->peer_cert_digest = NULL;
507 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
508 session->peer_cert_digest_len = 0;
509 }
510#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
511}
512#endif /* MBEDTLS_X509_CRT_PARSE_C */
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800515{
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800517 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800519
520 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800522
523 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800525
526 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800528
529 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800531
532 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800534
535 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800537
538 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800540
541 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800543
544 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800546
547 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800549
550 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800552
553 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800555
556 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800558
559 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800561
562 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800564
565 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800567
568 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800570
571 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800573
574 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800576
577 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800579
580 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800582
583 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800585
586 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800588
589 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800591
592 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800594
Jan Bruckner151f6422023-02-10 12:45:19 +0100595 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
596 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
597
Jerry Yu7a485c12022-10-31 13:08:18 +0800598 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800600
601 }
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800604}
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800607{
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800609}
610
Jerry Yud25cab02022-10-31 12:48:30 +0800611#if defined(MBEDTLS_DEBUG_C)
612static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800613 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800614 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
615 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
616 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
618 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
619 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
620 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
621 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
622 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
623 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
624 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
625 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
626 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
627 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
628 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
629 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
630 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
631 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
632 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
633 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
634 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
635 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
636 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
638 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
639 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100640 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
641 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800642};
643
Chien Wong4e9683e2023-12-28 17:07:43 +0800644static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800645 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
646 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
647 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
648 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
649 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
650 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
651 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
652 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
653 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
654 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
655 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
656 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
657 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
658 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
659 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
660 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
661 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
662 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
663 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
664 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
665 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
666 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
667 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
668 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
669 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
670 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
671 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100672 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
673 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800674};
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800677{
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return extension_name_table[
679 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800683{
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800701}
702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
704 int level, const char *file, int line,
705 int hs_msg_type, unsigned int extension_type,
706 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800707{
708 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800710 mbedtls_debug_print_msg(
711 ssl, level, file, line,
712 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ssl_tls13_get_hs_msg_name(hs_msg_type),
714 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800715 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800717 return;
718 }
719
720 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800722 mbedtls_debug_print_msg(
723 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
725 mbedtls_ssl_get_extension_name(extension_type), extension_type,
726 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800727 return;
728 }
729
730 mbedtls_debug_print_msg(
731 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
733 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800734}
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
737 int level, const char *file, int line,
738 int hs_msg_type, uint32_t extensions_mask,
739 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800740{
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 for (unsigned i = 0;
743 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
744 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800745 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800746 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800748 }
749}
750
Pengyu Lvee455c02023-01-12 14:37:24 +0800751#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800752static const char *ticket_flag_name_table[] =
753{
754 [0] = "ALLOW_PSK_RESUMPTION",
755 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
756 [3] = "ALLOW_EARLY_DATA",
757};
758
Pengyu Lv4938a562023-01-16 11:28:49 +0800759void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
760 int level, const char *file, int line,
761 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800762{
763 size_t i;
764
765 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800766 "print ticket_flags (0x%02x)", flags);
767
768 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800769
770 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800771 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800772 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
773 ticket_flag_name_table[i]);
774 }
775 }
776}
777#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
778
Jerry Yud25cab02022-10-31 12:48:30 +0800779#endif /* MBEDTLS_DEBUG_C */
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
782 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800783{
784 ((void) ciphersuite_info);
785
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100786#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800788 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800790#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100791#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800793 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800795#endif
796 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800798 return;
799 }
800}
801
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100802int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100803 unsigned hs_type,
804 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100805{
806 unsigned char hs_hdr[4];
807
808 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
810 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
811 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
812 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100815}
816
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100817int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100818 unsigned hs_type,
819 unsigned char const *msg,
820 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100821{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100822 int ret;
823 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100824 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100825 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100826 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100827 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100828}
829
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100830int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800831{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100832#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100833 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100834#if defined(MBEDTLS_USE_PSA_CRYPTO)
835 psa_status_t status;
836#else
837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
838#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100839#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800840 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100841#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100842#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100844 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
845 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200846 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100847 }
848 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
849 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200850 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100851 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100853 mbedtls_md_free(&ssl->handshake->fin_sha256);
854 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100855 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
856 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
857 0);
858 if (ret != 0) {
859 return ret;
860 }
861 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100862 if (ret != 0) {
863 return ret;
864 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800865#endif
866#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100867#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800868#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100869 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
870 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200871 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100872 }
873 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
874 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200875 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100878 mbedtls_md_free(&ssl->handshake->fin_sha384);
879 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100880 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
881 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
882 if (ret != 0) {
883 return ret;
884 }
885 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100886 if (ret != 0) {
887 return ret;
888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#endif
890#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100891 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800892}
893
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100894static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100895 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800896{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100897#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100898 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100899#if defined(MBEDTLS_USE_PSA_CRYPTO)
900 psa_status_t status;
901#else
902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
903#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100904#else /* SHA-256 or SHA-384 */
905 ((void) ssl);
906 (void) buf;
907 (void) len;
908#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100909#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
912 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200913 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100914 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800915#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100916 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100917 if (ret != 0) {
918 return ret;
919 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800920#endif
921#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100922#if defined(PSA_WANT_ALG_SHA_384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800923#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100924 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
925 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200926 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100927 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800928#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100929 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100930 if (ret != 0) {
931 return ret;
932 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800933#endif
934#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100935 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800936}
937
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100938#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100939static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100940 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800941{
942#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200943 return mbedtls_md_error_from_psa(psa_hash_update(
944 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100946 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800947#endif
948}
949#endif
950
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100951#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100952static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100953 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800954{
955#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200956 return mbedtls_md_error_from_psa(psa_hash_update(
957 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800958#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100959 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800960#endif
961}
962#endif
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965{
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100968#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500969#if defined(MBEDTLS_USE_PSA_CRYPTO)
970 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500971#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100972 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500974#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100975#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500977 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500978#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100979 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200980#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200982
983 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200987#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200988#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200989 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200991#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200992#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200993#if defined(MBEDTLS_USE_PSA_CRYPTO)
994 handshake->psa_pake_ctx = psa_pake_operation_init();
995 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
996#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200998#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200999#if defined(MBEDTLS_SSL_CLI_C)
1000 handshake->ecjpake_cache = NULL;
1001 handshake->ecjpake_cache_len = 0;
1002#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001003#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001004
Gilles Peskineeccd8882020-03-10 12:19:08 +01001005#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001007#endif
1008
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001009#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1010 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1011#endif
Hanno Becker75173122019-02-06 16:18:31 +00001012
1013#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1014 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001016#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001017}
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001020{
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001022
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001023#if defined(MBEDTLS_USE_PSA_CRYPTO)
1024 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1025 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001026#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1028 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001029#endif
1030
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001031#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001032#if defined(MBEDTLS_USE_PSA_CRYPTO)
1033 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1034 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001035#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 mbedtls_md_init(&transform->md_ctx_enc);
1037 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001038#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001039#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001040}
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001043{
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001045}
1046
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001048static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001049{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1051
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001052 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001053#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ssl->transform_negotiate) {
1055 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1056 }
Jerry Yu2e199812022-12-01 18:57:19 +08001057#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ssl->session_negotiate) {
1059 mbedtls_ssl_session_free(ssl->session_negotiate);
1060 }
1061 if (ssl->handshake) {
1062 mbedtls_ssl_handshake_free(ssl);
1063 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001064
Jerry Yu2e199812022-12-01 18:57:19 +08001065#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001066 /*
1067 * Either the pointers are now NULL or cleared properly and can be freed.
1068 * Now allocate missing structures.
1069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (ssl->transform_negotiate == NULL) {
1071 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001072 }
Jerry Yu2e199812022-12-01 18:57:19 +08001073#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (ssl->session_negotiate == NULL) {
1076 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001077 }
Paul Bakker48916f92012-09-16 19:57:18 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (ssl->handshake == NULL) {
1080 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001081 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001082#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1083 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1086 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001087#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001088
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001089 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001091#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001092 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001093#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 ssl->session_negotiate == NULL) {
1095 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001098 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001099
1100#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001102 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001103#endif
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001106 ssl->session_negotiate = NULL;
1107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001109 }
1110
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001111#if defined(MBEDTLS_SSL_EARLY_DATA)
1112#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001113 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001114#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001115#if defined(MBEDTLS_SSL_SRV_C)
1116 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1117#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001118 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001119#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001120
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001121 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 mbedtls_ssl_session_init(ssl->session_negotiate);
1123 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001124
Jerry Yu2e199812022-12-01 18:57:19 +08001125#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001127#endif
1128
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001129 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001130 ret = mbedtls_ssl_reset_checksum(ssl);
1131 if (ret != 0) {
1132 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1133 return ret;
1134 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001135
Jerry Yud0766ec2022-09-22 10:46:57 +08001136#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1137 defined(MBEDTLS_SSL_SRV_C) && \
1138 defined(MBEDTLS_SSL_SESSION_TICKETS)
1139 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001141#endif
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001145 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001148 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001150 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001154 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001155#endif
1156
Ronald Crone68ab4f2022-10-05 12:46:29 +02001157#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001158#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001160 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1161 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1163 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 const int *md;
1165 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001166 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001167 uint16_t *p;
1168
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001169 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1170 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1171 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1174 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001175 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001177#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001179#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001180
Jerry Yub476a442022-01-21 18:14:45 +08001181#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001183#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1185 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1186 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001187 }
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1190 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1191 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1194 sizeof(uint16_t));
1195 if (ssl->handshake->sig_algs == NULL) {
1196 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1197 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 p = (uint16_t *) ssl->handshake->sig_algs;
1200 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1201 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1202 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001203 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001205#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001207 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001208#endif
1209#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001211 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001212#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001213 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001214 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001215 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001217#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001218 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001219 ssl->handshake->sig_algs_heap_allocated = 0;
1220 }
Jerry Yucc539102022-06-27 16:27:35 +08001221#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001222#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001224}
1225
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001226#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001227/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001228MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001229static int ssl_cookie_write_dummy(void *ctx,
1230 unsigned char **p, unsigned char *end,
1231 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001232{
1233 ((void) ctx);
1234 ((void) p);
1235 ((void) end);
1236 ((void) cli_id);
1237 ((void) cli_id_len);
1238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001240}
1241
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001243static int ssl_cookie_check_dummy(void *ctx,
1244 const unsigned char *cookie, size_t cookie_len,
1245 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001246{
1247 ((void) ctx);
1248 ((void) cookie);
1249 ((void) cookie_len);
1250 ((void) cli_id);
1251 ((void) cli_id_len);
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001254}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001255#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001256
Paul Bakker5121ce52009-01-03 21:22:43 +00001257/*
1258 * Initialize an SSL context
1259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001261{
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001263}
1264
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001265MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001266static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001267{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001268 const mbedtls_ssl_config *conf = ssl->conf;
1269
Ronald Cron6f135e12021-12-08 16:57:54 +01001270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1272 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1273 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1274 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001275 }
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1278 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001279 }
1280#endif
1281
1282#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1284 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1285 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001286 }
1287#endif
1288
Ronald Cron6f135e12021-12-08 16:57:54 +01001289#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1291 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1292 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1293 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1297 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001298 }
1299#endif
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1302 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001303}
1304
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001305MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001306static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1307{
1308 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 ret = ssl_conf_version_check(ssl);
1310 if (ret != 0) {
1311 return ret;
1312 }
Jerry Yu60835a82021-08-04 10:13:52 +08001313
Yanray Wangb422cab2023-12-01 16:18:10 +08001314 if (ssl->conf->f_rng == NULL) {
1315 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1316 return MBEDTLS_ERR_SSL_NO_RNG;
1317 }
1318
Jerry Yu60835a82021-08-04 10:13:52 +08001319 /* Space for further checks */
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001322}
1323
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001324/*
1325 * Setup an SSL context
1326 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1329 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001330{
Janos Follath865b3eb2019-12-16 11:46:15 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001332 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1333 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001335 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if ((ret = ssl_conf_check(ssl)) != 0) {
1338 return ret;
1339 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001340 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001341
Paul Bakker62f2dee2012-09-28 07:31:51 +00001342 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001343 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001344 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001345
1346 /* Set to NULL in case of an error condition */
1347 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001348
Darryl Greenb33cc762019-11-28 14:29:44 +00001349#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1350 ssl->in_buf_len = in_buf_len;
1351#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1353 if (ssl->in_buf == NULL) {
1354 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001355 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001356 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001357 }
1358
Darryl Greenb33cc762019-11-28 14:29:44 +00001359#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1360 ssl->out_buf_len = out_buf_len;
1361#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1363 if (ssl->out_buf == NULL) {
1364 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001365 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001366 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001370
Johan Pascalb62bb512015-12-03 21:56:45 +01001371#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001373#endif
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001376 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001380
1381error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 mbedtls_free(ssl->in_buf);
1383 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001384
1385 ssl->conf = NULL;
1386
Darryl Greenb33cc762019-11-28 14:29:44 +00001387#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1388 ssl->in_buf_len = 0;
1389 ssl->out_buf_len = 0;
1390#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001391 ssl->in_buf = NULL;
1392 ssl->out_buf = NULL;
1393
1394 ssl->in_hdr = NULL;
1395 ssl->in_ctr = NULL;
1396 ssl->in_len = NULL;
1397 ssl->in_iv = NULL;
1398 ssl->in_msg = NULL;
1399
1400 ssl->out_hdr = NULL;
1401 ssl->out_ctr = NULL;
1402 ssl->out_len = NULL;
1403 ssl->out_iv = NULL;
1404 ssl->out_msg = NULL;
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407}
1408
1409/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001410 * Reset an initialized and used SSL context for re-use while retaining
1411 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001412 *
1413 * If partial is non-zero, keep data in the input buffer and client ID.
1414 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1417 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001418{
Darryl Greenb33cc762019-11-28 14:29:44 +00001419#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1420 size_t in_buf_len = ssl->in_buf_len;
1421 size_t out_buf_len = ssl->out_buf_len;
1422#else
1423 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1424 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1425#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001426
Hanno Beckerb0302c42021-08-03 09:39:42 +01001427#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1428 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001429#endif
1430
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001431 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001435
1436 /* Reset incoming message parsing */
1437 ssl->in_offt = NULL;
1438 ssl->nb_zero = 0;
1439 ssl->in_msgtype = 0;
1440 ssl->in_msglen = 0;
1441 ssl->in_hslen = 0;
1442 ssl->keep_current_message = 0;
1443 ssl->transform_in = NULL;
1444
1445#if defined(MBEDTLS_SSL_PROTO_DTLS)
1446 ssl->next_record_offset = 0;
1447 ssl->in_epoch = 0;
1448#endif
1449
1450 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001452 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001454 }
1455
Ronald Cronad8c17b2022-06-10 17:18:09 +02001456 ssl->send_alert = 0;
1457
Hanno Beckerb0302c42021-08-03 09:39:42 +01001458 /* Reset outgoing message writing */
1459 ssl->out_msgtype = 0;
1460 ssl->out_msglen = 0;
1461 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 memset(ssl->out_buf, 0, out_buf_len);
1463 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001464 ssl->transform_out = NULL;
1465
1466#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001468#endif
1469
XiaokangQian2b01dc32022-01-21 02:53:13 +00001470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ssl->transform) {
1472 mbedtls_ssl_transform_free(ssl->transform);
1473 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001474 ssl->transform = NULL;
1475 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001476#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1477
XiaokangQian2b01dc32022-01-21 02:53:13 +00001478#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 mbedtls_ssl_transform_free(ssl->transform_application);
1480 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001481 ssl->transform_application = NULL;
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001484#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1486 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001487 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001488#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1491 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001492 ssl->handshake->transform_handshake = NULL;
1493 }
1494
XiaokangQian2b01dc32022-01-21 02:53:13 +00001495#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001496}
1497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001499{
1500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1501
1502 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001503 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001506
1507 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_RENEGOTIATION)
1509 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001510 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001511
1512 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1514 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001515#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001517
Hanno Beckerb0302c42021-08-03 09:39:42 +01001518 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001519 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (ssl->session) {
1521 mbedtls_ssl_session_free(ssl->session);
1522 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001523 ssl->session = NULL;
1524 }
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001527 ssl->alpn_chosen = NULL;
1528#endif
1529
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001530#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001531 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001532#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001534#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (free_cli_id) {
1536 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001537 ssl->cli_id = NULL;
1538 ssl->cli_id_len = 0;
1539 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001540#endif
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if ((ret = ssl_handshake_init(ssl)) != 0) {
1543 return ret;
1544 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001547}
1548
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001549/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001550 * Reset an initialized and used SSL context for re-use while retaining
1551 * all application-set variables, function pointers and data.
1552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001554{
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001556}
1557
1558/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 * SSL set accessors
1560 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001561void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001563 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001564}
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001568 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001569}
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001573{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001574 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001575}
1576#endif
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001580 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001581}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1586 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001587{
1588 ssl->disable_datagram_packing = !allow_packing;
1589}
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1592 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001593{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001594 conf->hs_timeout_min = min;
1595 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001596}
1597#endif
1598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001601 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602}
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1606 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1607 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001609 conf->f_vrfy = f_vrfy;
1610 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001611}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1615 int (*f_rng)(void *, unsigned char *, size_t),
1616 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001617{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001618 conf->f_rng = f_rng;
1619 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620}
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1623 void (*f_dbg)(void *, int, const char *, int, const char *),
1624 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001626 conf->f_dbg = f_dbg;
1627 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001628}
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1631 void *p_bio,
1632 mbedtls_ssl_send_t *f_send,
1633 mbedtls_ssl_recv_t *f_recv,
1634 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001635{
1636 ssl->p_bio = p_bio;
1637 ssl->f_send = f_send;
1638 ssl->f_recv = f_recv;
1639 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001640}
1641
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001642#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001643void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001644{
1645 ssl->mtu = mtu;
1646}
1647#endif
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001650{
1651 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001652}
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1655 void *p_timer,
1656 mbedtls_ssl_set_timer_t *f_set_timer,
1657 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001658{
1659 ssl->p_timer = p_timer;
1660 ssl->f_set_timer = f_set_timer;
1661 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001662
1663 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001665}
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001668void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1669 void *p_cache,
1670 mbedtls_ssl_cache_get_t *f_get_cache,
1671 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001673 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001674 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001675 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001680int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001681{
Janos Follath865b3eb2019-12-16 11:46:15 +00001682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001685 session == NULL ||
1686 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1688 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 if (ssl->handshake->resume == 1) {
1692 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1693 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001694
Jerry Yu21092062022-10-10 21:21:31 +08001695#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron233fcaa2024-04-04 14:05:21 +02001697#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001698 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001702 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1704 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1705 session->ciphersuite));
1706 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001707 }
Ronald Cron233fcaa2024-04-04 14:05:21 +02001708#else
1709 /*
1710 * If session tickets are not enabled, it is not possible to resume a
1711 * TLS 1.3 session, thus do not make any change to the SSL context in
1712 * the first place.
1713 */
1714 return 0;
1715#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001716 }
Jerry Yu21092062022-10-10 21:21:31 +08001717#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1720 session)) != 0) {
1721 return ret;
1722 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001723
Paul Bakker0a597072012-09-25 21:55:46 +00001724 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1731 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001732{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001733 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734}
1735
Ronald Cron6f135e12021-12-08 16:57:54 +01001736#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1738 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001739{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001740 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001741}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001742
1743#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001744void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1745 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001746{
1747 conf->early_data_enabled = early_data_enabled;
1748}
Jerry Yucc4e0072022-11-22 17:22:22 +08001749
1750#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001751void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001753{
Jerry Yu39da9852022-12-06 16:58:36 +08001754 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001755}
1756#endif /* MBEDTLS_SSL_SRV_C */
1757
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001758#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001759#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001762void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1763 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001764{
1765 conf->cert_profile = profile;
1766}
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001769{
1770 mbedtls_ssl_key_cert *cur = key_cert, *next;
1771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001773 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001775 cur = next;
1776 }
1777}
1778
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001779/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001780MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001781static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1782 mbedtls_x509_crt *cert,
1783 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001784{
niisato8ee24222018-06-25 19:05:48 +09001785 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001788 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001790 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001792 }
1793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1795 if (new_cert == NULL) {
1796 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1797 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001798
niisato8ee24222018-06-25 19:05:48 +09001799 new_cert->cert = cert;
1800 new_cert->key = key;
1801 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001802
Glenn Strauss36872db2022-01-22 05:06:31 -05001803 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001805 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001807 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001809 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 }
niisato8ee24222018-06-25 19:05:48 +09001811 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001812 }
1813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001815}
1816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001818 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001820{
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001822}
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001825 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001827{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001828 conf->ca_chain = ca_chain;
1829 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001830
1831#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1832 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1833 * cannot be used together. */
1834 conf->f_ca_cb = NULL;
1835 conf->p_ca_cb = NULL;
1836#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001837}
Hanno Becker5adaad92019-03-27 16:54:37 +00001838
1839#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001840void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1841 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1842 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001843{
1844 conf->f_ca_cb = f_ca_cb;
1845 conf->p_ca_cb = p_ca_cb;
1846
1847 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1848 * cannot be used together. */
1849 conf->ca_chain = NULL;
1850 conf->ca_crl = NULL;
1851}
1852#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001854
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001855#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001856const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1857 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001858{
1859 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001861}
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1864 mbedtls_x509_crt *own_cert,
1865 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001866{
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1868 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001869}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1872 mbedtls_x509_crt *ca_chain,
1873 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001874{
1875 ssl->handshake->sni_ca_chain = ca_chain;
1876 ssl->handshake->sni_ca_crl = ca_crl;
1877}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001878
Glenn Strauss999ef702022-03-11 01:37:23 -05001879#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001880void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1881 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001882{
1883 ssl->handshake->dn_hints = crt;
1884}
1885#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1888 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001889{
1890 ssl->handshake->sni_authmode = authmode;
1891}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001892#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1893
Hanno Becker8927c832019-04-03 12:52:50 +01001894#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001895void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1896 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1897 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001898{
1899 ssl->f_vrfy = f_vrfy;
1900 ssl->p_vrfy = p_vrfy;
1901}
1902#endif
1903
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001904#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001905
Valerio Setti016f6822022-12-09 14:17:50 +01001906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001907static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1908static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1909
Valerio Setti016f6822022-12-09 14:17:50 +01001910static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 mbedtls_ssl_context *ssl,
1912 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001913{
1914 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001915 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001916 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001917 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001918 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001919 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1921 psa_pake_cs_set_primitive(&cipher_suite,
1922 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1923 PSA_ECC_FAMILY_SECP_R1,
1924 256));
1925 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1928 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001929 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001933 user = jpake_server_id;
1934 user_len = sizeof(jpake_server_id);
1935 peer = jpake_client_id;
1936 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001938 user = jpake_client_id;
1939 user_len = sizeof(jpake_client_id);
1940 peer = jpake_server_id;
1941 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001943
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001944 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1945 if (status != PSA_SUCCESS) {
1946 return status;
1947 }
1948
1949 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001951 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Valerio Setti016f6822022-12-09 14:17:50 +01001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1955 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001956 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Valerio Setti016f6822022-12-09 14:17:50 +01001958
1959 ssl->handshake->psa_pake_ctx_is_ok = 1;
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001962}
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1965 const unsigned char *pw,
1966 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001967{
1968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1969 psa_status_t status;
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (ssl->handshake == NULL || ssl->conf == NULL) {
1972 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001973 }
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 /* Empty password is not valid */
1976 if ((pw == NULL) || (pw_len == 0)) {
1977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1978 }
1979
1980 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1981 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1982 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1983
1984 status = psa_import_key(&attributes, pw, pw_len,
1985 &ssl->handshake->psa_pake_password);
1986 if (status != PSA_SUCCESS) {
1987 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1988 }
1989
1990 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1991 ssl->handshake->psa_pake_password);
1992 if (status != PSA_SUCCESS) {
1993 psa_destroy_key(ssl->handshake->psa_pake_password);
1994 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1995 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1996 }
1997
1998 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001999}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2002 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002003{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002004 psa_status_t status;
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (ssl->handshake == NULL || ssl->conf == NULL) {
2007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002008 }
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (mbedtls_svc_key_id_is_null(pwd)) {
2011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2012 }
2013
2014 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2015 if (status != PSA_SUCCESS) {
2016 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2017 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2018 }
2019
2020 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002021}
2022#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2024 const unsigned char *pw,
2025 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002026{
2027 mbedtls_ecjpake_role role;
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ssl->handshake == NULL || ssl->conf == NULL) {
2030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2031 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002032
Valerio Settic689ed82022-12-07 14:40:38 +01002033 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if ((pw == NULL) || (pw_len == 0)) {
2035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2036 }
Valerio Settic689ed82022-12-07 14:40:38 +01002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002039 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002041 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2045 role,
2046 MBEDTLS_MD_SHA256,
2047 MBEDTLS_ECP_DP_SECP256R1,
2048 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002049}
Valerio Setti02c25b52022-11-15 14:08:42 +01002050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002051#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002052
Ronald Cron73fe8df2022-10-05 14:31:43 +02002053#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002054int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002055{
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (conf->psk_identity == NULL ||
2057 conf->psk_identity_len == 0) {
2058 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002059 }
2060
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002061#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2063 return 1;
2064 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (conf->psk != NULL && conf->psk_len != 0) {
2068 return 1;
2069 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002072}
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002075{
2076 /* Remove reference to existing PSK, if any. */
2077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002079 /* The maintenance of the PSK key slot is the
2080 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002081 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002082 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002083#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002085 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002086 conf->psk = NULL;
2087 conf->psk_len = 0;
2088 }
2089
2090 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (conf->psk_identity != NULL) {
2092 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002093 conf->psk_identity = NULL;
2094 conf->psk_identity_len = 0;
2095 }
2096}
2097
Hanno Becker7390c712018-11-15 13:33:04 +00002098/* This function assumes that PSK identity in the SSL config is unset.
2099 * It checks that the provided identity is well-formed and attempts
2100 * to make a copy of it in the SSL config.
2101 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002102MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002103static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2104 unsigned char const *psk_identity,
2105 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002106{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002107 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002109 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 (psk_identity_len >> 16) != 0 ||
2111 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2112 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002113 }
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2116 if (conf->psk_identity == NULL) {
2117 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2118 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002119
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002120 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002124}
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2127 const unsigned char *psk, size_t psk_len,
2128 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002129{
Janos Follath865b3eb2019-12-16 11:46:15 +00002130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002131
2132 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2134 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2135 }
Hanno Becker7390c712018-11-15 13:33:04 +00002136
2137 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (psk == NULL) {
2139 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2140 }
2141 if (psk_len == 0) {
2142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2143 }
2144 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2145 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2146 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2149 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2150 }
Hanno Becker7390c712018-11-15 13:33:04 +00002151 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002153
2154 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2156 if (ret != 0) {
2157 ssl_conf_remove_psk(conf);
2158 }
Hanno Becker7390c712018-11-15 13:33:04 +00002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002161}
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002164{
2165#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002167 /* The maintenance of the external PSK key slot is the
2168 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (ssl->handshake->psk_opaque_is_internal) {
2170 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002171 ssl->handshake->psk_opaque_is_internal = 0;
2172 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002173 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002174 }
Neil Armstronge952a302022-05-03 10:22:14 +02002175#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002177 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002179 ssl->handshake->psk_len = 0;
lhuang0454adeab2024-06-10 12:16:29 -07002180 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002181 }
Neil Armstronge952a302022-05-03 10:22:14 +02002182#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002183}
2184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2186 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002187{
Neil Armstrong501c9322022-05-03 09:35:09 +02002188#if defined(MBEDTLS_USE_PSA_CRYPTO)
2189 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002191 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002192 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002193#endif /* MBEDTLS_USE_PSA_CRYPTO */
2194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (psk == NULL || ssl->handshake == NULL) {
2196 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2197 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2200 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2201 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002204
Neil Armstrong501c9322022-05-03 09:35:09 +02002205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002206#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2208 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2209 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2210 } else {
2211 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2212 }
2213 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002214 }
2215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002216
Jerry Yu568ec252022-07-22 21:27:34 +08002217#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2219 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2220 psa_set_key_usage_flags(&key_attributes,
2221 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002222 }
2223#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 psa_set_key_algorithm(&key_attributes, alg);
2226 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2229 if (status != PSA_SUCCESS) {
2230 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2231 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002232
2233 /* Allow calling psa_destroy_key() on psk remove */
2234 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002236#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2238 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2239 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002240
2241 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002245#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002246}
2247
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2250 mbedtls_svc_key_id_t psk,
2251 const unsigned char *psk_identity,
2252 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002253{
Janos Follath865b3eb2019-12-16 11:46:15 +00002254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002255
2256 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2258 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2259 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002260
Hanno Becker7390c712018-11-15 13:33:04 +00002261 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (mbedtls_svc_key_id_is_null(psk)) {
2263 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2264 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002265 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002266
2267 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2269 psk_identity_len);
2270 if (ret != 0) {
2271 ssl_conf_remove_psk(conf);
2272 }
Hanno Becker7390c712018-11-15 13:33:04 +00002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002275}
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2278 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002279{
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if ((mbedtls_svc_key_id_is_null(psk)) ||
2281 (ssl->handshake == NULL)) {
2282 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2283 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002286 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002288}
2289#endif /* MBEDTLS_USE_PSA_CRYPTO */
2290
Jerry Yu8897c072022-08-12 13:56:53 +08002291#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002292void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2293 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2294 size_t),
2295 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002296{
2297 conf->f_psk = f_psk;
2298 conf->p_psk = p_psk;
2299}
Jerry Yu8897c072022-08-12 13:56:53 +08002300#endif /* MBEDTLS_SSL_SRV_C */
2301
Ronald Cron73fe8df2022-10-05 14:31:43 +02002302#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002303
2304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002305static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002307{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002308#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (alg == PSA_ALG_CBC_NO_PADDING) {
2310 return MBEDTLS_SSL_MODE_CBC;
2311 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002312#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (PSA_ALG_IS_AEAD(alg)) {
2314 return MBEDTLS_SSL_MODE_AEAD;
2315 }
2316 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002317}
2318
2319#else /* MBEDTLS_USE_PSA_CRYPTO */
2320
2321static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002323{
2324#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (mode == MBEDTLS_MODE_CBC) {
2326 return MBEDTLS_SSL_MODE_CBC;
2327 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002328#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2329
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002330#if defined(MBEDTLS_GCM_C) || \
2331 defined(MBEDTLS_CCM_C) || \
2332 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002334 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 mode == MBEDTLS_MODE_CHACHAPOLY) {
2336 return MBEDTLS_SSL_MODE_AEAD;
2337 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002338#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002341}
Gilles Peskine301711e2022-04-26 16:57:05 +02002342#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002343
Gilles Peskinee108d982022-04-26 16:50:40 +02002344static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2345 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002347{
2348#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2350 base_mode == MBEDTLS_SSL_MODE_CBC) {
2351 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002352 }
2353#else
2354 (void) encrypt_then_mac;
2355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002357}
2358
Neil Armstrongab555e02022-04-04 11:07:59 +02002359mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002361{
Gilles Peskinee108d982022-04-26 16:50:40 +02002362 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002365#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002367#endif
2368 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002369
Gilles Peskinee108d982022-04-26 16:50:40 +02002370 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002371#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002372 encrypt_then_mac = transform->encrypt_then_mac;
2373#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002375}
2376
Neil Armstrongab555e02022-04-04 11:07:59 +02002377mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002378#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002380#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002382{
Gilles Peskinee108d982022-04-26 16:50:40 +02002383 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2384
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002385#if defined(MBEDTLS_USE_PSA_CRYPTO)
2386 psa_status_t status;
2387 psa_algorithm_t alg;
2388 psa_key_type_t type;
2389 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002390 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2391 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (status == PSA_SUCCESS) {
2393 base_mode = mbedtls_ssl_get_base_mode(alg);
2394 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002395#else
2396 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002397 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002399 base_mode =
2400 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002402 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002403#endif /* MBEDTLS_USE_PSA_CRYPTO */
2404
Gilles Peskinee108d982022-04-26 16:50:40 +02002405#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2406 int encrypt_then_mac = 0;
2407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002409}
2410
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002411#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002412
Ronald Cron8b592d22024-11-12 18:08:25 +01002413const mbedtls_error_pair_t psa_to_ssl_errors[] =
2414{
2415 { PSA_SUCCESS, 0 },
2416 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
2417 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
2418 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
2419 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
2420 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
2421 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
2422};
2423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2425 size_t taglen,
2426 psa_algorithm_t *alg,
2427 psa_key_type_t *key_type,
2428 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002429{
Elena Uziunaitec2561722024-07-05 11:37:33 +01002430#if !defined(PSA_WANT_ALG_CCM)
Yanray Wang19e4dc82023-11-16 18:02:05 +08002431 (void) taglen;
2432#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 switch (mbedtls_cipher_type) {
Elena Uziunaite74342c72024-07-05 11:31:29 +01002434#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002435 case MBEDTLS_CIPHER_AES_128_CBC:
2436 *alg = PSA_ALG_CBC_NO_PADDING;
2437 *key_type = PSA_KEY_TYPE_AES;
2438 *key_size = 128;
2439 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002440#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002441#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002442 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002444 *key_type = PSA_KEY_TYPE_AES;
2445 *key_size = 128;
2446 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002447#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002448#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002449 case MBEDTLS_CIPHER_AES_128_GCM:
2450 *alg = PSA_ALG_GCM;
2451 *key_type = PSA_KEY_TYPE_AES;
2452 *key_size = 128;
2453 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002454#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002455#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002456 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002458 *key_type = PSA_KEY_TYPE_AES;
2459 *key_size = 192;
2460 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002461#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002462#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002463 case MBEDTLS_CIPHER_AES_192_GCM:
2464 *alg = PSA_ALG_GCM;
2465 *key_type = PSA_KEY_TYPE_AES;
2466 *key_size = 192;
2467 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002468#endif
Elena Uziunaite74342c72024-07-05 11:31:29 +01002469#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002470 case MBEDTLS_CIPHER_AES_256_CBC:
2471 *alg = PSA_ALG_CBC_NO_PADDING;
2472 *key_type = PSA_KEY_TYPE_AES;
2473 *key_size = 256;
2474 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002475#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002476#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002477 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002479 *key_type = PSA_KEY_TYPE_AES;
2480 *key_size = 256;
2481 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002482#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002483#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002484 case MBEDTLS_CIPHER_AES_256_GCM:
2485 *alg = PSA_ALG_GCM;
2486 *key_type = PSA_KEY_TYPE_AES;
2487 *key_size = 256;
2488 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002489#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002490#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002491 case MBEDTLS_CIPHER_ARIA_128_CBC:
2492 *alg = PSA_ALG_CBC_NO_PADDING;
2493 *key_type = PSA_KEY_TYPE_ARIA;
2494 *key_size = 128;
2495 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002496#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002497#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002498 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002500 *key_type = PSA_KEY_TYPE_ARIA;
2501 *key_size = 128;
2502 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002503#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002504#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002505 case MBEDTLS_CIPHER_ARIA_128_GCM:
2506 *alg = PSA_ALG_GCM;
2507 *key_type = PSA_KEY_TYPE_ARIA;
2508 *key_size = 128;
2509 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002510#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002511#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002512 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002514 *key_type = PSA_KEY_TYPE_ARIA;
2515 *key_size = 192;
2516 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002517#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002518#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002519 case MBEDTLS_CIPHER_ARIA_192_GCM:
2520 *alg = PSA_ALG_GCM;
2521 *key_type = PSA_KEY_TYPE_ARIA;
2522 *key_size = 192;
2523 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002524#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002525#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002526 case MBEDTLS_CIPHER_ARIA_256_CBC:
2527 *alg = PSA_ALG_CBC_NO_PADDING;
2528 *key_type = PSA_KEY_TYPE_ARIA;
2529 *key_size = 256;
2530 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002531#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002532#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002533 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002535 *key_type = PSA_KEY_TYPE_ARIA;
2536 *key_size = 256;
2537 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002538#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002539#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002540 case MBEDTLS_CIPHER_ARIA_256_GCM:
2541 *alg = PSA_ALG_GCM;
2542 *key_type = PSA_KEY_TYPE_ARIA;
2543 *key_size = 256;
2544 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002545#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002546#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002547 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2548 *alg = PSA_ALG_CBC_NO_PADDING;
2549 *key_type = PSA_KEY_TYPE_CAMELLIA;
2550 *key_size = 128;
2551 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002552#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002553#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002554 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002556 *key_type = PSA_KEY_TYPE_CAMELLIA;
2557 *key_size = 128;
2558 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002559#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002560#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002561 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2562 *alg = PSA_ALG_GCM;
2563 *key_type = PSA_KEY_TYPE_CAMELLIA;
2564 *key_size = 128;
2565 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002566#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002567#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002568 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002570 *key_type = PSA_KEY_TYPE_CAMELLIA;
2571 *key_size = 192;
2572 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002573#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002574#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002575 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2576 *alg = PSA_ALG_GCM;
2577 *key_type = PSA_KEY_TYPE_CAMELLIA;
2578 *key_size = 192;
2579 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002580#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002581#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002582 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2583 *alg = PSA_ALG_CBC_NO_PADDING;
2584 *key_type = PSA_KEY_TYPE_CAMELLIA;
2585 *key_size = 256;
2586 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002587#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002588#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002589 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002591 *key_type = PSA_KEY_TYPE_CAMELLIA;
2592 *key_size = 256;
2593 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002594#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002595#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002596 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2597 *alg = PSA_ALG_GCM;
2598 *key_type = PSA_KEY_TYPE_CAMELLIA;
2599 *key_size = 256;
2600 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002601#endif
Elena Uziunaite5c70c302024-07-05 11:44:44 +01002602#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002603 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2604 *alg = PSA_ALG_CHACHA20_POLY1305;
2605 *key_type = PSA_KEY_TYPE_CHACHA20;
2606 *key_size = 256;
2607 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002608#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002609 case MBEDTLS_CIPHER_NULL:
2610 *alg = MBEDTLS_SSL_NULL_CIPHER;
2611 *key_type = 0;
2612 *key_size = 0;
2613 break;
2614 default:
2615 return PSA_ERROR_NOT_SUPPORTED;
2616 }
2617
2618 return PSA_SUCCESS;
2619}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002620#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002621
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002622#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002623int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2624 const unsigned char *dhm_P, size_t P_len,
2625 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002626{
Janos Follath865b3eb2019-12-16 11:46:15 +00002627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 mbedtls_mpi_free(&conf->dhm_P);
2630 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2633 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2634 mbedtls_mpi_free(&conf->dhm_P);
2635 mbedtls_mpi_free(&conf->dhm_G);
2636 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002637 }
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002640}
Paul Bakker5121ce52009-01-03 21:22:43 +00002641
Gilles Peskine449bd832023-01-11 14:50:10 +01002642int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002643{
Janos Follath865b3eb2019-12-16 11:46:15 +00002644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 mbedtls_mpi_free(&conf->dhm_P);
2647 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2650 &conf->dhm_P)) != 0 ||
2651 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2652 &conf->dhm_G)) != 0) {
2653 mbedtls_mpi_free(&conf->dhm_P);
2654 mbedtls_mpi_free(&conf->dhm_G);
2655 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002656 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002659}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002660#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002661
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002662#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2663/*
2664 * Set the minimum length for Diffie-Hellman parameters
2665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002666void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2667 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002668{
2669 conf->dhm_min_bitlen = bitlen;
2670}
2671#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2672
Ronald Crone68ab4f2022-10-05 12:46:29 +02002673#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002674#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002675/*
2676 * Set allowed/preferred hashes for handshake signatures
2677 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002678void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2679 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002680{
2681 conf->sig_hashes = hashes;
2682}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002683#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002684
Jerry Yuf017ee42022-01-12 15:49:48 +08002685/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2687 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002688{
Jerry Yuf017ee42022-01-12 15:49:48 +08002689#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2690 conf->sig_hashes = NULL;
2691#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2692 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002693}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002694#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002695
Brett Warrene0edc842021-08-17 09:53:13 +01002696/*
2697 * Set the allowed groups
2698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2700 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002701{
Brett Warrene0edc842021-08-17 09:53:13 +01002702 conf->group_list = group_list;
2703}
2704
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002705#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002706int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002707{
Hanno Becker947194e2017-04-07 13:25:49 +01002708 /* Initialize to suppress unnecessary compiler warning */
2709 size_t hostname_len = 0;
2710
2711 /* Check if new hostname is valid before
2712 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (hostname != NULL) {
2714 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2718 }
Hanno Becker947194e2017-04-07 13:25:49 +01002719 }
2720
2721 /* Now it's clear that we will overwrite the old hostname,
2722 * so we can free it safely */
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002725 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002726 }
2727
2728 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002731 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 } else {
2733 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2734 if (ssl->hostname == NULL) {
2735 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2736 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002739
Hanno Becker947194e2017-04-07 13:25:49 +01002740 ssl->hostname[hostname_len] = '\0';
2741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002745#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002746
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002747#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002748void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2749 int (*f_sni)(void *, mbedtls_ssl_context *,
2750 const unsigned char *, size_t),
2751 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002752{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002753 conf->f_sni = f_sni;
2754 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002755}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002759int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002760{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002761 size_t cur_len, tot_len;
2762 const char **p;
2763
2764 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002765 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2766 * MUST NOT be truncated."
2767 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002768 */
2769 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 for (p = protos; *p != NULL; p++) {
2771 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002772 tot_len += cur_len;
2773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if ((cur_len == 0) ||
2775 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2776 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2777 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2778 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002779 }
2780
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002781 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002784}
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002787{
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002791
Johan Pascalb62bb512015-12-03 21:56:45 +01002792#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002793void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2794 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002795{
2796 conf->dtls_srtp_mki_support = support_mki_value;
2797}
2798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2800 unsigned char *mki_value,
2801 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002802{
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2804 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002805 }
Ron Eldor591f1622018-01-22 12:30:04 +02002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2808 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002809 }
Ron Eldor591f1622018-01-22 12:30:04 +02002810
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002812 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002814}
2815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2817 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002818{
Johan Pascal253d0262020-09-22 13:04:45 +02002819 const mbedtls_ssl_srtp_profile *p;
2820 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002821
Johan Pascal253d0262020-09-22 13:04:45 +02002822 /* check the profiles list: all entry must be valid,
2823 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2825 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2826 p++) {
2827 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002828 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002830 /* unsupported value, stop parsing and set the size to an error value */
2831 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002832 }
2833 }
2834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2836 conf->dtls_srtp_profile_list = NULL;
2837 conf->dtls_srtp_profile_list_len = 0;
2838 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002839 }
2840
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002841 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002842 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002845}
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2848 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002849{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002850 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2851 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002853 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002855 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2857 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002858 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002859}
Johan Pascalb62bb512015-12-03 21:56:45 +01002860#endif /* MBEDTLS_SSL_DTLS_SRTP */
2861
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302862#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002863void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002864{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002865 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002866}
2867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002869{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002870 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002871}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302872#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002873
Janos Follath088ce432017-04-10 12:42:31 +01002874#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002875void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2876 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002877{
2878 conf->cert_req_ca_list = cert_req_ca_list;
2879}
2880#endif
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002883void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002884{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002885 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002886}
2887#endif
2888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002890void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002891{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002892 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002893}
2894#endif
2895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002897int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002898{
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2900 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2901 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002902 }
2903
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002904 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002912 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002913}
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002916void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002917{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002918 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002919}
2920
Gilles Peskine449bd832023-01-11 14:50:10 +01002921void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002922{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002923 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002924}
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2927 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002928{
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002934#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002935void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002936{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002937 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002938}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002939#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002940
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002941#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002942
Jerry Yud0766ec2022-09-22 10:46:57 +08002943#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002944void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
2945 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002946{
Jerry Yud0766ec2022-09-22 10:46:57 +08002947 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002948}
2949#endif
2950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
2952 mbedtls_ssl_ticket_write_t *f_ticket_write,
2953 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2954 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02002955{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002956 conf->f_ticket_write = f_ticket_write;
2957 conf->f_ticket_parse = f_ticket_parse;
2958 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002959}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002962
Gilles Peskine449bd832023-01-11 14:50:10 +01002963void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
2964 mbedtls_ssl_export_keys_t *f_export_keys,
2965 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002966{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002967 ssl->f_export_keys = f_export_keys;
2968 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002969}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002970
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002971#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002972void mbedtls_ssl_conf_async_private_cb(
2973 mbedtls_ssl_config *conf,
2974 mbedtls_ssl_async_sign_t *f_async_sign,
2975 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2976 mbedtls_ssl_async_resume_t *f_async_resume,
2977 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002979{
2980 conf->f_async_sign_start = f_async_sign;
2981 conf->f_async_decrypt_start = f_async_decrypt;
2982 conf->f_async_resume = f_async_resume;
2983 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002984 conf->p_async_config_data = async_config_data;
2985}
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02002988{
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02002990}
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002993{
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (ssl->handshake == NULL) {
2995 return NULL;
2996 } else {
2997 return ssl->handshake->user_async_ctx;
2998 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002999}
3000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3002 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003003{
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003005 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003007}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003008#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
3011 * SSL get accessors
3012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003013uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003014{
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (ssl->session != NULL) {
3016 return ssl->session->verify_result;
3017 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 if (ssl->session_negotiate != NULL) {
3020 return ssl->session_negotiate->verify_result;
3021 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003024}
3025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003027{
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 if (ssl == NULL || ssl->session == NULL) {
3029 return 0;
3030 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003033}
3034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003036{
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (ssl == NULL || ssl->session == NULL) {
3038 return NULL;
3039 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003042}
3043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3048 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003049 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003051 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003053 }
3054 }
3055#endif
3056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003058 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003060 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003062 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003064 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003065}
3066
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003067#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3068
3069size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
3070{
3071 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3072 size_t record_size_limit = max_len;
3073
3074 if (ssl->session != NULL &&
3075 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3076 ssl->session->record_size_limit < max_len) {
3077 record_size_limit = ssl->session->record_size_limit;
3078 }
3079
3080 // TODO: this is currently untested
3081 /* During a handshake, use the value being negotiated */
3082 if (ssl->session_negotiate != NULL &&
3083 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
3084 ssl->session_negotiate->record_size_limit < max_len) {
3085 record_size_limit = ssl->session_negotiate->record_size_limit;
3086 }
3087
3088 return record_size_limit;
3089}
3090#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3091
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003092#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003093size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003094{
David Horstmann95d516f2021-05-04 18:36:56 +01003095 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003096 size_t read_mfl;
3097
Jerry Yuddda0502022-12-01 19:43:12 +08003098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003099 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3101 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3102 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003103 }
Jerry Yuddda0502022-12-01 19:43:12 +08003104#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003105
3106 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (ssl->session_out != NULL) {
3108 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3109 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003110 max_len = read_mfl;
3111 }
3112 }
3113
Jerry Yuddda0502022-12-01 19:43:12 +08003114 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 if (ssl->session_negotiate != NULL) {
3116 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3117 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003118 max_len = read_mfl;
3119 }
3120 }
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003123}
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003126{
3127 size_t max_len;
3128
3129 /*
3130 * Assume mfl_code is correct since it was checked when set
3131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003133
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003134 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (ssl->session_out != NULL &&
3136 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3137 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003138 }
3139
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003140 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (ssl->session_negotiate != NULL &&
3142 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3143 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003144 }
3145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003147}
3148#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3149
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003151size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003152{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003153 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3155 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3156 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3157 return 0;
3158 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3161 return ssl->mtu;
3162 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (ssl->mtu == 0) {
3165 return ssl->handshake->mtu;
3166 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 return ssl->mtu < ssl->handshake->mtu ?
3169 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003170}
3171#endif /* MBEDTLS_SSL_PROTO_DTLS */
3172
Gilles Peskine449bd832023-01-11 14:50:10 +01003173int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003174{
3175 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3176
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003177#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003178 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003179 !defined(MBEDTLS_SSL_PROTO_DTLS)
3180 (void) ssl;
3181#endif
3182
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003183#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003187 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003189#endif
3190
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003191#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3192 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3193
3194 if (max_len > record_size_limit) {
3195 max_len = record_size_limit;
3196 }
3197#endif
3198
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003199 if (ssl->transform_out != NULL &&
3200 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003201 /*
3202 * In TLS 1.3 case, when records are protected, `max_len` as computed
3203 * above is the maximum length of the TLSInnerPlaintext structure that
3204 * along the plaintext payload contains the inner content type (one byte)
3205 * and some zero padding. Given the algorithm used for padding
3206 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3207 * the plaintext payload. Round down to a multiple of
3208 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3209 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003210 */
3211 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3212 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3213 }
3214
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003215#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3217 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3218 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003219 const size_t overhead = (size_t) ret;
3220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if (ret < 0) {
3222 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003223 }
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 if (mtu <= overhead) {
3226 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3227 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3228 }
3229
3230 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003231 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003233 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003235
Hanno Becker0defedb2018-08-10 12:35:02 +01003236#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003237 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3238 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003239 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003240#endif
3241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003243}
3244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003246{
3247 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3248
3249#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3250 (void) ssl;
3251#endif
3252
3253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003257 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003259#endif
3260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003262}
3263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003265const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003266{
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 if (ssl == NULL || ssl->session == NULL) {
3268 return NULL;
3269 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003270
Hanno Beckere6824572019-02-07 13:18:46 +00003271#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003273#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003280int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3281 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003282{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003283 int ret;
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003286 dst == NULL ||
3287 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3289 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003290 }
3291
Hanno Beckere810bbc2021-05-14 16:01:05 +01003292 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3293 * idempotent: Each session can only be exported once.
3294 *
3295 * (This is in preparation for TLS 1.3 support where we will
3296 * need the ability to export multiple sessions (aka tickets),
3297 * which will be achieved by calling mbedtls_ssl_get_session()
3298 * multiple times until it fails.)
3299 *
3300 * Check whether we have already exported the current session,
3301 * and fail if so.
3302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (ssl->session->exported == 1) {
3304 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3305 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3308 if (ret != 0) {
3309 return ret;
3310 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003311
3312 /* Remember that we've exported the session. */
3313 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003317
David Horstmanncb01b362024-02-29 17:31:46 +00003318#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3319
3320/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003321 *
David Horstmanncb01b362024-02-29 17:31:46 +00003322 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003323 */
3324static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3325 unsigned char *buf,
3326 size_t buf_len)
3327{
3328 unsigned char *p = buf;
3329 size_t used = 0;
3330
3331#if defined(MBEDTLS_HAVE_TIME)
3332 uint64_t start;
3333#endif
3334#if defined(MBEDTLS_X509_CRT_PARSE_C)
3335#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3336 size_t cert_len;
3337#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3338#endif /* MBEDTLS_X509_CRT_PARSE_C */
3339
3340 /*
3341 * Time
3342 */
3343#if defined(MBEDTLS_HAVE_TIME)
3344 used += 8;
3345
3346 if (used <= buf_len) {
3347 start = (uint64_t) session->start;
3348
3349 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3350 p += 8;
3351 }
3352#endif /* MBEDTLS_HAVE_TIME */
3353
3354 /*
3355 * Basic mandatory fields
3356 */
3357 used += 1 /* id_len */
3358 + sizeof(session->id)
3359 + sizeof(session->master)
3360 + 4; /* verify_result */
3361
3362 if (used <= buf_len) {
3363 *p++ = MBEDTLS_BYTE_0(session->id_len);
3364 memcpy(p, session->id, 32);
3365 p += 32;
3366
3367 memcpy(p, session->master, 48);
3368 p += 48;
3369
3370 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3371 p += 4;
3372 }
3373
3374 /*
3375 * Peer's end-entity certificate
3376 */
3377#if defined(MBEDTLS_X509_CRT_PARSE_C)
3378#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3379 if (session->peer_cert == NULL) {
3380 cert_len = 0;
3381 } else {
3382 cert_len = session->peer_cert->raw.len;
3383 }
3384
3385 used += 3 + cert_len;
3386
3387 if (used <= buf_len) {
3388 *p++ = MBEDTLS_BYTE_2(cert_len);
3389 *p++ = MBEDTLS_BYTE_1(cert_len);
3390 *p++ = MBEDTLS_BYTE_0(cert_len);
3391
3392 if (session->peer_cert != NULL) {
3393 memcpy(p, session->peer_cert->raw.p, cert_len);
3394 p += cert_len;
3395 }
3396 }
3397#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3398 if (session->peer_cert_digest != NULL) {
3399 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3400 if (used <= buf_len) {
3401 *p++ = (unsigned char) session->peer_cert_digest_type;
3402 *p++ = (unsigned char) session->peer_cert_digest_len;
3403 memcpy(p, session->peer_cert_digest,
3404 session->peer_cert_digest_len);
3405 p += session->peer_cert_digest_len;
3406 }
3407 } else {
3408 used += 2;
3409 if (used <= buf_len) {
3410 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3411 *p++ = 0;
3412 }
3413 }
3414#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3415#endif /* MBEDTLS_X509_CRT_PARSE_C */
3416
3417 /*
3418 * Session ticket if any, plus associated data
3419 */
3420#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3421#if defined(MBEDTLS_SSL_CLI_C)
3422 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3423 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3424
3425 if (used <= buf_len) {
3426 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3427 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3428 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3429
3430 if (session->ticket != NULL) {
3431 memcpy(p, session->ticket, session->ticket_len);
3432 p += session->ticket_len;
3433 }
3434
3435 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3436 p += 4;
3437 }
3438 }
3439#endif /* MBEDTLS_SSL_CLI_C */
3440#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3441 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3442 used += 8;
3443
3444 if (used <= buf_len) {
3445 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3446 p += 8;
3447 }
3448 }
3449#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3450#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3451
3452 /*
3453 * Misc extension-related info
3454 */
3455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3456 used += 1;
3457
3458 if (used <= buf_len) {
3459 *p++ = session->mfl_code;
3460 }
3461#endif
3462
3463#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3464 used += 1;
3465
3466 if (used <= buf_len) {
3467 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3468 }
3469#endif
3470
3471 return used;
3472}
3473
3474MBEDTLS_CHECK_RETURN_CRITICAL
3475static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3476 const unsigned char *buf,
3477 size_t len)
3478{
3479#if defined(MBEDTLS_HAVE_TIME)
3480 uint64_t start;
3481#endif
3482#if defined(MBEDTLS_X509_CRT_PARSE_C)
3483#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3484 size_t cert_len;
3485#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3486#endif /* MBEDTLS_X509_CRT_PARSE_C */
3487
3488 const unsigned char *p = buf;
3489 const unsigned char * const end = buf + len;
3490
3491 /*
3492 * Time
3493 */
3494#if defined(MBEDTLS_HAVE_TIME)
3495 if (8 > (size_t) (end - p)) {
3496 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3497 }
3498
3499 start = MBEDTLS_GET_UINT64_BE(p, 0);
3500 p += 8;
3501
3502 session->start = (time_t) start;
3503#endif /* MBEDTLS_HAVE_TIME */
3504
3505 /*
3506 * Basic mandatory fields
3507 */
3508 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3510 }
3511
3512 session->id_len = *p++;
3513 memcpy(session->id, p, 32);
3514 p += 32;
3515
3516 memcpy(session->master, p, 48);
3517 p += 48;
3518
3519 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3520 p += 4;
3521
3522 /* Immediately clear invalid pointer values that have been read, in case
3523 * we exit early before we replaced them with valid ones. */
3524#if defined(MBEDTLS_X509_CRT_PARSE_C)
3525#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3526 session->peer_cert = NULL;
3527#else
3528 session->peer_cert_digest = NULL;
3529#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3530#endif /* MBEDTLS_X509_CRT_PARSE_C */
3531#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3532 session->ticket = NULL;
3533#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3534
3535 /*
3536 * Peer certificate
3537 */
3538#if defined(MBEDTLS_X509_CRT_PARSE_C)
3539#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3540 /* Deserialize CRT from the end of the ticket. */
3541 if (3 > (size_t) (end - p)) {
3542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3543 }
3544
3545 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3546 p += 3;
3547
3548 if (cert_len != 0) {
3549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3550
3551 if (cert_len > (size_t) (end - p)) {
3552 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3553 }
3554
3555 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3556
3557 if (session->peer_cert == NULL) {
3558 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3559 }
3560
3561 mbedtls_x509_crt_init(session->peer_cert);
3562
3563 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3564 p, cert_len)) != 0) {
3565 mbedtls_x509_crt_free(session->peer_cert);
3566 mbedtls_free(session->peer_cert);
3567 session->peer_cert = NULL;
3568 return ret;
3569 }
3570
3571 p += cert_len;
3572 }
3573#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3574 /* Deserialize CRT digest from the end of the ticket. */
3575 if (2 > (size_t) (end - p)) {
3576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3577 }
3578
3579 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3580 session->peer_cert_digest_len = (size_t) *p++;
3581
3582 if (session->peer_cert_digest_len != 0) {
3583 const mbedtls_md_info_t *md_info =
3584 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3585 if (md_info == NULL) {
3586 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3587 }
3588 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3590 }
3591
3592 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3594 }
3595
3596 session->peer_cert_digest =
3597 mbedtls_calloc(1, session->peer_cert_digest_len);
3598 if (session->peer_cert_digest == NULL) {
3599 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3600 }
3601
3602 memcpy(session->peer_cert_digest, p,
3603 session->peer_cert_digest_len);
3604 p += session->peer_cert_digest_len;
3605 }
3606#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3607#endif /* MBEDTLS_X509_CRT_PARSE_C */
3608
3609 /*
3610 * Session ticket and associated data
3611 */
3612#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3613#if defined(MBEDTLS_SSL_CLI_C)
3614 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3615 if (3 > (size_t) (end - p)) {
3616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3617 }
3618
3619 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3620 p += 3;
3621
3622 if (session->ticket_len != 0) {
3623 if (session->ticket_len > (size_t) (end - p)) {
3624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3625 }
3626
3627 session->ticket = mbedtls_calloc(1, session->ticket_len);
3628 if (session->ticket == NULL) {
3629 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3630 }
3631
3632 memcpy(session->ticket, p, session->ticket_len);
3633 p += session->ticket_len;
3634 }
3635
3636 if (4 > (size_t) (end - p)) {
3637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3638 }
3639
3640 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3641 p += 4;
3642 }
3643#endif /* MBEDTLS_SSL_CLI_C */
3644#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3645 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3646 if (8 > (size_t) (end - p)) {
3647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3648 }
3649 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3650 p += 8;
3651 }
3652#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3653#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3654
3655 /*
3656 * Misc extension-related info
3657 */
3658#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3659 if (1 > (size_t) (end - p)) {
3660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3661 }
3662
3663 session->mfl_code = *p++;
3664#endif
3665
3666#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3667 if (1 > (size_t) (end - p)) {
3668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3669 }
3670
3671 session->encrypt_then_mac = *p++;
3672#endif
3673
3674 /* Done, should have consumed entire buffer */
3675 if (p != end) {
3676 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3677 }
3678
3679 return 0;
3680}
3681
3682#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3683
3684#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3685/* Serialization of TLS 1.3 sessions:
3686 *
David Horstmanncb01b362024-02-29 17:31:46 +00003687 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003688 */
3689#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3690MBEDTLS_CHECK_RETURN_CRITICAL
3691static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3692 unsigned char *buf,
3693 size_t buf_len,
3694 size_t *olen)
3695{
3696 unsigned char *p = buf;
3697#if defined(MBEDTLS_SSL_CLI_C) && \
3698 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3699 size_t hostname_len = (session->hostname == NULL) ?
3700 0 : strlen(session->hostname) + 1;
3701#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003702
3703#if defined(MBEDTLS_SSL_SRV_C) && \
3704 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003705 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003706 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003707#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003708 size_t needed = 4 /* ticket_age_add */
3709 + 1 /* ticket_flags */
3710 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003711
David Horstmanne59f9702024-02-29 16:08:57 +00003712 *olen = 0;
3713
3714 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3715 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3716 }
3717 needed += session->resumption_key_len; /* resumption_key */
3718
3719#if defined(MBEDTLS_SSL_EARLY_DATA)
3720 needed += 4; /* max_early_data_size */
3721#endif
3722#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3723 needed += 2; /* record_size_limit */
3724#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3725
3726#if defined(MBEDTLS_HAVE_TIME)
3727 needed += 8; /* ticket_creation_time or ticket_reception_time */
3728#endif
3729
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003730#if defined(MBEDTLS_SSL_SRV_C)
3731 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3732#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003733 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003734 + alpn_len; /* alpn */
3735#endif
3736 }
3737#endif /* MBEDTLS_SSL_SRV_C */
3738
David Horstmanne59f9702024-02-29 16:08:57 +00003739#if defined(MBEDTLS_SSL_CLI_C)
3740 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3741#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3742 needed += 2 /* hostname_len */
3743 + hostname_len; /* hostname */
3744#endif
3745
3746 needed += 4 /* ticket_lifetime */
3747 + 2; /* ticket_len */
3748
3749 /* Check size_t overflow */
3750 if (session->ticket_len > SIZE_MAX - needed) {
3751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3752 }
3753
3754 needed += session->ticket_len; /* ticket */
3755 }
3756#endif /* MBEDTLS_SSL_CLI_C */
3757
3758 *olen = needed;
3759 if (needed > buf_len) {
3760 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3761 }
3762
3763 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3764 p[4] = session->ticket_flags;
3765
3766 /* save resumption_key */
3767 p[5] = session->resumption_key_len;
3768 p += 6;
3769 memcpy(p, session->resumption_key, session->resumption_key_len);
3770 p += session->resumption_key_len;
3771
3772#if defined(MBEDTLS_SSL_EARLY_DATA)
3773 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3774 p += 4;
3775#endif
3776#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3777 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3778 p += 2;
3779#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3780
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003781#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003782 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003783#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003784 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3785 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003786#endif /* MBEDTLS_HAVE_TIME */
3787
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003788#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003789 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3790 p += 2;
3791
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003792 if (alpn_len > 0) {
3793 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003794 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003795 p += alpn_len;
3796 }
3797#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3798 }
3799#endif /* MBEDTLS_SSL_SRV_C */
3800
David Horstmanne59f9702024-02-29 16:08:57 +00003801#if defined(MBEDTLS_SSL_CLI_C)
3802 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3803#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3804 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3805 p += 2;
3806 if (hostname_len > 0) {
3807 /* save host name */
3808 memcpy(p, session->hostname, hostname_len);
3809 p += hostname_len;
3810 }
3811#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3812
3813#if defined(MBEDTLS_HAVE_TIME)
3814 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3815 p += 8;
3816#endif
3817 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3818 p += 4;
3819
3820 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3821 p += 2;
3822
3823 if (session->ticket != NULL && session->ticket_len > 0) {
3824 memcpy(p, session->ticket, session->ticket_len);
3825 p += session->ticket_len;
3826 }
3827 }
3828#endif /* MBEDTLS_SSL_CLI_C */
3829 return 0;
3830}
3831
3832MBEDTLS_CHECK_RETURN_CRITICAL
3833static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3834 const unsigned char *buf,
3835 size_t len)
3836{
3837 const unsigned char *p = buf;
3838 const unsigned char *end = buf + len;
3839
3840 if (end - p < 6) {
3841 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3842 }
3843 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3844 session->ticket_flags = p[4];
3845
3846 /* load resumption_key */
3847 session->resumption_key_len = p[5];
3848 p += 6;
3849
3850 if (end - p < session->resumption_key_len) {
3851 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3852 }
3853
3854 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3855 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3856 }
3857 memcpy(session->resumption_key, p, session->resumption_key_len);
3858 p += session->resumption_key_len;
3859
3860#if defined(MBEDTLS_SSL_EARLY_DATA)
3861 if (end - p < 4) {
3862 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3863 }
3864 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3865 p += 4;
3866#endif
3867#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3868 if (end - p < 2) {
3869 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3870 }
3871 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3872 p += 2;
3873#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3874
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003875#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003876 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003877#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003878 if (end - p < 8) {
3879 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3880 }
3881 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3882 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003883#endif /* MBEDTLS_HAVE_TIME */
3884
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003885#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003886 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003887
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003888 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3890 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003891
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003892 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3893 p += 2;
3894
3895 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003896 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3897 }
3898
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003899 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003900 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3901 if (ret != 0) {
3902 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003903 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003904 p += alpn_len;
3905 }
3906#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3907 }
3908#endif /* MBEDTLS_SSL_SRV_C */
3909
David Horstmanne59f9702024-02-29 16:08:57 +00003910#if defined(MBEDTLS_SSL_CLI_C)
3911 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3912#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3913 size_t hostname_len;
3914 /* load host name */
3915 if (end - p < 2) {
3916 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3917 }
3918 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3919 p += 2;
3920
3921 if (end - p < (long int) hostname_len) {
3922 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3923 }
3924 if (hostname_len > 0) {
3925 session->hostname = mbedtls_calloc(1, hostname_len);
3926 if (session->hostname == NULL) {
3927 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3928 }
3929 memcpy(session->hostname, p, hostname_len);
3930 p += hostname_len;
3931 }
3932#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3933
3934#if defined(MBEDTLS_HAVE_TIME)
3935 if (end - p < 8) {
3936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3937 }
3938 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
3939 p += 8;
3940#endif
3941 if (end - p < 4) {
3942 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3943 }
3944 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3945 p += 4;
3946
3947 if (end - p < 2) {
3948 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3949 }
3950 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
3951 p += 2;
3952
3953 if (end - p < (long int) session->ticket_len) {
3954 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3955 }
3956 if (session->ticket_len > 0) {
3957 session->ticket = mbedtls_calloc(1, session->ticket_len);
3958 if (session->ticket == NULL) {
3959 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3960 }
3961 memcpy(session->ticket, p, session->ticket_len);
3962 p += session->ticket_len;
3963 }
3964 }
3965#endif /* MBEDTLS_SSL_CLI_C */
3966
3967 return 0;
3968
3969}
3970#else /* MBEDTLS_SSL_SESSION_TICKETS */
3971MBEDTLS_CHECK_RETURN_CRITICAL
3972static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3973 unsigned char *buf,
3974 size_t buf_len,
3975 size_t *olen)
3976{
3977 ((void) session);
3978 ((void) buf);
3979 ((void) buf_len);
3980 *olen = 0;
3981 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3982}
3983
3984static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritiusd36913a2023-01-24 17:48:29 +01003985 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00003986 size_t buf_len)
3987{
3988 ((void) session);
3989 ((void) buf);
3990 ((void) buf_len);
3991 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3992}
3993#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
3994#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3995
Paul Bakker5121ce52009-01-03 21:22:43 +00003996/*
Hanno Beckera835da52019-05-16 12:39:07 +01003997 * Define ticket header determining Mbed TLS version
3998 * and structure of the ticket.
3999 */
4000
Hanno Becker94ef3b32019-05-16 12:50:45 +01004001/*
Hanno Becker50b59662019-05-28 14:30:45 +01004002 * Define bitflag determining compile-time settings influencing
4003 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01004004 */
4005
Hanno Becker50b59662019-05-28 14:30:45 +01004006#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01004007#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01004008#else
Hanno Becker3e088662019-05-29 11:10:18 +01004009#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004010#endif /* MBEDTLS_HAVE_TIME */
4011
4012#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01004013#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004014#else
Hanno Becker3e088662019-05-29 11:10:18 +01004015#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004016#endif /* MBEDTLS_X509_CRT_PARSE_C */
4017
David Horstmann5c5a32f2024-02-13 17:53:35 +00004018#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00004019#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00004020#else
David Horstmannf686f1d2024-03-01 11:20:32 +00004021#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00004022#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4023
Hanno Becker94ef3b32019-05-16 12:50:45 +01004024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01004025#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004026#else
Hanno Becker3e088662019-05-29 11:10:18 +01004027#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004028#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
4029
4030#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01004031#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004032#else
Hanno Becker3e088662019-05-29 11:10:18 +01004033#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004034#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
4035
Hanno Becker94ef3b32019-05-16 12:50:45 +01004036#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01004037#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01004038#else
Hanno Becker3e088662019-05-29 11:10:18 +01004039#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01004040#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
4041
Hanno Becker94ef3b32019-05-16 12:50:45 +01004042#if defined(MBEDTLS_SSL_SESSION_TICKETS)
4043#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
4044#else
4045#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
4046#endif /* MBEDTLS_SSL_SESSION_TICKETS */
4047
David Horstmann92b258b2024-02-13 17:23:34 +00004048#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4049#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
4050#else
4051#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
4052#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4053
4054#if defined(MBEDTLS_SSL_EARLY_DATA)
4055#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
4056#else
4057#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
4058#endif /* MBEDTLS_SSL_EARLY_DATA */
4059
4060#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4061#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
4062#else
4063#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
4064#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
4065
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004066#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
4067 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00004068#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
4069#else
4070#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
4071#endif /* MBEDTLS_SSL_ALPN */
4072
Hanno Becker3e088662019-05-29 11:10:18 +01004073#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
4074#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
4075#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
4076#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01004077#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
4078#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00004079#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00004080#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
4081#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
4082#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00004083#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01004084
Hanno Becker50b59662019-05-28 14:30:45 +01004085#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 ((uint16_t) ( \
4087 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
4088 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
4089 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
4090 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
4091 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
4092 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00004093 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00004094 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
4095 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00004096 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
4097 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
4098 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
4099 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004100 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00004101 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00004102 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01004103
Chien Wong4e9683e2023-12-28 17:07:43 +08004104static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01004105 MBEDTLS_VERSION_MAJOR,
4106 MBEDTLS_VERSION_MINOR,
4107 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4109 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01004110};
Hanno Beckera835da52019-05-16 12:39:07 +01004111
4112/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004113 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02004114 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004115 *
David Horstmanncb01b362024-02-29 17:31:46 +00004116 * TLS 1.2 session:
4117 *
4118 * struct {
4119 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
4120 * opaque ticket<0..2^24-1>; // length 0 means no ticket
4121 * uint32 ticket_lifetime;
4122 * #endif
4123 * } ClientOnlyData;
4124 *
4125 * struct {
4126 * #if defined(MBEDTLS_HAVE_TIME)
4127 * uint64 start_time;
4128 * #endif
4129 * uint8 session_id_len; // at most 32
4130 * opaque session_id[32];
4131 * opaque master[48]; // fixed length in the standard
4132 * uint32 verify_result;
4133 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
4134 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
4135 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00004136 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00004137 * opaque peer_cert_digest<0..2^8-1>
4138 * #endif
4139 * select (endpoint) {
4140 * case client: ClientOnlyData;
4141 * case server: uint64 ticket_creation_time;
4142 * };
4143 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4144 * uint8 mfl_code; // up to 255 according to standard
4145 * #endif
4146 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4147 * uint8 encrypt_then_mac; // 0 or 1
4148 * #endif
4149 * } serialized_session_tls12;
4150 *
4151 *
4152 * TLS 1.3 Session:
4153 *
4154 * struct {
4155 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
4156 * opaque hostname<0..2^16-1>;
4157 * #endif
4158 * #if defined(MBEDTLS_HAVE_TIME)
4159 * uint64 ticket_reception_time;
4160 * #endif
4161 * uint32 ticket_lifetime;
4162 * opaque ticket<1..2^16-1>;
4163 * } ClientOnlyData;
4164 *
4165 * struct {
4166 * uint32 ticket_age_add;
4167 * uint8 ticket_flags;
4168 * opaque resumption_key<0..255>;
4169 * #if defined(MBEDTLS_SSL_EARLY_DATA)
4170 * uint32 max_early_data_size;
4171 * #endif
4172 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
4173 * uint16 record_size_limit;
4174 * #endif
4175 * select ( endpoint ) {
4176 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004177 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00004178 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00004179 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00004180 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00004181 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00004182 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00004183 * #endif
4184 * };
4185 * } serialized_session_tls13;
4186 *
4187 *
4188 * SSL session:
4189 *
4190 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004191 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004192 * opaque mbedtls_version[3]; // library version: major, minor, patch
4193 * opaque session_format[2]; // library-version specific 16-bit field
4194 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004195 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004196 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004197 * Note: When updating the format, remember to keep
4198 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004199 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004200 * // In this version, `session_format` determines
4201 * // the setting of those compile-time
4202 * // configuration options which influence
4203 * // the structure of mbedtls_ssl_session.
4204 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004205 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004206 * // - TLS 1.2 (0x0303)
4207 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004208 * uint8_t endpoint;
4209 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004210 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004211 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004212 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004213 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004214 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004215 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004216 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004217 *
4218 * };
4219 *
4220 * } serialized_session;
4221 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004222 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004223
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004225static int ssl_session_save(const mbedtls_ssl_session *session,
4226 unsigned char omit_header,
4227 unsigned char *buf,
4228 size_t buf_len,
4229 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004230{
4231 unsigned char *p = buf;
4232 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004233 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004234#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4235 size_t out_len;
4236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4237#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 if (session == NULL) {
4239 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4240 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004241
Gilles Peskine449bd832023-01-11 14:50:10 +01004242 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004243 /*
4244 * Add Mbed TLS version identifier
4245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004247
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 if (used <= buf_len) {
4249 memcpy(p, ssl_serialized_session_header,
4250 sizeof(ssl_serialized_session_header));
4251 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004252 }
4253 }
4254
4255 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004256 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004257 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004258 used += 1 /* TLS version */
4259 + 1 /* endpoint */
4260 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (used <= buf_len) {
4262 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004263 *p++ = session->endpoint;
4264 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4265 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004266 }
4267
4268 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004269 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004271#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 case MBEDTLS_SSL_VERSION_TLS1_2:
4273 used += ssl_tls12_session_save(session, p, remaining_len);
4274 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004275#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4276
Jerry Yu251a12e2022-07-13 15:15:48 +08004277#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 case MBEDTLS_SSL_VERSION_TLS1_3:
4279 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4280 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4281 return ret;
4282 }
4283 used += out_len;
4284 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004285#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4286
Gilles Peskine449bd832023-01-11 14:50:10 +01004287 default:
4288 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004289 }
4290
4291 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004292 if (used > buf_len) {
4293 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4294 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004295
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004297}
4298
4299/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004300 * Public wrapper for ssl_session_save()
4301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004302int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4303 unsigned char *buf,
4304 size_t buf_len,
4305 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004306{
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004308}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004309
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004310/*
4311 * Deserialize session, see mbedtls_ssl_session_save() for format.
4312 *
4313 * This internal version is wrapped by a public function that cleans up in
4314 * case of error, and has an extra option omit_header.
4315 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004316MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004317static int ssl_session_load(mbedtls_ssl_session *session,
4318 unsigned char omit_header,
4319 const unsigned char *buf,
4320 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004321{
4322 const unsigned char *p = buf;
4323 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004324 size_t remaining_len;
4325
4326
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (session == NULL) {
4328 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4329 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004332 /*
4333 * Check Mbed TLS version identifier
4334 */
4335
Gilles Peskine449bd832023-01-11 14:50:10 +01004336 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4337 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004339
4340 if (memcmp(p, ssl_serialized_session_header,
4341 sizeof(ssl_serialized_session_header)) != 0) {
4342 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4343 }
4344 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004345 }
4346
4347 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004348 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004349 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004350 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4352 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004353 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004354 session->endpoint = *p++;
4355 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4356 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004357
4358 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004359 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004361#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 case MBEDTLS_SSL_VERSION_TLS1_2:
4363 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004364#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4365
Jerry Yu438ddd82022-07-07 06:55:50 +00004366#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 case MBEDTLS_SSL_VERSION_TLS1_3:
4368 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004369#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4370
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 default:
4372 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004373 }
4374}
4375
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004376/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004377 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4380 const unsigned char *buf,
4381 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004382{
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (ret != 0) {
4386 mbedtls_ssl_session_free(session);
4387 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004388
Gilles Peskine449bd832023-01-11 14:50:10 +01004389 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004390}
4391
4392/*
Paul Bakker1961b702013-01-25 14:49:24 +01004393 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004396static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004397{
4398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4399
Ronald Cron66dbf912022-02-02 15:33:46 +01004400 /*
4401 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004402 * that were written into the output buffer by the previous handshake step,
4403 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004404 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4405 * We proceed to the next handshake step only when all data from the
4406 * previous one have been sent to the peer, thus we make sure that this is
4407 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4408 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4409 * we have to wait before to go ahead.
4410 * In the case of TLS 1.3, handshake step handlers do not send data to the
4411 * peer. Data are only sent here and through
4412 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004413 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4416 return ret;
4417 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004418
4419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4421 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4422 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4423 return ret;
4424 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004425 }
4426#endif /* MBEDTLS_SSL_PROTO_DTLS */
4427
Gilles Peskine449bd832023-01-11 14:50:10 +01004428 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004429}
4430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004432{
Hanno Becker41934dd2021-08-07 19:13:43 +01004433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004434
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004436 ssl->conf == NULL ||
4437 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4439 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004440 }
4441
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 ret = ssl_prepare_handshake_step(ssl);
4443 if (ret != 0) {
4444 return ret;
4445 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 ret = mbedtls_ssl_handle_pending_alert(ssl);
4448 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004449 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 }
Jerry Yue7047812021-09-13 19:26:39 +08004451
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004452 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4453 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4454 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4458 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004459 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004460
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004462 case MBEDTLS_SSL_HELLO_REQUEST:
4463 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004464 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004465 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004466
Ronald Cron9f0fba32022-02-10 16:45:15 +01004467 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004468 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004469 break;
4470
4471 default:
4472#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4474 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4475 } else {
4476 ret = mbedtls_ssl_handshake_client_step(ssl);
4477 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004478#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004480#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004482#endif
4483 }
Jerry Yub9930e72021-08-06 17:11:51 +08004484 }
Ronald Cron6291b232023-03-08 15:51:25 +01004485#endif /* MBEDTLS_SSL_CLI_C */
4486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004489#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4490 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004492 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 ret = mbedtls_ssl_handshake_server_step(ssl);
4494 }
Ronald Cron6291b232023-03-08 15:51:25 +01004495#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4496 ret = mbedtls_ssl_handshake_server_step(ssl);
4497#else
4498 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004499#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004500 }
4501#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004504 /* handshake_step return error. And it is same
4505 * with alert_reason.
4506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004507 if (ssl->send_alert) {
4508 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004509 goto cleanup;
4510 }
4511 }
4512
4513cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004515}
4516
4517/*
4518 * Perform the SSL handshake
4519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004520int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004521{
4522 int ret = 0;
4523
Hanno Beckera817ea42020-10-20 15:20:23 +01004524 /* Sanity checks */
4525
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 if (ssl == NULL || ssl->conf == NULL) {
4527 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4528 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004529
Hanno Beckera817ea42020-10-20 15:20:23 +01004530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4532 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4533 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4534 "mbedtls_ssl_set_timer_cb() for DTLS"));
4535 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004536 }
4537#endif /* MBEDTLS_SSL_PROTO_DTLS */
4538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004540
Hanno Beckera817ea42020-10-20 15:20:23 +01004541 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4543 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004544
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004546 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 }
Paul Bakker1961b702013-01-25 14:49:24 +01004548 }
4549
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004553}
4554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555#if defined(MBEDTLS_SSL_RENEGOTIATION)
4556#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004557/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004558 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004559 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004560MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004561static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004562{
Janos Follath865b3eb2019-12-16 11:46:15 +00004563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004566
4567 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4569 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4572 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4573 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004574 }
4575
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004577
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004581
4582/*
4583 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584 * - any side: calling mbedtls_ssl_renegotiate(),
4585 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4586 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004587 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004588 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004591int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004592{
Janos Follath865b3eb2019-12-16 11:46:15 +00004593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if ((ret = ssl_handshake_init(ssl)) != 0) {
4598 return ret;
4599 }
Paul Bakker48916f92012-09-16 19:57:18 +00004600
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004601 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4602 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4605 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4606 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004607 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004609 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004611 }
4612#endif
4613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004614 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4615 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4618 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4619 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004620 }
4621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004625}
4626
4627/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004628 * Renegotiate current connection on client,
4629 * or request renegotiation on server
4630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004631int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if (ssl == NULL || ssl->conf == NULL) {
4636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4637 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004640 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4642 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4644 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004646 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004647
4648 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if (ssl->out_left != 0) {
4650 return mbedtls_ssl_flush_output(ssl);
4651 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004654 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004658 /*
4659 * On client, either start the renegotiation process or,
4660 * if already in progress, continue the handshake
4661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4663 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004665 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004666
4667 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4669 return ret;
4670 }
4671 } else {
4672 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4673 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4674 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004675 }
4676 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004684{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004685 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004688 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004690
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004691#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Brett Warrene0edc842021-08-17 09:53:13 +01004692#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004693 if (ssl->handshake->group_list_heap_allocated) {
4694 mbedtls_free((void *) handshake->group_list);
4695 }
Brett Warrene0edc842021-08-17 09:53:13 +01004696 handshake->group_list = NULL;
4697#endif /* MBEDTLS_DEPRECATED_REMOVED */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004698#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Brett Warrene0edc842021-08-17 09:53:13 +01004699
Ronald Crone68ab4f2022-10-05 12:46:29 +02004700#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004701#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 if (ssl->handshake->sig_algs_heap_allocated) {
4703 mbedtls_free((void *) handshake->sig_algs);
4704 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004705 handshake->sig_algs = NULL;
4706#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004707#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 if (ssl->handshake->certificate_request_context) {
4709 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004710 }
4711#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004712#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004713
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004714#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4716 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004717 handshake->async_in_progress = 0;
4718 }
4719#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4720
Elena Uziunaite0916cd72024-05-23 17:01:07 +01004721#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004722#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004724#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004725 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004726#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004727#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01004728#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004729#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004731#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004732 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004733#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004734#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004738#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004739#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004740 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004742#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004743
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004744#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004745#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004747 /*
4748 * Opaque keys are not stored in the handshake's data and it's the user
4749 * responsibility to destroy them. Clear ones, instead, are created by
4750 * the TLS library and should be destroyed at the same level
4751 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4753 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004754 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004755 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4756#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004758#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004759#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004761 handshake->ecjpake_cache = NULL;
4762 handshake->ecjpake_cache_len = 0;
4763#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004764#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004765
Valerio Setti7aeec542023-07-05 18:57:21 +02004766#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004767 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004768 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004769 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004771#endif
4772
Ronald Cron73fe8df2022-10-05 14:31:43 +02004773#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004774#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004776 /* The maintenance of the external PSK key slot is the
4777 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 if (ssl->handshake->psk_opaque_is_internal) {
4779 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004780 ssl->handshake->psk_opaque_is_internal = 0;
4781 }
4782 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4783 }
Neil Armstronge952a302022-05-03 10:22:14 +02004784#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004786 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004787 }
Neil Armstronge952a302022-05-03 10:22:14 +02004788#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004789#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4792 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004793 /*
4794 * Free only the linked list wrapper, not the keys themselves
4795 * since the belong to the SNI callback
4796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004798#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004799
Gilles Peskineeccd8882020-03-10 12:19:08 +01004800#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4802 if (handshake->ecrs_peer_cert != NULL) {
4803 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4804 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004805 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004806#endif
4807
Hanno Becker75173122019-02-06 16:18:31 +00004808#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4809 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004811#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4812
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004813#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4815 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004816#endif /* MBEDTLS_SSL_CLI_C &&
4817 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004818
4819#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 mbedtls_ssl_flight_free(handshake->flight);
4821 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004822#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004823
Valerio Settiea59c432023-07-25 11:14:03 +02004824#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004825 if (handshake->xxdh_psa_privkey_is_external == 0) {
4826 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 }
Valerio Settiea59c432023-07-25 11:14:03 +02004828#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004829
Ronald Cron6f135e12021-12-08 16:57:54 +01004830#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 mbedtls_ssl_transform_free(handshake->transform_handshake);
4832 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004833#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4835 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004836#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004837#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004838
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004839
4840#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4841 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4842 * processes datagrams and the fact that a datagram is allowed to have
4843 * several records in it, it is possible that the I/O buffers are not
4844 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4846 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004847#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004848
Jerry Yuba9c7272021-10-30 11:54:10 +08004849 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 mbedtls_platform_zeroize(handshake,
4851 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004852}
4853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004855{
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004857 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004860#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004862#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004863
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004864#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004865#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4866 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004868#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004870#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004871
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004872#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4873 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004874 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004875#endif
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004878}
4879
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004880#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004881
4882#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4883#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4884#else
4885#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4886#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4887
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004888#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004889
4890#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4891#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4892#else
4893#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4894#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4895
4896#if defined(MBEDTLS_SSL_ALPN)
4897#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4898#else
4899#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4900#endif /* MBEDTLS_SSL_ALPN */
4901
4902#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4903#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4904#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4905#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4906
4907#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 ((uint32_t) ( \
4909 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4910 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4911 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4912 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4913 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4914 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4915 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4916 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004917
Chien Wong4e9683e2023-12-28 17:07:43 +08004918static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004919 MBEDTLS_VERSION_MAJOR,
4920 MBEDTLS_VERSION_MINOR,
4921 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4923 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4924 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4925 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4926 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004927};
4928
Paul Bakker5121ce52009-01-03 21:22:43 +00004929/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004930 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004931 *
4932 * The format of the serialized data is:
4933 * (in the presentation language of TLS, RFC 8446 section 3)
4934 *
4935 * // header
4936 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004937 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004938 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004939 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004940 * Note: When updating the format, remember to keep these
4941 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004942 *
4943 * // session sub-structure
4944 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4945 * // transform sub-structure
4946 * uint8 random[64]; // ServerHello.random+ClientHello.random
4947 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4948 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4949 * // fields from ssl_context
4950 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4951 * uint64 in_window_top; // DTLS: last validated record seq_num
4952 * uint64 in_window; // DTLS: bitmask for replay protection
4953 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4954 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4955 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4956 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4957 *
4958 * Note that many fields of the ssl_context or sub-structures are not
4959 * serialized, as they fall in one of the following categories:
4960 *
4961 * 1. forced value (eg in_left must be 0)
4962 * 2. pointer to dynamically-allocated memory (eg session, transform)
4963 * 3. value can be re-derived from other data (eg session keys from MS)
4964 * 4. value was temporary (eg content of input buffer)
4965 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004966 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004967int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4968 unsigned char *buf,
4969 size_t buf_len,
4970 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004971{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004972 unsigned char *p = buf;
4973 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004974 size_t session_len;
4975 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004976
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004977 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004978 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4979 * this function's documentation.
4980 *
4981 * These are due to assumptions/limitations in the implementation. Some of
4982 * them are likely to stay (no handshake in progress) some might go away
4983 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004984 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004985 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4987 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4988 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004989 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 if (ssl->handshake != NULL) {
4991 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4992 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004993 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004994 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 if (ssl->transform == NULL || ssl->session == NULL) {
4996 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004998 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004999 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 if (mbedtls_ssl_check_pending(ssl) != 0) {
5001 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
5002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005003 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 if (ssl->out_left != 0) {
5005 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
5006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005007 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00005008 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5010 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
5011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005012 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005013 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
5015 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
5016 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005017 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005018 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01005019 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
5020 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
5021 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005022 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005023 /* Renegotiation must not be enabled */
5024#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5026 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
5027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03005028 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02005029#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005030
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005031 /*
5032 * Version and format identifier
5033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005035
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 if (used <= buf_len) {
5037 memcpy(p, ssl_serialized_context_header,
5038 sizeof(ssl_serialized_context_header));
5039 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005040 }
5041
5042 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005043 * Session (length + data)
5044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
5046 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
5047 return ret;
5048 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005049
5050 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 if (used <= buf_len) {
5052 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005053 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005054
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 ret = ssl_session_save(ssl->session, 1,
5056 p, session_len, &session_len);
5057 if (ret != 0) {
5058 return ret;
5059 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005060
5061 p += session_len;
5062 }
5063
5064 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005065 * Transform
5066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 used += sizeof(ssl->transform->randbytes);
5068 if (used <= buf_len) {
5069 memcpy(p, ssl->transform->randbytes,
5070 sizeof(ssl->transform->randbytes));
5071 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005072 }
5073
5074#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005075 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005077 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005079 p += ssl->transform->in_cid_len;
5080
5081 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005082 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005083 p += ssl->transform->out_cid_len;
5084 }
5085#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5086
5087 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005088 * Saved fields from top-level ssl_context structure
5089 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005090 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (used <= buf_len) {
5092 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005093 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005094 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005095
5096#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5097 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 if (used <= buf_len) {
5099 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005100 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005101
Gilles Peskine449bd832023-01-11 14:50:10 +01005102 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005103 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005104 }
5105#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5106
5107#if defined(MBEDTLS_SSL_PROTO_DTLS)
5108 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005110 *p++ = ssl->disable_datagram_packing;
5111 }
5112#endif /* MBEDTLS_SSL_PROTO_DTLS */
5113
Jerry Yuae0b2e22021-10-08 15:21:19 +08005114 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (used <= buf_len) {
5116 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08005117 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005118 }
5119
5120#if defined(MBEDTLS_SSL_PROTO_DTLS)
5121 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 if (used <= buf_len) {
5123 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01005124 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005125 }
5126#endif /* MBEDTLS_SSL_PROTO_DTLS */
5127
5128#if defined(MBEDTLS_SSL_ALPN)
5129 {
5130 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01005131 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005132 : 0;
5133
5134 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005136 *p++ = alpn_len;
5137
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 if (ssl->alpn_chosen != NULL) {
5139 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005140 p += alpn_len;
5141 }
5142 }
5143 }
5144#endif /* MBEDTLS_SSL_ALPN */
5145
5146 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005147 * Done
5148 */
5149 *olen = used;
5150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 if (used > buf_len) {
5152 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5153 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005158}
5159
5160/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005161 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005162 *
5163 * This internal version is wrapped by a public function that cleans up in
5164 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005165 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005166MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005167static int ssl_context_load(mbedtls_ssl_context *ssl,
5168 const unsigned char *buf,
5169 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005170{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005171 const unsigned char *p = buf;
5172 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005173 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00005174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005175#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04005176 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005177#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005178
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005179 /*
5180 * The context should have been freshly setup or reset.
5181 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02005182 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005183 * renegotiating, or if the user mistakenly loaded a session first.)
5184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
5186 ssl->session != NULL) {
5187 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005188 }
5189
5190 /*
5191 * We can't check that the config matches the initial one, but we can at
5192 * least check it matches the requirements for serializing.
5193 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005194 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005195#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02005196 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005197#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01005198 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
5199 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
5200 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
5201 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005203 }
5204
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005206
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005207 /*
5208 * Check version identifier
5209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5211 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005213
5214 if (memcmp(p, ssl_serialized_context_header,
5215 sizeof(ssl_serialized_context_header)) != 0) {
5216 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5217 }
5218 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005219
5220 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005221 * Session
5222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if ((size_t) (end - p) < 4) {
5224 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5225 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005226
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005227 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005228 p += 4;
5229
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005230 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005231 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005232 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005233 ssl->session_in = ssl->session;
5234 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005235 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005236
Gilles Peskine449bd832023-01-11 14:50:10 +01005237 if ((size_t) (end - p) < session_len) {
5238 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5239 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 ret = ssl_session_load(ssl->session, 1, p, session_len);
5242 if (ret != 0) {
5243 mbedtls_ssl_session_free(ssl->session);
5244 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005245 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005246
5247 p += session_len;
5248
5249 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005250 * Transform
5251 */
5252
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005253 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005254 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005255#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005256 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005257 ssl->transform_in = ssl->transform;
5258 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005259 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005260#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005261
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5264 if (prf_func == NULL) {
5265 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5266 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005267
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005268 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5270 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5271 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005272
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 ret = ssl_tls12_populate_transform(ssl->transform,
5274 ssl->session->ciphersuite,
5275 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005276#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005277 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005278#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 prf_func,
5280 p, /* currently pointing to randbytes */
5281 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5282 ssl->conf->endpoint,
5283 ssl);
5284 if (ret != 0) {
5285 return ret;
5286 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005289
5290#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5291 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 if ((size_t) (end - p) < 1) {
5293 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5294 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005295
5296 ssl->transform->in_cid_len = *p++;
5297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5299 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5300 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005303 p += ssl->transform->in_cid_len;
5304
5305 ssl->transform->out_cid_len = *p++;
5306
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5308 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5309 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005312 p += ssl->transform->out_cid_len;
5313#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5314
5315 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005316 * Saved fields from top-level ssl_context structure
5317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 if ((size_t) (end - p) < 4) {
5319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5320 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005321
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005322 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005323 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005324
5325#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 if ((size_t) (end - p) < 16) {
5327 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5328 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005329
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005330 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005331 p += 8;
5332
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005333 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005334 p += 8;
5335#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5336
5337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if ((size_t) (end - p) < 1) {
5339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5340 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005341
5342 ssl->disable_datagram_packing = *p++;
5343#endif /* MBEDTLS_SSL_PROTO_DTLS */
5344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5346 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5347 }
5348 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5349 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005350
5351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 if ((size_t) (end - p) < 2) {
5353 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5354 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005355
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005356 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005357 p += 2;
5358#endif /* MBEDTLS_SSL_PROTO_DTLS */
5359
5360#if defined(MBEDTLS_SSL_ALPN)
5361 {
5362 uint8_t alpn_len;
5363 const char **cur;
5364
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 if ((size_t) (end - p) < 1) {
5366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5367 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005368
5369 alpn_len = *p++;
5370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005372 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5374 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005375 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005376 ssl->alpn_chosen = *cur;
5377 break;
5378 }
5379 }
5380 }
5381
5382 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5384 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5385 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005386
5387 p += alpn_len;
5388 }
5389#endif /* MBEDTLS_SSL_ALPN */
5390
5391 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005392 * Forced fields from top-level ssl_context structure
5393 *
5394 * Most of them already set to the correct value by mbedtls_ssl_init() and
5395 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5396 */
5397 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005398 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005399
Hanno Becker361b10d2019-08-30 10:42:49 +01005400 /* Adjust pointers for header fields of outgoing records to
5401 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005403
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005404#if defined(MBEDTLS_SSL_PROTO_DTLS)
5405 ssl->in_epoch = 1;
5406#endif
5407
5408 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5409 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005410 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5411 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 if (ssl->handshake != NULL) {
5413 mbedtls_ssl_handshake_free(ssl);
5414 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005415 ssl->handshake = NULL;
5416 }
5417
5418 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005419 * Done - should have consumed entire buffer
5420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (p != end) {
5422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5423 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005426}
5427
5428/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005429 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005431int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5432 const unsigned char *buf,
5433 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005434{
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (ret != 0) {
5438 mbedtls_ssl_free(context);
5439 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005442}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005443#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005444
5445/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 * Free an SSL context
5447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005448void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005449{
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005451 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005455
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005457#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5458 size_t out_buf_len = ssl->out_buf_len;
5459#else
5460 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5461#endif
5462
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005463 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005464 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 }
5466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005468#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5469 size_t in_buf_len = ssl->in_buf_len;
5470#else
5471 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5472#endif
5473
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005474 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005475 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 }
5477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 if (ssl->transform) {
5479 mbedtls_ssl_transform_free(ssl->transform);
5480 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005481 }
5482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 if (ssl->handshake) {
5484 mbedtls_ssl_handshake_free(ssl);
5485 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005486
5487#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5489 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005490#endif
5491
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 mbedtls_ssl_session_free(ssl->session_negotiate);
5493 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005494 }
5495
Ronald Cron6f135e12021-12-08 16:57:54 +01005496#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 mbedtls_ssl_transform_free(ssl->transform_application);
5498 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005499#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 if (ssl->session) {
5502 mbedtls_ssl_session_free(ssl->session);
5503 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005504 }
5505
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005506#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005508 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005511
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005512#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005514#endif
5515
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005517
Paul Bakker86f04f42013-02-14 11:20:09 +01005518 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005520}
5521
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005522/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005523 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005525void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005526{
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005528}
5529
Gilles Peskineae270bf2021-06-02 00:05:29 +02005530/* The selection should be the same as mbedtls_x509_crt_profile_default in
5531 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005532 * curves with a lower resource usage come first.
Manuel Pégourié-Gonnard93d45912025-01-14 11:45:44 +01005533 * See the documentation of mbedtls_ssl_conf_groups() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005534 * about this list.
5535 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005536static const uint16_t ssl_preset_default_groups[] = {
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005537#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Brett Warrene0edc842021-08-17 09:53:13 +01005538 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005539#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005540#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005541 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005542#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005543#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005544 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005545#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005546#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Brett Warrene0edc842021-08-17 09:53:13 +01005547 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005548#endif
Elena Uziunaite24e24f22024-07-04 17:53:40 +01005549#if defined(PSA_WANT_ECC_SECP_R1_521)
Brett Warrene0edc842021-08-17 09:53:13 +01005550 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005551#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005552#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005553 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005554#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005555#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005556 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005557#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005558#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Brett Warrene0edc842021-08-17 09:53:13 +01005559 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005560#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005561#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005562 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5563 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5564 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5565 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5566 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5567#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005568 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005569};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005570
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005571static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005572 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5573 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5574 0
5575};
5576
Ronald Crone68ab4f2022-10-05 12:46:29 +02005577#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005578
Jerry Yu909df7b2022-01-22 11:56:27 +08005579/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005580 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005581 * rules SHOULD be upheld.
5582 * - No duplicate entries.
5583 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005584 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005585 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005586 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005587static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005588
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005589#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005590 defined(PSA_WANT_ALG_SHA_256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005591 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005592 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005593 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5594#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005595
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005596#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005597 defined(PSA_WANT_ALG_SHA_384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005598 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005599 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005600 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5601#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005602
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005603#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +02005604 defined(PSA_WANT_ALG_SHA_512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005605 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005606 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005607 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5608#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005609
Gabor Mezeic15ef932024-06-13 12:53:54 +02005610#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005611 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005612#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005613
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005614#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005615 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005616#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005617
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005618#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005619 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005620#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005621
Gabor Mezeic15ef932024-06-13 12:53:54 +02005622#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005623 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Gabor Mezeic15ef932024-06-13 12:53:54 +02005624#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005625
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005626#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005627 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005628#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005629
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005630#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005631 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005632#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005633
Gabor Mezei15b95a62022-05-09 16:37:58 +02005634 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005635};
5636
5637/* NOTICE: see above */
5638#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5639static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005640
Gabor Mezeic15ef932024-06-13 12:53:54 +02005641#if defined(PSA_WANT_ALG_SHA_512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005642#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005644#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005645#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5646 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005647#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005648#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005650#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005651#endif /* PSA_WANT_ALG_SHA_512 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005652
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005653#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005654#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005656#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005657#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5658 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005659#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005660#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005662#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005663#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005664
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005665#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005666#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005668#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005669#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5670 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005671#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005672#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005674#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005675#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005676
Gabor Mezei15b95a62022-05-09 16:37:58 +02005677 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005678};
5679#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005680
Jerry Yu909df7b2022-01-22 11:56:27 +08005681/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005682static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005683
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005684#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005685 defined(PSA_WANT_ALG_SHA_256) && \
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005686 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005687 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005688 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5689#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005690
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005691#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005692 defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005693 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu53037892022-01-25 11:02:06 +08005694 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005695 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5696#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005697
Gabor Mezei15b95a62022-05-09 16:37:58 +02005698 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005699};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005700
Jerry Yu909df7b2022-01-22 11:56:27 +08005701/* NOTICE: see above */
5702#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5703static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005704
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005705#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005706#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005708#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005709#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005710
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005711#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005712#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005714#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005715#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005716
Gabor Mezei15b95a62022-05-09 16:37:58 +02005717 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005718};
Jerry Yu909df7b2022-01-22 11:56:27 +08005719#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5720
Ronald Crone68ab4f2022-10-05 12:46:29 +02005721#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005722
Chien Wong4e9683e2023-12-28 17:07:43 +08005723static const uint16_t ssl_preset_suiteb_groups[] = {
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005724#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005725 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005726#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005727#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005728 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005729#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005730 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005731};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005732
Ronald Crone68ab4f2022-10-05 12:46:29 +02005733#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005734/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005735 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005736MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005737static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005738{
5739 size_t i, j;
5740 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005741
Gilles Peskine449bd832023-01-11 14:50:10 +01005742 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5743 for (j = 0; j < i; j++) {
5744 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005745 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 }
5747 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5748 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5749 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005750 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005751 }
5752 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005754}
5755
Ronald Crone68ab4f2022-10-05 12:46:29 +02005756#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005757
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005758/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005759 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005761int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5762 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005763{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005764#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005766#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005767
Ronald Crone68ab4f2022-10-05 12:46:29 +02005768#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5770 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5771 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005772 }
5773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5775 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5776 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005777 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005778
5779#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5781 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5782 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005783 }
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5786 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5787 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005788 }
5789#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005790#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005791
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005792 /* Use the functions here so that they are covered in tests,
5793 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 mbedtls_ssl_conf_endpoint(conf, endpoint);
5795 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005796
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005797 /*
5798 * Things that are common to all presets
5799 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005800#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005802 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5803#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5804 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5805#endif
5806 }
5807#endif
5808
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005809#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5810 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5811#endif
5812
5813#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5814 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5815#endif
5816
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005817#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005818 conf->f_cookie_write = ssl_cookie_write_dummy;
5819 conf->f_cookie_check = ssl_cookie_check_dummy;
5820#endif
5821
5822#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5823 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5824#endif
5825
Janos Follath088ce432017-04-10 12:42:31 +01005826#if defined(MBEDTLS_SSL_SRV_C)
5827 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005828 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005829#endif
5830
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005831#if defined(MBEDTLS_SSL_PROTO_DTLS)
5832 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5833 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5834#endif
5835
5836#if defined(MBEDTLS_SSL_RENEGOTIATION)
5837 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 memset(conf->renego_period, 0x00, 2);
5839 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005840#endif
5841
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005842#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005844 const unsigned char dhm_p[] =
5845 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5846 const unsigned char dhm_g[] =
5847 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005848
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5850 dhm_p, sizeof(dhm_p),
5851 dhm_g, sizeof(dhm_g))) != 0) {
5852 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005853 }
5854 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005855#endif
5856
Ronald Cron6f135e12021-12-08 16:57:54 +01005857#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005858
5859#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005860 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005861#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005862 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005863#endif
5864#endif /* MBEDTLS_SSL_EARLY_DATA */
5865
Jerry Yud0766ec2022-09-22 10:46:57 +08005866#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005867 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005869#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005870 /*
5871 * Allow all TLS 1.3 key exchange modes by default.
5872 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005873 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005874#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005875
Gilles Peskine449bd832023-01-11 14:50:10 +01005876 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005877#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005878 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005879 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005880#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005882#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005884#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005885 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5886 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005887#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5888 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5889 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5890#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5891 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5892 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5893#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005895#endif
5896 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005897
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005898 /*
5899 * Preset-specific defaults
5900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005902 /*
5903 * NSA Suite B
5904 */
5905 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005906
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005907 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005908
5909#if defined(MBEDTLS_X509_CRT_PARSE_C)
5910 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005911#endif
5912
Ronald Crone68ab4f2022-10-05 12:46:29 +02005913#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005916 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005917 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005920#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005921
Brett Warrene0edc842021-08-17 09:53:13 +01005922 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005923 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005924
5925 /*
5926 * Default
5927 */
5928 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005929
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005930 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005931
5932#if defined(MBEDTLS_X509_CRT_PARSE_C)
5933 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5934#endif
5935
Ronald Crone68ab4f2022-10-05 12:46:29 +02005936#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005939 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005941#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005943#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005944
Brett Warrene0edc842021-08-17 09:53:13 +01005945 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005946
5947#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5948 conf->dhm_min_bitlen = 1024;
5949#endif
5950 }
5951
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005953}
5954
5955/*
5956 * Free mbedtls_ssl_config
5957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005958void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005959{
Troy-Butlerda73abc2024-04-02 13:37:31 -04005960 if (conf == NULL) {
5961 return;
5962 }
5963
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005964#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 mbedtls_mpi_free(&conf->dhm_P);
5966 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005967#endif
5968
Ronald Cron73fe8df2022-10-05 14:31:43 +02005969#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005970#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005972 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5973 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005974#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005976 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005977 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005978 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005979 }
5980
Gilles Peskine449bd832023-01-11 14:50:10 +01005981 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005982 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005983 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005984 conf->psk_identity_len = 0;
5985 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005986#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005987
5988#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005989 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005990#endif
5991
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005993}
5994
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005995#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02005996 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005997/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006000unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006001{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
6004 return MBEDTLS_SSL_SIG_RSA;
6005 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006006#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006007#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006008 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
6009 return MBEDTLS_SSL_SIG_ECDSA;
6010 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006011#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006013}
6014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01006016{
Gilles Peskine449bd832023-01-11 14:50:10 +01006017 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01006018 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006020 case MBEDTLS_PK_ECDSA:
6021 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006023 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006024 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01006025 }
6026}
6027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006029{
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031#if defined(MBEDTLS_RSA_C)
6032 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006034#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02006035#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006038#endif
6039 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006041 }
6042}
Valerio Settie9646ec2023-08-02 20:02:28 +02006043#endif /* MBEDTLS_PK_C &&
6044 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006045
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006046/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006047 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006049mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006050{
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 switch (hash) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006052#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006055#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006056#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006059#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006060#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006063#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006064#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006067#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006068#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006071#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006072#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006075#endif
6076 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006078 }
6079}
6080
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006081/*
6082 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
6083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006084unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006085{
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 switch (md) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01006087#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006088 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006090#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01006091#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006092 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01006093 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006094#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01006095#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006096 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02006098#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006099#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006100 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006102#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006103#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006104 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02006106#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02006107#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006108 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006110#endif
6111 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006113 }
6114}
6115
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006116/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02006117 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006118 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006121{
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006123
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 if (group_list == NULL) {
6125 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01006126 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 for (; *group_list != 0; group_list++) {
6129 if (*group_list == tls_id) {
6130 return 0;
6131 }
6132 }
6133
6134 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006135}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006136
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006137#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006138/*
6139 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
6140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006141int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006142{
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006144
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006146 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01006150}
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01006151#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Valerio Setti67419f02023-01-04 16:12:42 +01006152
Valerio Setti18c9fed2022-12-30 17:44:24 +01006153static const struct {
6154 uint16_t tls_id;
6155 mbedtls_ecp_group_id ecp_group_id;
6156 psa_ecc_family_t psa_family;
6157 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01006158} tls_id_match_table[] =
6159{
Elena Uziunaite24e24f22024-07-04 17:53:40 +01006160#if defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settiacd32c02023-06-29 18:06:29 +02006161 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006162#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006163#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settiacd32c02023-06-29 18:06:29 +02006164 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006165#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01006166#if defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006167 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006168#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006169#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02006170 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006171#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01006172#if defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006173 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006174#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006175#if defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006176 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006177#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01006178#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02006179 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006180#endif
Elena Uziunaiteeaa0cf02024-07-04 17:41:25 +01006181#if defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02006182 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006183#endif
Elena Uziunaitea3632862024-07-04 13:52:43 +01006184#if defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006185 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006186#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01006187#if defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02006188 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006189#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006190#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settiacd32c02023-06-29 18:06:29 +02006191 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006192#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01006193#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settiacd32c02023-06-29 18:06:29 +02006194 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006195#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02006196 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01006197};
6198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02006200 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006202{
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6204 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006205 if (type != NULL) {
6206 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 }
6208 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006209 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006211 return PSA_SUCCESS;
6212 }
6213 }
6214
6215 return PSA_ERROR_NOT_SUPPORTED;
6216}
6217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006219{
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6221 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006222 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006224 }
6225
6226 return MBEDTLS_ECP_DP_NONE;
6227}
6228
Gilles Peskine449bd832023-01-11 14:50:10 +01006229uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006230{
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6232 i++) {
6233 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006234 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006236 }
6237
6238 return 0;
6239}
6240
Valerio Setti67419f02023-01-04 16:12:42 +01006241#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006242static const struct {
6243 uint16_t tls_id;
6244 const char *name;
6245} tls_id_curve_name_table[] =
6246{
Valerio Setti54e23792023-07-07 10:49:27 +02006247 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6248 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6249 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6250 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6251 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6252 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6253 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6254 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6255 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6256 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6257 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6258 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6259 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006260 { 0, NULL },
6261};
6262
Gilles Peskine449bd832023-01-11 14:50:10 +01006263const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006264{
Valerio Settiacd32c02023-06-29 18:06:29 +02006265 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6266 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6267 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006269 }
6270
6271 return NULL;
6272}
Valerio Setti67419f02023-01-04 16:12:42 +01006273#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006274
Jerry Yu148165c2021-09-24 23:20:59 +08006275#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006276int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6277 const mbedtls_md_type_t md,
6278 unsigned char *dst,
6279 size_t dst_len,
6280 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006281{
Ronald Cronf6893e12022-01-07 22:09:01 +01006282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6283 psa_hash_operation_t *hash_operation_to_clone;
6284 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6285
Jerry Yu148165c2021-09-24 23:20:59 +08006286 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006289#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 case MBEDTLS_MD_SHA384:
6291 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6292 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006293#endif
6294
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006295#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 case MBEDTLS_MD_SHA256:
6297 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6298 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006299#endif
6300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 default:
6302 goto exit;
6303 }
6304
6305 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6306 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006307 goto exit;
6308 }
6309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6311 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006312 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006314
6315exit:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006316#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006317 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006318 (void) ssl;
6319#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006320 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006321}
6322#else /* MBEDTLS_USE_PSA_CRYPTO */
6323
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006324#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006325MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006326static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
6327 unsigned char *dst,
6328 size_t dst_len,
6329 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006330{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006331 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006332 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 if (dst_len < 48) {
6335 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6336 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006337
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006338 mbedtls_md_init(&sha384);
6339 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006340 if (ret != 0) {
6341 goto exit;
6342 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006343 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006344 if (ret != 0) {
6345 goto exit;
6346 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006347
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006348 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006349 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006350 goto exit;
6351 }
6352
6353 *olen = 48;
6354
6355exit:
6356
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006357 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006359}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006360#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006361
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006362#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006363MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006364static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
6365 unsigned char *dst,
6366 size_t dst_len,
6367 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006368{
Jerry Yu24c0ec32021-09-09 14:21:07 +08006369 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006370 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 if (dst_len < 32) {
6373 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6374 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08006375
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006376 mbedtls_md_init(&sha256);
6377 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6378 if (ret != 0) {
6379 goto exit;
6380 }
6381 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6382 if (ret != 0) {
6383 goto exit;
6384 }
Jerry Yuc5aef882021-12-23 20:15:02 +08006385
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006386 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
6387 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08006388 goto exit;
6389 }
6390
6391 *olen = 32;
6392
6393exit:
6394
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006395 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006397}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006398#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6401 const mbedtls_md_type_t md,
6402 unsigned char *dst,
6403 size_t dst_len,
6404 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08006405{
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006407
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006408#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 case MBEDTLS_MD_SHA384:
6410 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006411#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006412
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006413#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 case MBEDTLS_MD_SHA256:
6415 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006416#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 default:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006419#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006420 !defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 (void) ssl;
6422 (void) dst;
6423 (void) dst_len;
6424 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04006425#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08006427 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08006429}
XiaokangQian647719a2021-12-07 09:16:29 +00006430
Jerry Yu148165c2021-09-24 23:20:59 +08006431#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08006432
Ronald Crone68ab4f2022-10-05 12:46:29 +02006433#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006434/* mbedtls_ssl_parse_sig_alg_ext()
6435 *
6436 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6437 * value (TLS 1.3 RFC8446):
6438 * enum {
6439 * ....
6440 * ecdsa_secp256r1_sha256( 0x0403 ),
6441 * ecdsa_secp384r1_sha384( 0x0503 ),
6442 * ecdsa_secp521r1_sha512( 0x0603 ),
6443 * ....
6444 * } SignatureScheme;
6445 *
6446 * struct {
6447 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6448 * } SignatureSchemeList;
6449 *
6450 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6451 * value (TLS 1.2 RFC5246):
6452 * enum {
6453 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6454 * sha512(6), (255)
6455 * } HashAlgorithm;
6456 *
6457 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6458 * SignatureAlgorithm;
6459 *
6460 * struct {
6461 * HashAlgorithm hash;
6462 * SignatureAlgorithm signature;
6463 * } SignatureAndHashAlgorithm;
6464 *
6465 * SignatureAndHashAlgorithm
6466 * supported_signature_algorithms<2..2^16-2>;
6467 *
6468 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6469 * generalization of the TLS 1.2 signature algorithm extension.
6470 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6471 * `SignatureScheme` field of TLS 1.3
6472 *
6473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006474int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6475 const unsigned char *buf,
6476 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006477{
6478 const unsigned char *p = buf;
6479 size_t supported_sig_algs_len = 0;
6480 const unsigned char *supported_sig_algs_end;
6481 uint16_t sig_alg;
6482 uint32_t common_idx = 0;
6483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6485 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006486 p += 2;
6487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 memset(ssl->handshake->received_sig_algs, 0,
6489 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006492 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 while (p < supported_sig_algs_end) {
6494 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6495 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006496 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6498 sig_alg,
6499 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006500#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6502 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6503 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006504 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006505 }
6506#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6509 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006510
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006512 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6513 common_idx += 1;
6514 }
6515 }
6516 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 if (p != end) {
6518 MBEDTLS_SSL_DEBUG_MSG(1,
6519 ("Signature algorithms extension length misaligned"));
6520 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6521 MBEDTLS_ERR_SSL_DECODE_ERROR);
6522 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006523 }
6524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 if (common_idx == 0) {
6526 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6527 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6528 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6529 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006530 }
6531
Gabor Mezei15b95a62022-05-09 16:37:58 +02006532 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006533 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006534}
6535
Ronald Crone68ab4f2022-10-05 12:46:29 +02006536#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006537
Jerry Yudc7bd172022-02-17 13:44:15 +08006538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6539
6540#if defined(MBEDTLS_USE_PSA_CRYPTO)
6541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6543 mbedtls_svc_key_id_t key,
6544 psa_algorithm_t alg,
6545 const unsigned char *raw_psk, size_t raw_psk_length,
6546 const unsigned char *seed, size_t seed_length,
6547 const unsigned char *label, size_t label_length,
6548 const unsigned char *other_secret,
6549 size_t other_secret_length,
6550 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006551{
6552 psa_status_t status;
6553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 status = psa_key_derivation_setup(derivation, alg);
6555 if (status != PSA_SUCCESS) {
6556 return status;
6557 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6560 status = psa_key_derivation_input_bytes(derivation,
6561 PSA_KEY_DERIVATION_INPUT_SEED,
6562 seed, seed_length);
6563 if (status != PSA_SUCCESS) {
6564 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006565 }
6566
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 if (other_secret != NULL) {
6568 status = psa_key_derivation_input_bytes(derivation,
6569 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6570 other_secret, other_secret_length);
6571 if (status != PSA_SUCCESS) {
6572 return status;
6573 }
6574 }
6575
6576 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006577 status = psa_key_derivation_input_bytes(
6578 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 raw_psk, raw_psk_length);
6580 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006581 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006583 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 if (status != PSA_SUCCESS) {
6585 return status;
6586 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 status = psa_key_derivation_input_bytes(derivation,
6589 PSA_KEY_DERIVATION_INPUT_LABEL,
6590 label, label_length);
6591 if (status != PSA_SUCCESS) {
6592 return status;
6593 }
6594 } else {
6595 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006596 }
6597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 status = psa_key_derivation_set_capacity(derivation, capacity);
6599 if (status != PSA_SUCCESS) {
6600 return status;
6601 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006604}
6605
Andrzej Kurek57d10632022-10-24 10:32:01 -04006606#if defined(PSA_WANT_ALG_SHA_384) || \
6607 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006608MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006609static int tls_prf_generic(mbedtls_md_type_t md_type,
6610 const unsigned char *secret, size_t slen,
6611 const char *label,
6612 const unsigned char *random, size_t rlen,
6613 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006614{
6615 psa_status_t status;
6616 psa_algorithm_t alg;
6617 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6618 psa_key_derivation_operation_t derivation =
6619 PSA_KEY_DERIVATION_OPERATION_INIT;
6620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006622 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006624 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006626
6627 /* Normally a "secret" should be long enough to be impossible to
6628 * find by brute force, and in particular should not be empty. But
6629 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6630 * and for this use case it makes sense to have a 0-length "secret".
6631 * Since the key API doesn't allow importing a key of length 0,
6632 * keep master_key=0, which setup_psa_key_derivation() understands
6633 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006635 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6637 psa_set_key_algorithm(&key_attributes, alg);
6638 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006639
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6641 if (status != PSA_SUCCESS) {
6642 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6643 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006644 }
6645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 status = setup_psa_key_derivation(&derivation,
6647 master_key, alg,
6648 NULL, 0,
6649 random, rlen,
6650 (unsigned char const *) label,
6651 (size_t) strlen(label),
6652 NULL, 0,
6653 dlen);
6654 if (status != PSA_SUCCESS) {
6655 psa_key_derivation_abort(&derivation);
6656 psa_destroy_key(master_key);
6657 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006658 }
6659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6661 if (status != PSA_SUCCESS) {
6662 psa_key_derivation_abort(&derivation);
6663 psa_destroy_key(master_key);
6664 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006665 }
6666
Gilles Peskine449bd832023-01-11 14:50:10 +01006667 status = psa_key_derivation_abort(&derivation);
6668 if (status != PSA_SUCCESS) {
6669 psa_destroy_key(master_key);
6670 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006671 }
6672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 if (!mbedtls_svc_key_id_is_null(master_key)) {
6674 status = psa_destroy_key(master_key);
6675 }
6676 if (status != PSA_SUCCESS) {
6677 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6678 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006679
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006681}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006682#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006683#else /* MBEDTLS_USE_PSA_CRYPTO */
6684
Andrzej Kurek57d10632022-10-24 10:32:01 -04006685#if defined(MBEDTLS_MD_C) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006686 (defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006687 defined(PSA_WANT_ALG_SHA_384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006689static int tls_prf_generic(mbedtls_md_type_t md_type,
6690 const unsigned char *secret, size_t slen,
6691 const char *label,
6692 const unsigned char *random, size_t rlen,
6693 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006694{
6695 size_t nb;
6696 size_t i, j, k, md_len;
6697 unsigned char *tmp;
6698 size_t tmp_len = 0;
6699 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6700 const mbedtls_md_info_t *md_info;
6701 mbedtls_md_context_t md_ctx;
6702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006705
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6707 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6708 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 tmp_len = md_len + strlen(label) + rlen;
6713 tmp = mbedtls_calloc(1, tmp_len);
6714 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006715 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6716 goto exit;
6717 }
6718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 nb = strlen(label);
6720 memcpy(tmp + md_len, label, nb);
6721 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006722 nb += rlen;
6723
6724 /*
6725 * Compute P_<hash>(secret, label + random)[0..dlen]
6726 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006728 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6732 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006733 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 }
6735 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6736 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006737 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 }
6739 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6740 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006741 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006743
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 for (i = 0; i < dlen; i += md_len) {
6745 ret = mbedtls_md_hmac_reset(&md_ctx);
6746 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006747 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 }
6749 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6750 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006751 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 }
6753 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6754 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006755 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 ret = mbedtls_md_hmac_reset(&md_ctx);
6759 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006760 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 }
6762 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6763 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006764 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006765 }
6766 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6767 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006768 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006770
Gilles Peskine449bd832023-01-11 14:50:10 +01006771 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006774 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006776 }
6777
6778exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 if (tmp != NULL) {
6782 mbedtls_platform_zeroize(tmp, tmp_len);
6783 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006786
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006788
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006790}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006791#endif /* MBEDTLS_MD_C && ( PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006792#endif /* MBEDTLS_USE_PSA_CRYPTO */
6793
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006794#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006796static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6797 const char *label,
6798 const unsigned char *random, size_t rlen,
6799 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006800{
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6802 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006803}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006804#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006805
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006806#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006808static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6809 const char *label,
6810 const unsigned char *random, size_t rlen,
6811 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006812{
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6814 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006815}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006816#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006817
Jerry Yuf009d862022-02-17 14:01:37 +08006818/*
6819 * Set appropriate PRF function and other SSL / TLS1.2 functions
6820 *
6821 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006822 * - hash associated with the ciphersuite (only used by TLS 1.2)
6823 *
6824 * Outputs:
6825 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6826 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006827MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006828static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6829 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006830{
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006831#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006833 handshake->tls_prf = tls_prf_sha384;
6834 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6835 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006837#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006838#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuf009d862022-02-17 14:01:37 +08006839 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006840 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006841 handshake->tls_prf = tls_prf_sha256;
6842 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6843 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6844 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006845#else
Jerry Yuf009d862022-02-17 14:01:37 +08006846 {
Jerry Yuf009d862022-02-17 14:01:37 +08006847 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006848 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006850 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006851#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006852
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006854}
Jerry Yud6ab2352022-02-17 14:03:43 +08006855
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006856/*
6857 * Compute master secret if needed
6858 *
6859 * Parameters:
6860 * [in/out] handshake
6861 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6862 * (PSA-PSK) ciphersuite_info, psk_opaque
6863 * [out] premaster (cleared)
6864 * [out] master
6865 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6866 * debug: conf->f_dbg, conf->p_dbg
6867 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006868 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006869 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006870MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006871static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6872 unsigned char *master,
6873 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006874{
6875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6876
6877 /* cf. RFC 5246, Section 8.1:
6878 * "The master secret is always exactly 48 bytes in length." */
6879 size_t const master_secret_len = 48;
6880
6881#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6882 unsigned char session_hash[48];
6883#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6884
6885 /* The label for the KDF used for key expansion.
6886 * This is either "master secret" or "extended master secret"
6887 * depending on whether the Extended Master Secret extension
6888 * is used. */
6889 char const *lbl = "master secret";
6890
Przemek Stekielae4ed302022-04-05 17:15:55 +02006891 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006892 * - If the Extended Master Secret extension is not used,
6893 * this is ClientHello.Random + ServerHello.Random
6894 * (see Sect. 8.1 in RFC 5246).
6895 * - If the Extended Master Secret extension is used,
6896 * this is the transcript of the handshake so far.
6897 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006898 unsigned char const *seed = handshake->randbytes;
6899 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006900
6901#if !defined(MBEDTLS_DEBUG_C) && \
6902 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6903 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006905 ssl = NULL; /* make sure we don't use it except for those cases */
6906 (void) ssl;
6907#endif
6908
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (handshake->resume != 0) {
6910 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6911 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006912 }
6913
6914#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006916 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006917 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006918 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6919 if (ret != 0) {
6920 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6921 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006922
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6924 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006925 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006926#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006927
Przemek Stekiel99114f32022-04-22 11:20:09 +02006928#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006929 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006931 /* Perform PSK-to-MS expansion in a single step. */
6932 psa_status_t status;
6933 psa_algorithm_t alg;
6934 mbedtls_svc_key_id_t psk;
6935 psa_key_derivation_operation_t derivation =
6936 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01006937 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006944 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006946 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006948
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006949 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006953 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006954 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006955 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006956 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006957 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006958 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006959 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6960 other_secret = handshake->premaster + 2;
6961 break;
6962 default:
6963 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006964 }
6965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 status = setup_psa_key_derivation(&derivation, psk, alg,
6967 ssl->conf->psk, ssl->conf->psk_len,
6968 seed, seed_len,
6969 (unsigned char const *) lbl,
6970 (size_t) strlen(lbl),
6971 other_secret, other_secret_len,
6972 master_secret_len);
6973 if (status != PSA_SUCCESS) {
6974 psa_key_derivation_abort(&derivation);
6975 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006976 }
6977
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 status = psa_key_derivation_output_bytes(&derivation,
6979 master,
6980 master_secret_len);
6981 if (status != PSA_SUCCESS) {
6982 psa_key_derivation_abort(&derivation);
6983 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006984 }
6985
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 status = psa_key_derivation_abort(&derivation);
6987 if (status != PSA_SUCCESS) {
6988 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6989 }
6990 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006991#endif
6992 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006993#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6995 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006996 psa_status_t status;
6997 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6998 psa_key_derivation_operation_t derivation =
6999 PSA_KEY_DERIVATION_OPERATION_INIT;
7000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02007002
7003 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
7004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 status = psa_key_derivation_setup(&derivation, alg);
7006 if (status != PSA_SUCCESS) {
7007 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007008 }
7009
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 status = psa_key_derivation_set_capacity(&derivation,
7011 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
7012 if (status != PSA_SUCCESS) {
7013 psa_key_derivation_abort(&derivation);
7014 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007015 }
7016
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
7018 &derivation);
7019 if (status != PSA_SUCCESS) {
7020 psa_key_derivation_abort(&derivation);
7021 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007022 }
7023
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 status = psa_key_derivation_output_bytes(&derivation,
7025 handshake->premaster,
7026 handshake->pmslen);
7027 if (status != PSA_SUCCESS) {
7028 psa_key_derivation_abort(&derivation);
7029 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
7030 }
7031
7032 status = psa_key_derivation_abort(&derivation);
7033 if (status != PSA_SUCCESS) {
7034 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02007035 }
7036 }
7037#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
7039 lbl, seed, seed_len,
7040 master,
7041 master_secret_len);
7042 if (ret != 0) {
7043 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
7044 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007045 }
7046
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
7048 handshake->premaster,
7049 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 mbedtls_platform_zeroize(handshake->premaster,
7052 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007053 }
7054
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08007056}
7057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08007059{
7060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7061 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
7062 ssl->handshake->ciphersuite_info;
7063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007065
7066 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01007068 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 if (ret != 0) {
7070 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
7071 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007072 }
7073
7074 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 ret = ssl_compute_master(ssl->handshake,
7076 ssl->session_negotiate->master,
7077 ssl);
7078 if (ret != 0) {
7079 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
7080 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007081 }
7082
7083 /* Swap the client and server random values:
7084 * - MS derivation wanted client+server (RFC 5246 8.1)
7085 * - key derivation wants server+client (RFC 5246 6.3) */
7086 {
7087 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01007088 memcpy(tmp, ssl->handshake->randbytes, 64);
7089 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
7090 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
7091 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08007092 }
7093
7094 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
7096 ssl->session_negotiate->ciphersuite,
7097 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007098#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007100#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 ssl->handshake->tls_prf,
7102 ssl->handshake->randbytes,
7103 ssl->tls_version,
7104 ssl->conf->endpoint,
7105 ssl);
7106 if (ret != 0) {
7107 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
7108 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08007109 }
7110
7111 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 mbedtls_platform_zeroize(ssl->handshake->randbytes,
7113 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08007114
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08007116
Gilles Peskine449bd832023-01-11 14:50:10 +01007117 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08007118}
Jerry Yu8392e0d2022-02-17 14:10:24 +08007119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007121{
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007123#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007124 case MBEDTLS_SSL_HASH_SHA384:
7125 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7126 break;
7127#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007128#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01007129 case MBEDTLS_SSL_HASH_SHA256:
7130 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7131 break;
7132#endif
7133 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007135 }
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007136#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007137 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007138 (void) ssl;
7139#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01007141}
7142
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007143#if defined(MBEDTLS_USE_PSA_CRYPTO)
7144static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
7145 const psa_hash_operation_t *hs_op,
7146 size_t buffer_size,
7147 unsigned char *hash,
7148 size_t *hlen)
7149{
7150 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007151 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007152
7153#if !defined(MBEDTLS_DEBUG_C)
7154 (void) ssl;
7155#endif
7156 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007157 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007158 if (status != PSA_SUCCESS) {
7159 goto exit;
7160 }
7161
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007162 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007163 if (status != PSA_SUCCESS) {
7164 goto exit;
7165 }
7166
7167 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
7168 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
7169
7170exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007171 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007172 return mbedtls_md_error_from_psa(status);
7173}
7174#else
7175static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
7176 const mbedtls_md_context_t *hs_ctx,
7177 unsigned char *hash,
7178 size_t *hlen)
7179{
7180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007181 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007182
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007183 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007184
7185#if !defined(MBEDTLS_DEBUG_C)
7186 (void) ssl;
7187#endif
7188 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
7189
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007190 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007191 if (ret != 0) {
7192 goto exit;
7193 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007194 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007195 if (ret != 0) {
7196 goto exit;
7197 }
7198
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007199 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007200 if (ret != 0) {
7201 goto exit;
7202 }
7203
7204 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
7205
7206 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
7207 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
7208
7209exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007210 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007211 return ret;
7212}
7213#endif /* MBEDTLS_USE_PSA_CRYPTO */
7214
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007215#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007216int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
7217 unsigned char *hash,
7218 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08007219{
7220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007221 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
7222 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007223#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007224 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
7225 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08007226#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007227}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007228#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08007229
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007230#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007231int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
7232 unsigned char *hash,
7233 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08007234{
7235#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007236 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
7237 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007238#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02007239 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
7240 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08007241#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007242}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007243#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08007244
Neil Armstrong80f6f322022-05-03 17:56:38 +02007245#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
7246 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007247int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08007248{
7249 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007251 const unsigned char *psk = NULL;
7252 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007254
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007256 /*
7257 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007258 * checked before calling this function.
7259 *
7260 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02007261 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08007262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
7264 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7265 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02007266 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007267 }
7268
7269 /*
7270 * PMS = struct {
7271 * opaque other_secret<0..2^16-1>;
7272 * opaque psk<0..2^16-1>;
7273 * };
7274 * with "other_secret" depending on the particular key exchange
7275 */
7276#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
7278 if (end - p < 2) {
7279 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7280 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007283 p += 2;
7284
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (end < p || (size_t) (end - p) < psk_len) {
7286 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7287 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007288
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007290 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007292#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007293#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7296 size_t len;
7297
7298 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007300 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7302 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
7303 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007306 p += 2 + len;
7307
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
7309 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08007310#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02007311#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08007313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7314 size_t zlen;
7315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007317 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
7319 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
7320 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08007321 }
7322
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007324 p += 2 + zlen;
7325
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
7327 MBEDTLS_DEBUG_ECDH_Z);
7328 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02007329#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08007330 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7332 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08007333 }
7334
7335 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 if (end - p < 2) {
7337 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7338 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08007341 p += 2;
7342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 if (end < p || (size_t) (end - p) < psk_len) {
7344 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
7345 }
Jerry Yuce3dca42022-02-17 14:16:37 +08007346
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08007348 p += psk_len;
7349
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00007350 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08007351
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08007353}
Neil Armstrong80f6f322022-05-03 17:56:38 +02007354#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08007355
7356#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007358static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007359
7360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007361int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08007362{
7363 /* If renegotiation is not enforced, retransmit until we would reach max
7364 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007366 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
7367 unsigned char doublings = 1;
7368
Gilles Peskine449bd832023-01-11 14:50:10 +01007369 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08007370 ++doublings;
7371 ratio >>= 1;
7372 }
7373
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (++ssl->renego_records_seen > doublings) {
7375 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
7376 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08007377 }
7378 }
7379
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08007381}
7382#endif
7383#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08007384
Jerry Yud9526692022-02-17 14:23:47 +08007385/*
7386 * Handshake functions
7387 */
7388#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
7389/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01007390int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007391{
7392 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7393 ssl->handshake->ciphersuite_info;
7394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7398 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007399 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007401 }
7402
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7404 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007405}
7406
Gilles Peskine449bd832023-01-11 14:50:10 +01007407int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007408{
7409 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7410 ssl->handshake->ciphersuite_info;
7411
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007413
Gilles Peskine449bd832023-01-11 14:50:10 +01007414 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7415 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007416 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007418 }
7419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
7421 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007422}
7423
7424#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7425/* Some certificate support -> implement write and parse */
7426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007428{
7429 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
7430 size_t i, n;
7431 const mbedtls_x509_crt *crt;
7432 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7433 ssl->handshake->ciphersuite_info;
7434
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7438 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007439 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007441 }
7442
7443#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7445 if (ssl->handshake->client_auth == 0) {
7446 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007447 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007449 }
7450 }
7451#endif /* MBEDTLS_SSL_CLI_C */
7452#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7454 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007455 /* Should never happen because we shouldn't have picked the
7456 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007458 }
7459 }
7460#endif
7461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007463
7464 /*
7465 * 0 . 0 handshake type
7466 * 1 . 3 handshake length
7467 * 4 . 6 length of all certs
7468 * 7 . 9 length of cert. 1
7469 * 10 . n-1 peer certificate
7470 * n . n+2 length of cert. 2
7471 * n+3 . ... upper level cert, etc.
7472 */
7473 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007475
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007477 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7479 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7480 " > %" MBEDTLS_PRINTF_SIZET,
7481 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7482 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007483 }
7484
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7486 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7487 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007490 i += n; crt = crt->next;
7491 }
7492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7494 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7495 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007496
7497 ssl->out_msglen = i;
7498 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7499 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7500
7501 ssl->state++;
7502
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7504 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7505 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007506 }
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007511}
7512
7513#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7514
7515#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007516MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007517static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7518 unsigned char *crt_buf,
7519 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007520{
7521 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 if (peer_crt == NULL) {
7524 return -1;
7525 }
Jerry Yud9526692022-02-17 14:23:47 +08007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if (peer_crt->raw.len != crt_buf_len) {
7528 return -1;
7529 }
Jerry Yud9526692022-02-17 14:23:47 +08007530
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007532}
7533#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007534MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007535static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7536 unsigned char *crt_buf,
7537 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007538{
7539 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7540 unsigned char const * const peer_cert_digest =
7541 ssl->session->peer_cert_digest;
7542 mbedtls_md_type_t const peer_cert_digest_type =
7543 ssl->session->peer_cert_digest_type;
7544 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007545 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007546 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7547 size_t digest_len;
7548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 if (peer_cert_digest == NULL || digest_info == NULL) {
7550 return -1;
7551 }
Jerry Yud9526692022-02-17 14:23:47 +08007552
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 digest_len = mbedtls_md_get_size(digest_info);
7554 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7555 return -1;
7556 }
Jerry Yud9526692022-02-17 14:23:47 +08007557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7559 if (ret != 0) {
7560 return -1;
7561 }
Jerry Yud9526692022-02-17 14:23:47 +08007562
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007564}
7565#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7566#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7567
7568/*
7569 * Once the certificate message is read, parse it into a cert chain and
7570 * perform basic checks, but leave actual verification to the caller
7571 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007572MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007573static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7574 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007575{
7576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7577#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007579#endif
7580 size_t i, n;
7581 uint8_t alert;
7582
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7584 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7585 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7586 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7587 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007588 }
7589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7591 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7592 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7593 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007594 }
7595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7598 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7599 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7600 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007601 }
7602
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007604
7605 /*
7606 * Same message structure as in mbedtls_ssl_write_certificate()
7607 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007608 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (ssl->in_msg[i] != 0 ||
7611 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7612 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7613 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7614 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7615 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007616 }
7617
7618 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7619 i += 3;
7620
7621 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007623 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 if (i + 3 > ssl->in_hslen) {
7625 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7626 mbedtls_ssl_send_alert_message(ssl,
7627 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7628 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7629 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007630 }
7631 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7632 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 if (ssl->in_msg[i] != 0) {
7634 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7635 mbedtls_ssl_send_alert_message(ssl,
7636 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7637 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7638 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007639 }
7640
7641 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007642 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007643 i += 3;
7644
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 if (n < 128 || i + n > ssl->in_hslen) {
7646 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7647 mbedtls_ssl_send_alert_message(ssl,
7648 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7649 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7650 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007651 }
7652
7653 /* Check if we're handling the first CRT in the chain. */
7654#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007656 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007658 /* During client-side renegotiation, check that the server's
7659 * end-CRTs hasn't changed compared to the initial handshake,
7660 * mitigating the triple handshake attack. On success, reuse
7661 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7663 if (ssl_check_peer_crt_unchanged(ssl,
7664 &ssl->in_msg[i],
7665 n) != 0) {
7666 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7667 mbedtls_ssl_send_alert_message(ssl,
7668 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7669 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7670 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007671 }
7672
7673 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007675 }
7676#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7677
7678 /* Parse the next certificate in the chain. */
7679#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007681#else
7682 /* If we don't need to store the CRT chain permanently, parse
7683 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007685#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007687 case 0: /*ok*/
7688 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7689 /* Ignore certificate with an unknown algorithm: maybe a
7690 prior certificate was already trusted. */
7691 break;
7692
7693 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7694 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7695 goto crt_parse_der_failed;
7696
7697 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7698 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7699 goto crt_parse_der_failed;
7700
7701 default:
7702 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007703crt_parse_der_failed:
7704 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7705 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7706 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007707 }
7708
7709 i += n;
7710 }
7711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7713 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007714}
7715
7716#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007717MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007718static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007719{
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7721 return -1;
7722 }
Jerry Yud9526692022-02-17 14:23:47 +08007723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007725 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7726 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7728 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7729 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007730 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007732}
7733#endif /* MBEDTLS_SSL_SRV_C */
7734
7735/* Check if a certificate message is expected.
7736 * Return either
7737 * - SSL_CERTIFICATE_EXPECTED, or
7738 * - SSL_CERTIFICATE_SKIP
7739 * indicating whether a Certificate message is expected or not.
7740 */
7741#define SSL_CERTIFICATE_EXPECTED 0
7742#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007743MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007744static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7745 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007746{
7747 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7748 ssl->handshake->ciphersuite_info;
7749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7751 return SSL_CERTIFICATE_SKIP;
7752 }
Jerry Yud9526692022-02-17 14:23:47 +08007753
7754#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007756 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007757 ssl->session_negotiate->verify_result =
7758 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007760 }
7761 }
7762#else
7763 ((void) authmode);
7764#endif /* MBEDTLS_SSL_SRV_C */
7765
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007767}
7768
Jerry Yud9526692022-02-17 14:23:47 +08007769#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007770MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007771static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7772 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007773{
7774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7775 /* Remember digest of the peer's end-CRT. */
7776 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7778 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7779 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7780 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7781 mbedtls_ssl_send_alert_message(ssl,
7782 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7783 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007784
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007786 }
7787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 ret = mbedtls_md(mbedtls_md_info_from_type(
7789 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7790 start, len,
7791 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007792
7793 ssl->session_negotiate->peer_cert_digest_type =
7794 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7795 ssl->session_negotiate->peer_cert_digest_len =
7796 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7797
Gilles Peskine449bd832023-01-11 14:50:10 +01007798 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007799}
7800
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007801MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007802static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7803 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007804{
7805 unsigned char *end = start + len;
7806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7807
7808 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7810 ret = mbedtls_pk_parse_subpubkey(&start, end,
7811 &ssl->handshake->peer_pubkey);
7812 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007813 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007815 }
7816
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007818}
7819#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7820
Gilles Peskine449bd832023-01-11 14:50:10 +01007821int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007822{
7823 int ret = 0;
7824 int crt_expected;
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +02007825 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007826#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7827 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7828 ? ssl->handshake->sni_authmode
7829 : ssl->conf->authmode;
7830#else
7831 const int authmode = ssl->conf->authmode;
7832#endif
7833 void *rs_ctx = NULL;
7834 mbedtls_x509_crt *chain = NULL;
7835
Gilles Peskine449bd832023-01-11 14:50:10 +01007836 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007837
Gilles Peskine449bd832023-01-11 14:50:10 +01007838 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7839 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7840 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007841 goto exit;
7842 }
7843
7844#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 if (ssl->handshake->ecrs_enabled &&
7846 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007847 chain = ssl->handshake->ecrs_peer_cert;
7848 ssl->handshake->ecrs_peer_cert = NULL;
7849 goto crt_verify;
7850 }
7851#endif
7852
Gilles Peskine449bd832023-01-11 14:50:10 +01007853 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007854 /* mbedtls_ssl_read_record may have sent an alert already. We
7855 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007857 goto exit;
7858 }
7859
7860#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007862 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007865 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 }
Jerry Yud9526692022-02-17 14:23:47 +08007867
7868 goto exit;
7869 }
7870#endif /* MBEDTLS_SSL_SRV_C */
7871
7872 /* Clear existing peer CRT structure in case we tried to
7873 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007875
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7877 if (chain == NULL) {
7878 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7879 sizeof(mbedtls_x509_crt)));
7880 mbedtls_ssl_send_alert_message(ssl,
7881 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7882 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007883
7884 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7885 goto exit;
7886 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007888
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 ret = ssl_parse_certificate_chain(ssl, chain);
7890 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007891 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 }
Jerry Yud9526692022-02-17 14:23:47 +08007893
7894#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007896 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 }
Jerry Yud9526692022-02-17 14:23:47 +08007898
7899crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007901 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 }
Jerry Yud9526692022-02-17 14:23:47 +08007903#endif
7904
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +02007905 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
7906 ssl->handshake->ciphersuite_info,
7907 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007909 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 }
Jerry Yud9526692022-02-17 14:23:47 +08007911
7912#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7913 {
7914 unsigned char *crt_start, *pk_start;
7915 size_t crt_len, pk_len;
7916
7917 /* We parse the CRT chain without copying, so
7918 * these pointers point into the input buffer,
7919 * and are hence still valid after freeing the
7920 * CRT chain. */
7921
7922 crt_start = chain->raw.p;
7923 crt_len = chain->raw.len;
7924
7925 pk_start = chain->pk_raw.p;
7926 pk_len = chain->pk_raw.len;
7927
7928 /* Free the CRT structures before computing
7929 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 mbedtls_x509_crt_free(chain);
7931 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007932 chain = NULL;
7933
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7935 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007936 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 }
Jerry Yud9526692022-02-17 14:23:47 +08007938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7940 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007941 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 }
Jerry Yud9526692022-02-17 14:23:47 +08007943 }
7944#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7945 /* Pass ownership to session structure. */
7946 ssl->session_negotiate->peer_cert = chain;
7947 chain = NULL;
7948#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7949
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007951
7952exit:
7953
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007955 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 }
Jerry Yud9526692022-02-17 14:23:47 +08007957
7958#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007960 ssl->handshake->ecrs_peer_cert = chain;
7961 chain = NULL;
7962 }
7963#endif
7964
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 if (chain != NULL) {
7966 mbedtls_x509_crt_free(chain);
7967 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007968 }
7969
Gilles Peskine449bd832023-01-11 14:50:10 +01007970 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007971}
7972#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7973
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007974static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
7975 unsigned char *padbuf, size_t hlen,
7976 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007977{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00007978 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007979 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007980#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007981 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007982 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007983 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007984 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007985#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007987 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007988 mbedtls_md_context_t cloned_ctx;
7989 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007990#endif
7991
7992 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007994 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007998 ? "client finished"
7999 : "server finished";
8000
8001#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008002 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008003
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008004 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008006 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008007 }
8008
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008009 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008011 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08008012 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008013 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008014#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008015 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08008016
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008017 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008018 if (ret != 0) {
8019 goto exit;
8020 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008021 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01008022 if (ret != 0) {
8023 goto exit;
8024 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08008025
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008026 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008027 if (ret != 0) {
8028 goto exit;
8029 }
8030#endif /* MBEDTLS_USE_PSA_CRYPTO */
8031
8032 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
8033
Jerry Yu615bd6f2022-02-17 14:25:15 +08008034 /*
8035 * TLSv1.2:
8036 * hash = PRF( master, finished_label,
8037 * Hash( handshake ) )[0.11]
8038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008040 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008041
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008043
Paul Elliottecb95be2023-08-11 16:41:04 +01008044 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08008045
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008046 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008047
8048exit:
8049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008050 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02008051 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008052#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02008053 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01008054 return ret;
8055#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08008056}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008057
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008058#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008059static int ssl_calc_finished_tls_sha256(
8060 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
8061{
8062 unsigned char padbuf[32];
8063 return ssl_calc_finished_tls_generic(ssl,
8064#if defined(MBEDTLS_USE_PSA_CRYPTO)
8065 &ssl->handshake->fin_sha256_psa,
8066#else
8067 &ssl->handshake->fin_sha256,
8068#endif
8069 padbuf, sizeof(padbuf),
8070 buf, from);
8071}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008072#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08008073
Jerry Yub7ba49e2022-02-17 14:25:53 +08008074
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008075#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01008076static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08008078{
Jerry Yub7ba49e2022-02-17 14:25:53 +08008079 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008080 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008082 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008083#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008084 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08008085#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02008086 padbuf, sizeof(padbuf),
8087 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08008088}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008089#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08008090
Gilles Peskine449bd832023-01-11 14:50:10 +01008091void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08008092{
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008094
8095 /*
8096 * Free our handshake params
8097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 mbedtls_ssl_handshake_free(ssl);
8099 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08008100 ssl->handshake = NULL;
8101
8102 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008103 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08008104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 if (ssl->transform) {
8106 mbedtls_ssl_transform_free(ssl->transform);
8107 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08008108 }
8109 ssl->transform = ssl->transform_negotiate;
8110 ssl->transform_negotiate = NULL;
8111
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08008113}
8114
Gilles Peskine449bd832023-01-11 14:50:10 +01008115void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08008116{
8117 int resume = ssl->handshake->resume;
8118
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008120
8121#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008123 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
8124 ssl->renego_records_seen = 0;
8125 }
8126#endif
8127
8128 /*
8129 * Free the previous session and switch in the current one
8130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008132#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8133 /* RFC 7366 3.1: keep the EtM state */
8134 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008136#endif
8137
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 mbedtls_ssl_session_free(ssl->session);
8139 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008140 }
8141 ssl->session = ssl->session_negotiate;
8142 ssl->session_negotiate = NULL;
8143
8144 /*
8145 * Add cache entry
8146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008147 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08008148 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 resume == 0) {
8150 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
8151 ssl->session->id,
8152 ssl->session->id_len,
8153 ssl->session) != 0) {
8154 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
8155 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08008156 }
8157
8158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8160 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08008161 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008163
8164 /* Keep last flight around in case we need to resend it:
8165 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
8167 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08008168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08008170
Jerry Yu5ed73ff2022-10-27 13:08:42 +08008171 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08008172
Gilles Peskine449bd832023-01-11 14:50:10 +01008173 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08008174}
8175
Gilles Peskine449bd832023-01-11 14:50:10 +01008176int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008177{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00008178 int ret;
8179 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008182
Gilles Peskine449bd832023-01-11 14:50:10 +01008183 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008184
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008185 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
8186 if (ret != 0) {
8187 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8188 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008189
8190 /*
8191 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8192 * may define some other value. Currently (early 2016), no defined
8193 * ciphersuite does this (and this is unlikely to change as activity has
8194 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8195 */
8196 hash_len = 12;
8197
8198#if defined(MBEDTLS_SSL_RENEGOTIATION)
8199 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008201#endif
8202
8203 ssl->out_msglen = 4 + hash_len;
8204 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8205 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
8206
8207 /*
8208 * In case of session resuming, invert the client and server
8209 * ChangeCipherSpec messages order.
8210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008212#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008214 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008216#endif
8217#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008219 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008221#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008222 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008223 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008225
8226 /*
8227 * Switch to our negotiated transform and session parameters for outbound
8228 * data.
8229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008231
8232#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008234 unsigned char i;
8235
8236 /* Remember current epoch settings for resending */
8237 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8239 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008240
8241 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008243
8244
8245 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 for (i = 2; i > 0; i--) {
8247 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008248 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 }
8250 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008251
8252 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 if (i == 0) {
8254 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8255 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008256 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008258#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008260
8261 ssl->transform_out = ssl->transform_negotiate;
8262 ssl->session_out = ssl->session_negotiate;
8263
8264#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008265 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8266 mbedtls_ssl_send_flight_completed(ssl);
8267 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008268#endif
8269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8271 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8272 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008273 }
8274
8275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8277 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8278 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8279 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008280 }
8281#endif
8282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008286}
8287
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008288#define SSL_MAX_HASH_LEN 12
8289
Gilles Peskine449bd832023-01-11 14:50:10 +01008290int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008291{
8292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8293 unsigned int hash_len = 12;
8294 unsigned char buf[SSL_MAX_HASH_LEN];
8295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008297
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008298 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8299 if (ret != 0) {
8300 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8301 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8304 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008305 goto exit;
8306 }
8307
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8309 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8310 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8311 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008312 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8313 goto exit;
8314 }
8315
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8317 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8318 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008319 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8320 goto exit;
8321 }
8322
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8324 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8325 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8326 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008327 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8328 goto exit;
8329 }
8330
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8332 buf, hash_len) != 0) {
8333 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8334 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8335 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008336 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8337 goto exit;
8338 }
8339
8340#if defined(MBEDTLS_SSL_RENEGOTIATION)
8341 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008343#endif
8344
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008346#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008348 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008350#endif
8351#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008353 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008357 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008359
8360#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8362 mbedtls_ssl_recv_flight_completed(ssl);
8363 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008364#endif
8365
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008367
8368exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 mbedtls_platform_zeroize(buf, hash_len);
8370 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008371}
8372
Jerry Yu392112c2022-02-17 14:34:10 +08008373#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8374/*
8375 * Helper to get TLS 1.2 PRF from ciphersuite
8376 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008378static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008379{
Jerry Yu392112c2022-02-17 14:34:10 +08008380 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008382#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8384 return tls_prf_sha384;
8385 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008386#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008387#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008388 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8390 return tls_prf_sha256;
8391 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008392 }
8393#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008394#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008395 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008396 (void) ciphersuite_info;
8397#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008398
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008400}
8401#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008402
Gilles Peskine449bd832023-01-11 14:50:10 +01008403static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008404{
8405 ((void) tls_prf);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01008406#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if (tls_prf == tls_prf_sha384) {
8408 return MBEDTLS_SSL_TLS_PRF_SHA384;
8409 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008410#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01008411#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 if (tls_prf == tls_prf_sha256) {
8413 return MBEDTLS_SSL_TLS_PRF_SHA256;
8414 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008415#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008417}
8418
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008419/*
8420 * Populate a transform structure with session keys and all the other
8421 * necessary information.
8422 *
8423 * Parameters:
8424 * - [in/out]: transform: structure to populate
8425 * [in] must be just initialised with mbedtls_ssl_transform_init()
8426 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8427 * - [in] ciphersuite
8428 * - [in] master
8429 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008430 * - [in] tls_prf: pointer to PRF to use for key derivation
8431 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008432 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008433 * - [in] endpoint: client or server
8434 * - [in] ssl: used for:
8435 * - ssl->conf->{f,p}_export_keys
8436 * [in] optionally used for:
8437 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8438 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008439MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008440static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8441 int ciphersuite,
8442 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008443#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008445#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 ssl_tls_prf_t tls_prf,
8447 const unsigned char randbytes[64],
8448 mbedtls_ssl_protocol_version tls_version,
8449 unsigned endpoint,
8450 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008451{
8452 int ret = 0;
8453 unsigned char keyblk[256];
8454 unsigned char *key1;
8455 unsigned char *key2;
8456 unsigned char *mac_enc;
8457 unsigned char *mac_dec;
8458 size_t mac_key_len = 0;
8459 size_t iv_copy_len;
8460 size_t keylen;
8461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008462 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008463#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008464 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008465 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008466#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008467
8468#if defined(MBEDTLS_USE_PSA_CRYPTO)
8469 psa_key_type_t key_type;
8470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8471 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008472 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008473 size_t key_bits;
8474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8475#endif
8476
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008477 /*
8478 * Some data just needs copying into the structure
8479 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008480#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008481 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008482#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008483 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008484
8485#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008487#endif
8488
8489#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008490 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008491 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8492 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008494 }
8495#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8496
8497 /*
8498 * Get various info structures
8499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8501 if (ciphersuite_info == NULL) {
8502 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8503 ciphersuite));
8504 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008505 }
8506
Neil Armstrongab555e02022-04-04 11:07:59 +02008507 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008508#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008510#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008514 transform->taglen =
8515 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008517
8518#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008519 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 transform->taglen,
8521 &alg,
8522 &key_type,
8523 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008524 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008526 goto end;
8527 }
8528#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008529 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008530 if (cipher_info == NULL) {
8531 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8532 ciphersuite_info->cipher));
8533 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008534 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008535#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008536
Neil Armstronge4512952022-03-08 09:08:22 +01008537#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008538 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008540 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 (unsigned) ciphersuite_info->mac));
8542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008543 }
8544#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008545 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 if (md_info == NULL) {
8547 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8548 (unsigned) ciphersuite_info->mac));
8549 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008550 }
Neil Armstronge4512952022-03-08 09:08:22 +01008551#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008552
8553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8554 /* Copy own and peer's CID if the use of the CID
8555 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8557 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008558
8559 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8561 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8562 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008563
8564 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8566 ssl->handshake->peer_cid_len);
8567 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8568 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008569 }
8570#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8571
8572 /*
8573 * Compute key block using the PRF
8574 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8576 if (ret != 0) {
8577 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8578 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008579 }
8580
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8582 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8583 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8584 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8585 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008586
8587 /*
8588 * Determine the appropriate key, IV and MAC length.
8589 */
8590
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008591#if defined(MBEDTLS_USE_PSA_CRYPTO)
8592 keylen = PSA_BITS_TO_BYTES(key_bits);
8593#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008595#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008596
Valerio Settie5707042023-10-11 11:54:42 +02008597#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008599 size_t explicit_ivlen;
8600
8601 transform->maclen = 0;
8602 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008603
8604 /* All modes haves 96-bit IVs, but the length of the static parts vary
8605 * with mode and version:
8606 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8607 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8608 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8609 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8610 * sequence number).
8611 */
8612 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008613
8614 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008617#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008618 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8619 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008620#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008623 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008625 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008627
8628 /* Minimum length of encrypted record */
8629 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8630 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 } else
Valerio Settie5707042023-10-11 11:54:42 +02008632#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008633#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008635 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008637#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008639#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008640 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008641#endif /* MBEDTLS_USE_PSA_CRYPTO */
8642
8643#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008644 /* Get MAC length */
8645 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8646#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008647 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8649 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8650 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008651 goto end;
8652 }
8653
8654 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008656#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008657 transform->maclen = mac_key_len;
8658
8659 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008660#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008662#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008663 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008664#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008665
8666 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008668 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008670 /*
8671 * GenericBlockCipher:
8672 * 1. if EtM is in use: one block plus MAC
8673 * otherwise: * first multiple of blocklen greater than maclen
8674 * 2. IV
8675 */
8676#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008678 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 + block_size;
8680 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008681#endif
8682 {
8683 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008684 + block_size
8685 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008686 }
8687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008689 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 } else {
8691 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008692 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8693 goto end;
8694 }
8695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008697#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8698 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8700 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008701 }
8702
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8704 (unsigned) keylen,
8705 (unsigned) transform->minlen,
8706 (unsigned) transform->ivlen,
8707 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008708
8709 /*
8710 * Finally setup the cipher contexts, IVs and MAC secrets.
8711 */
8712#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008714 key1 = keyblk + mac_key_len * 2;
8715 key2 = keyblk + mac_key_len * 2 + keylen;
8716
8717 mac_enc = keyblk;
8718 mac_dec = keyblk + mac_key_len;
8719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 iv_copy_len = (transform->fixed_ivlen) ?
8721 transform->fixed_ivlen : transform->ivlen;
8722 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8723 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8724 iv_copy_len);
8725 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008726#endif /* MBEDTLS_SSL_CLI_C */
8727#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008729 key1 = keyblk + mac_key_len * 2 + keylen;
8730 key2 = keyblk + mac_key_len * 2;
8731
8732 mac_enc = keyblk + mac_key_len;
8733 mac_dec = keyblk;
8734
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 iv_copy_len = (transform->fixed_ivlen) ?
8736 transform->fixed_ivlen : transform->ivlen;
8737 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8738 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8739 iv_copy_len);
8740 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008741#endif /* MBEDTLS_SSL_SRV_C */
8742 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008744 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8745 goto end;
8746 }
8747
Paul Elliott4fb19552023-10-18 12:15:30 +01008748 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 ssl->f_export_keys(ssl->p_export_keys,
8750 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8751 master, 48,
8752 randbytes + 32,
8753 randbytes,
8754 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008755 }
8756
8757#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008758 transform->psa_alg = alg;
8759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8761 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8762 psa_set_key_algorithm(&attributes, alg);
8763 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if ((status = psa_import_key(&attributes,
8766 key1,
8767 PSA_BITS_TO_BYTES(key_bits),
8768 &transform->psa_key_enc)) != PSA_SUCCESS) {
8769 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008770 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008772 goto end;
8773 }
8774
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008776
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if ((status = psa_import_key(&attributes,
8778 key2,
8779 PSA_BITS_TO_BYTES(key_bits),
8780 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008781 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008783 goto end;
8784 }
8785 }
8786#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8788 cipher_info)) != 0) {
8789 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008790 goto end;
8791 }
8792
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8794 cipher_info)) != 0) {
8795 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008796 goto end;
8797 }
8798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8800 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8801 MBEDTLS_ENCRYPT)) != 0) {
8802 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008803 goto end;
8804 }
8805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8807 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8808 MBEDTLS_DECRYPT)) != 0) {
8809 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008810 goto end;
8811 }
8812
8813#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8815 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8816 MBEDTLS_PADDING_NONE)) != 0) {
8817 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008818 goto end;
8819 }
8820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8822 MBEDTLS_PADDING_NONE)) != 0) {
8823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008824 goto end;
8825 }
8826 }
8827#endif /* MBEDTLS_CIPHER_MODE_CBC */
8828#endif /* MBEDTLS_USE_PSA_CRYPTO */
8829
Neil Armstrong29c0c042022-03-17 17:47:28 +01008830#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8831 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8832 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008834#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8838 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8839 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 if ((status = psa_import_key(&attributes,
8842 mac_enc, mac_key_len,
8843 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008844 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008846 goto end;
8847 }
8848
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8850 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008851#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008853#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008855 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8857 PSA_KEY_USAGE_VERIFY_HASH);
8858 } else {
8859 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8860 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 if ((status = psa_import_key(&attributes,
8863 mac_dec, mac_key_len,
8864 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008865 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008866 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008867 goto end;
8868 }
8869#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8871 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008872 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 }
8874 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8875 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008876 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008878#endif /* MBEDTLS_USE_PSA_CRYPTO */
8879 }
8880#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8881
8882 ((void) mac_dec);
8883 ((void) mac_enc);
8884
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008885end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8887 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008888}
8889
Valerio Settia08b1a42022-11-17 15:10:02 +01008890#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8891 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008892int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 psa_pake_operation_t *pake_ctx,
8894 const unsigned char *buf,
8895 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008896{
8897 psa_status_t status;
8898 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008899 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008900 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8901 * At round two perform a single cycle
8902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 for (; remaining_steps > 0; remaining_steps--) {
8906 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008907 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008909 /* Length is stored at the first byte */
8910 size_t length = buf[input_offset];
8911 input_offset += 1;
8912
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008914 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8915 }
8916
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 status = psa_pake_input(pake_ctx, step,
8918 buf + input_offset, length);
8919 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008920 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008921 }
8922
8923 input_offset += length;
8924 }
8925 }
8926
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008928 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008930
Gilles Peskine449bd832023-01-11 14:50:10 +01008931 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008932}
8933
Valerio Setti6b3dab02022-11-17 17:14:54 +01008934int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 psa_pake_operation_t *pake_ctx,
8936 unsigned char *buf,
8937 size_t len, size_t *olen,
8938 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008939{
8940 psa_status_t status;
8941 size_t output_offset = 0;
8942 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008943 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008944 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8945 * At round two perform a single cycle
8946 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008947 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008948
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 for (; remaining_steps > 0; remaining_steps--) {
8950 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8951 step <= PSA_PAKE_STEP_ZK_PROOF;
8952 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008953 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008954 * For each step, prepend 1 byte with the length of the data as
8955 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 status = psa_pake_output(pake_ctx, step,
8958 buf + output_offset + 1,
8959 len - output_offset - 1,
8960 &output_len);
8961 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008962 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008963 }
8964
Valerio Setti99d88c12022-11-22 16:03:43 +01008965 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008966
8967 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008968 }
8969 }
8970
8971 *olen = output_offset;
8972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008974}
Valerio Settia08b1a42022-11-17 15:10:02 +01008975#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8976
Jerry Yuee40f9d2022-02-17 14:55:16 +08008977#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008978int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8979 unsigned char *hash, size_t *hashlen,
8980 unsigned char *data, size_t data_len,
8981 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008982{
8983 psa_status_t status;
8984 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008985 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008986
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008988
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 if ((status = psa_hash_setup(&hash_operation,
8990 hash_alg)) != PSA_SUCCESS) {
8991 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008992 goto exit;
8993 }
8994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8996 64)) != PSA_SUCCESS) {
8997 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008998 goto exit;
8999 }
9000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if ((status = psa_hash_update(&hash_operation,
9002 data, data_len)) != PSA_SUCCESS) {
9003 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009004 goto exit;
9005 }
9006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
9008 hashlen)) != PSA_SUCCESS) {
9009 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
9010 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009011 }
9012
9013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 if (status != PSA_SUCCESS) {
9015 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9016 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9017 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08009018 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009020 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
9021 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009023 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009025 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009027 }
9028 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009030}
9031
9032#else
9033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
9035 unsigned char *hash, size_t *hashlen,
9036 unsigned char *data, size_t data_len,
9037 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08009038{
9039 int ret = 0;
9040 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
9042 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08009045
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009047
9048 /*
9049 * digitally-signed struct {
9050 * opaque client_random[32];
9051 * opaque server_random[32];
9052 * ServerDHParams params;
9053 * };
9054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
9056 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009057 goto exit;
9058 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
9060 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009061 goto exit;
9062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
9064 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009065 goto exit;
9066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
9068 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009069 goto exit;
9070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
9072 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009073 goto exit;
9074 }
9075
9076exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08009078
Gilles Peskine449bd832023-01-11 14:50:10 +01009079 if (ret != 0) {
9080 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9081 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
9082 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08009083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08009085}
9086#endif /* MBEDTLS_USE_PSA_CRYPTO */
9087
Jerry Yud9d91da2022-02-17 14:57:06 +08009088#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
9089
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009090/* Find the preferred hash for a given signature algorithm. */
9091unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 mbedtls_ssl_context *ssl,
9093 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08009094{
Gabor Mezei078e8032022-04-27 21:17:56 +02009095 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02009096 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02009097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
9099 return MBEDTLS_SSL_HASH_NONE;
9100 }
Gabor Mezei078e8032022-04-27 21:17:56 +02009101
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009103 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
9105 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009106 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
9108 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009109
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009110 mbedtls_md_type_t md_alg =
9111 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
9112 if (md_alg == MBEDTLS_MD_NONE) {
9113 continue;
9114 }
9115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02009119 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02009120 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009121
Gilles Peskine449bd832023-01-11 14:50:10 +01009122 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
9123 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9124 PSA_ALG_ECDSA(psa_hash_alg),
9125 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009126 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009128
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
9130 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
9131 PSA_ALG_RSA_PKCS1V15_SIGN(
9132 psa_hash_alg),
9133 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009134 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009136 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009137#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02009138
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02009140 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009141 }
Jerry Yud9d91da2022-02-17 14:57:06 +08009142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08009144}
9145
9146#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
9147
Jerry Yudc7bd172022-02-17 13:44:15 +08009148#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9149
XiaokangQian75d40ef2022-04-20 11:05:24 +00009150int mbedtls_ssl_validate_ciphersuite(
9151 const mbedtls_ssl_context *ssl,
9152 const mbedtls_ssl_ciphersuite_t *suite_info,
9153 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009155{
9156 (void) ssl;
9157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (suite_info == NULL) {
9159 return -1;
9160 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009161
Gilles Peskine449bd832023-01-11 14:50:10 +01009162 if ((suite_info->min_tls_version > max_tls_version) ||
9163 (suite_info->max_tls_version < min_tls_version)) {
9164 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009165 }
9166
XiaokangQian060d8672022-04-21 09:24:56 +00009167#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009168#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9171 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009172#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9174 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009175#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009176 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009177 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009178 }
9179#endif
9180
9181 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9182#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9184 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9185 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009186 }
9187#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9188#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9189
Gilles Peskine449bd832023-01-11 14:50:10 +01009190 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009191}
9192
Ronald Crone68ab4f2022-10-05 12:46:29 +02009193#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009194/*
9195 * Function for writing a signature algorithm extension.
9196 *
9197 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9198 * value (TLS 1.3 RFC8446):
9199 * enum {
9200 * ....
9201 * ecdsa_secp256r1_sha256( 0x0403 ),
9202 * ecdsa_secp384r1_sha384( 0x0503 ),
9203 * ecdsa_secp521r1_sha512( 0x0603 ),
9204 * ....
9205 * } SignatureScheme;
9206 *
9207 * struct {
9208 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9209 * } SignatureSchemeList;
9210 *
9211 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9212 * value (TLS 1.2 RFC5246):
9213 * enum {
9214 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9215 * sha512(6), (255)
9216 * } HashAlgorithm;
9217 *
9218 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9219 * SignatureAlgorithm;
9220 *
9221 * struct {
9222 * HashAlgorithm hash;
9223 * SignatureAlgorithm signature;
9224 * } SignatureAndHashAlgorithm;
9225 *
9226 * SignatureAndHashAlgorithm
9227 * supported_signature_algorithms<2..2^16-2>;
9228 *
9229 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9230 * generalization of the TLS 1.2 signature algorithm extension.
9231 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9232 * `SignatureScheme` field of TLS 1.3
9233 *
9234 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009235int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9236 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009237{
9238 unsigned char *p = buf;
9239 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9240 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9241
9242 *out_len = 0;
9243
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009245
9246 /* Check if we have space for header and length field:
9247 * - extension_type (2 bytes)
9248 * - extension_data_length (2 bytes)
9249 * - supported_signature_algorithms_length (2 bytes)
9250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009252 p += 6;
9253
9254 /*
9255 * Write supported_signature_algorithms
9256 */
9257 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9259 if (sig_alg == NULL) {
9260 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9261 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009262
Gilles Peskine449bd832023-01-11 14:50:10 +01009263 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9264 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9265 *sig_alg,
9266 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9267 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009268 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 }
9270 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9271 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009272 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9274 *sig_alg,
9275 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009276 }
9277
9278 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009279 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 if (supported_sig_alg_len == 0) {
9281 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9282 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009283 }
9284
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9286 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9287 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009288
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009289 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009290
9291#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009293#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009294
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009296}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009297#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009298
XiaokangQian40a35232022-05-07 09:02:40 +00009299#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009300/*
9301 * mbedtls_ssl_parse_server_name_ext
9302 *
9303 * Structure of server_name extension:
9304 *
9305 * enum {
9306 * host_name(0), (255)
9307 * } NameType;
9308 * opaque HostName<1..2^16-1>;
9309 *
9310 * struct {
9311 * NameType name_type;
9312 * select (name_type) {
9313 * case host_name: HostName;
9314 * } name;
9315 * } ServerName;
9316 * struct {
9317 * ServerName server_name_list<1..2^16-1>
9318 * } ServerNameList;
9319 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009320MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009321int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9322 const unsigned char *buf,
9323 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009324{
9325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9326 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009327 size_t server_name_list_len, hostname_len;
9328 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009331
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9333 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009334 p += 2;
9335
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009337 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 while (p < server_name_list_end) {
9339 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9340 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9341 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9342 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009345 /* sni_name is intended to be used only during the parsing of the
9346 * ClientHello message (it is reset to NULL before the end of
9347 * the message parsing). Thus it is ok to just point to the
9348 * reception buffer and not make a copy of it.
9349 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009350 ssl->handshake->sni_name = p + 3;
9351 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 if (ssl->conf->f_sni == NULL) {
9353 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009354 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9356 ssl, p + 3, hostname_len);
9357 if (ret != 0) {
9358 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9359 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9360 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9361 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9362 }
9363 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009364 }
9365
9366 p += hostname_len + 3;
9367 }
9368
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009370}
9371#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9372
XiaokangQianacb39922022-06-17 10:18:48 +00009373#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009374MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009375int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9376 const unsigned char *buf,
9377 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009378{
9379 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009380 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009381 const unsigned char *protocol_name_list;
9382 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009383 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009384
9385 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009386 if (ssl->conf->alpn_list == NULL) {
9387 return 0;
9388 }
XiaokangQianacb39922022-06-17 10:18:48 +00009389
9390 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009391 * RFC7301, section 3.1
9392 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009393 *
XiaokangQianc7403452022-06-23 03:24:12 +00009394 * struct {
9395 * ProtocolName protocol_name_list<2..2^16-1>
9396 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009397 */
9398
XiaokangQianc7403452022-06-23 03:24:12 +00009399 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009400 * protocol_name_list_len 2 bytes
9401 * protocol_name_len 1 bytes
9402 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009405
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009407 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009409 protocol_name_list = p;
9410 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009411
9412 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009414 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9416 protocol_name_len);
9417 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009418 MBEDTLS_SSL_PEND_FATAL_ALERT(
9419 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9421 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009422 }
9423
9424 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009425 }
9426
9427 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9429 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009430 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009432 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 if (protocol_name_len == alpn_len &&
9434 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009435 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009437 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009438
9439 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009440 }
9441 }
9442
XiaokangQian95d5f542022-06-24 02:29:26 +00009443 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009444 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9446 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9447 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009448}
9449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9451 unsigned char *buf,
9452 unsigned char *end,
9453 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009454{
9455 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009456 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009457 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009458
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 if (ssl->alpn_chosen == NULL) {
9460 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009461 }
9462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 protocol_name_len = strlen(ssl->alpn_chosen);
9464 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009467 /*
9468 * 0 . 1 ext identifier
9469 * 2 . 3 ext length
9470 * 4 . 5 protocol list length
9471 * 6 . 6 protocol name length
9472 * 7 . 7+n protocol name
9473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009475
XiaokangQian95d5f542022-06-24 02:29:26 +00009476 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009477
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9479 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009480 /* Note: the length of the chosen protocol has been checked to be less
9481 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009484
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009486
9487#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009489#endif
9490
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009492}
9493#endif /* MBEDTLS_SSL_ALPN */
9494
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009495#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009496 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009497 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009498 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009499int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9500 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009501{
9502 /* Initialize to suppress unnecessary compiler warning */
9503 size_t hostname_len = 0;
9504
9505 /* Check if new hostname is valid before
9506 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 if (hostname != NULL) {
9508 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9511 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9512 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009513 }
9514
9515 /* Now it's clear that we will overwrite the old hostname,
9516 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009518 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009520 }
9521
9522 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009524 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 } else {
9526 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9527 if (session->hostname == NULL) {
9528 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9529 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009532 }
9533
Gilles Peskine449bd832023-01-11 14:50:10 +01009534 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009535}
Xiaokang Qian03409292022-10-12 02:49:52 +00009536#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9537 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009538 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009539 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009540
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009541#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
9542 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009543int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
9544 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009545{
9546 size_t alpn_len = 0;
9547
9548 if (alpn != NULL) {
9549 alpn_len = strlen(alpn);
9550
9551 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
9552 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9553 }
9554 }
9555
9556 if (session->ticket_alpn != NULL) {
9557 mbedtls_zeroize_and_free(session->ticket_alpn,
9558 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009559 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009560 }
9561
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009562 if (alpn != NULL) {
9563 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009564 if (session->ticket_alpn == NULL) {
9565 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9566 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00009567 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00009568 }
9569
9570 return 0;
9571}
9572#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009573
9574/*
9575 * The following functions are used by 1.2 and 1.3, client and server.
9576 */
9577#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
9578int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
9579 const mbedtls_ssl_ciphersuite_t *ciphersuite,
9580 int recv_endpoint,
9581 mbedtls_ssl_protocol_version tls_version,
9582 uint32_t *flags)
9583{
9584 int ret = 0;
9585 unsigned int usage = 0;
9586 const char *ext_oid;
9587 size_t ext_len;
9588
9589 /*
9590 * keyUsage
9591 */
9592
9593 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
9594 * to check what a compliant client will think while choosing which cert
9595 * to send to the client. */
9596#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
9597 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9598 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9599 /* TLS 1.2 server part of the key exchange */
9600 switch (ciphersuite->key_exchange) {
9601 case MBEDTLS_KEY_EXCHANGE_RSA:
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02009602 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
9603 break;
9604
9605 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
9606 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
9607 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
9608 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9609 break;
9610
9611 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
9612 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
9613 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
9614 break;
9615
9616 /* Don't use default: we want warnings when adding new values */
9617 case MBEDTLS_KEY_EXCHANGE_NONE:
9618 case MBEDTLS_KEY_EXCHANGE_PSK:
9619 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
9620 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
9621 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
9622 usage = 0;
9623 }
9624 } else
9625#endif
9626 {
9627 /* This is either TLS 1.3 authentication, which always uses signatures,
9628 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
9629 * options we implement, both using signatures. */
9630 (void) tls_version;
9631 (void) ciphersuite;
9632 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
9633 }
9634
9635 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
9636 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
9637 ret = -1;
9638 }
9639
9640 /*
9641 * extKeyUsage
9642 */
9643
9644 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
9645 ext_oid = MBEDTLS_OID_SERVER_AUTH;
9646 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
9647 } else {
9648 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
9649 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
9650 }
9651
9652 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
9653 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
9654 ret = -1;
9655 }
9656
9657 return ret;
9658}
9659
9660int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
9661 int authmode,
9662 mbedtls_x509_crt *chain,
9663 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
9664 void *rs_ctx)
9665{
9666 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
9667 return 0;
9668 }
9669
9670 /*
9671 * Primary check: use the appropriate X.509 verification function
9672 */
9673 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
9674 void *p_vrfy;
9675 if (ssl->f_vrfy != NULL) {
9676 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
9677 f_vrfy = ssl->f_vrfy;
9678 p_vrfy = ssl->p_vrfy;
9679 } else {
9680 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
9681 f_vrfy = ssl->conf->f_vrfy;
9682 p_vrfy = ssl->conf->p_vrfy;
9683 }
9684
9685 int ret = 0;
9686 int have_ca_chain_or_callback = 0;
9687#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
9688 if (ssl->conf->f_ca_cb != NULL) {
9689 ((void) rs_ctx);
9690 have_ca_chain_or_callback = 1;
9691
9692 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
9693 ret = mbedtls_x509_crt_verify_with_ca_cb(
9694 chain,
9695 ssl->conf->f_ca_cb,
9696 ssl->conf->p_ca_cb,
9697 ssl->conf->cert_profile,
9698 ssl->hostname,
9699 &ssl->session_negotiate->verify_result,
9700 f_vrfy, p_vrfy);
9701 } else
9702#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
9703 {
9704 mbedtls_x509_crt *ca_chain;
9705 mbedtls_x509_crl *ca_crl;
9706#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
9707 if (ssl->handshake->sni_ca_chain != NULL) {
9708 ca_chain = ssl->handshake->sni_ca_chain;
9709 ca_crl = ssl->handshake->sni_ca_crl;
9710 } else
9711#endif
9712 {
9713 ca_chain = ssl->conf->ca_chain;
9714 ca_crl = ssl->conf->ca_crl;
9715 }
9716
9717 if (ca_chain != NULL) {
9718 have_ca_chain_or_callback = 1;
9719 }
9720
9721 ret = mbedtls_x509_crt_verify_restartable(
9722 chain,
9723 ca_chain, ca_crl,
9724 ssl->conf->cert_profile,
9725 ssl->hostname,
9726 &ssl->session_negotiate->verify_result,
9727 f_vrfy, p_vrfy, rs_ctx);
9728 }
9729
9730 if (ret != 0) {
9731 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9732 }
9733
9734#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9735 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9736 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9737 }
9738#endif
9739
9740 /*
9741 * Secondary checks: always done, but change 'ret' only if it was 0
9742 */
9743
9744 /* With TLS 1.2 and ECC certs, check that the curve used by the
9745 * certificate is on our list of acceptable curves.
9746 *
9747 * With TLS 1.3 this is not needed because the curve is part of the
9748 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9749 * we validate the signature made with the key associated to this cert.
9750 */
9751#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9752 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
9753 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9754 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9755 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9756 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9757 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9758 if (ret == 0) {
9759 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9760 }
9761 }
9762 }
9763#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9764
9765 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9766 if (mbedtls_ssl_check_cert_usage(chain,
9767 ciphersuite_info,
9768 ssl->conf->endpoint,
9769 ssl->tls_version,
9770 &ssl->session_negotiate->verify_result) != 0) {
9771 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9772 if (ret == 0) {
9773 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9774 }
9775 }
9776
9777 /* With authmode optional, we want to keep going if the certificate was
9778 * unacceptable, but still fail on other errors (out of memory etc),
9779 * including fatal errors from the f_vrfy callback.
9780 *
9781 * The only acceptable errors are:
9782 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9783 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9784 * Anything else is a fatal error. */
9785 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9786 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9787 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9788 ret = 0;
9789 }
9790
9791 /* Return a specific error as this is a user error: inconsistent
9792 * configuration - can't verify without trust anchors. */
9793 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9794 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9795 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9796 }
9797
9798 if (ret != 0) {
9799 uint8_t alert;
9800
9801 /* The certificate may have been rejected for several reasons.
9802 Pick one and send the corresponding alert. Which alert to send
9803 may be a subject of debate in some cases. */
9804 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9805 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9806 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9807 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9808 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9809 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9810 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9811 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9812 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9813 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9814 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9815 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9816 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9817 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9818 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9819 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9820 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9821 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9822 } else {
9823 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9824 }
9825 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9826 alert);
9827 }
9828
9829#if defined(MBEDTLS_DEBUG_C)
9830 if (ssl->session_negotiate->verify_result != 0) {
9831 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9832 (unsigned int) ssl->session_negotiate->verify_result));
9833 } else {
9834 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9835 }
9836#endif /* MBEDTLS_DEBUG_C */
9837
9838 return ret;
9839}
9840#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#endif /* MBEDTLS_SSL_TLS_C */