blob: 28bf1d8a0c289b11facf51d4fae43350f6efa22a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * http://www.ietf.org/rfc/rfc2246.txt
9 * http://www.ietf.org/rfc/rfc4346.txt
10 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000015
SimonBd5800b72016-04-26 07:43:27 +010016#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010019#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010020#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000021#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040022
Janos Follath73c616b2019-12-18 15:07:04 +000023#include "mbedtls/debug.h"
24#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010026#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020027#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050031#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010032#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020033#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050035#include "psa/crypto.h"
36#endif
37
Janos Follath23bdca02016-10-07 14:47:14 +010038#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020040#endif
41
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040043/* Define local translating functions to save code size by not using too many
44 * arguments in each translating place. */
45static int local_err_translation(psa_status_t status)
46{
47 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040048 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040049 psa_generic_status_to_mbedtls);
50}
51#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050052#endif
53
Ronald Cronad8c17b2022-06-10 17:18:09 +020054#if defined(MBEDTLS_TEST_HOOKS)
55static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
56
57void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010058 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020059{
60 chk_buf_ptr_fail_args.cur = cur;
61 chk_buf_ptr_fail_args.end = end;
62 chk_buf_ptr_fail_args.need = need;
63}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020066{
Gilles Peskine449bd832023-01-11 14:50:10 +010067 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020068}
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020071{
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return (chk_buf_ptr_fail_args.cur != args->cur) ||
73 (chk_buf_ptr_fail_args.end != args->end) ||
74 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020075}
76#endif /* MBEDTLS_TEST_HOOKS */
77
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010079
Hanno Beckera0e20d02019-05-15 14:03:01 +010080#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010081/* Top-level Connection ID API */
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
84 size_t len,
85 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010086{
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
88 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
89 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
92 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
93 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010094 }
95
96 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010097 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
102 int enable,
103 unsigned char const *own_cid,
104 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
108 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100109
Hanno Beckerca092242019-04-25 16:01:49 +0100110 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (enable == MBEDTLS_SSL_CID_DISABLED) {
112 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
113 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100114 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
116 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (own_cid_len != ssl->conf->cid_len) {
119 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
120 (unsigned) own_cid_len,
121 (unsigned) ssl->conf->cid_len));
122 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100123 }
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100126 /* Truncation is not an issue here because
127 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
128 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100131}
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
134 int *enabled,
135 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
136 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000137{
138 *enabled = MBEDTLS_SSL_CID_DISABLED;
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
142 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000143
144 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
145 * zero as this is indistinguishable from not requesting to use
146 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
148 return 0;
149 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000152 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (own_cid != NULL) {
154 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
155 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000156 }
157
158 *enabled = MBEDTLS_SSL_CID_ENABLED;
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000161}
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
164 int *enabled,
165 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
166 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
171 mbedtls_ssl_is_handshake_over(ssl) == 0) {
172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100173 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100174
Hanno Beckerc5f24222019-05-03 12:54:52 +0100175 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
176 * were used, but client and server requested the empty CID.
177 * This is indistinguishable from not using the CID extension
178 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (ssl->transform_in->in_cid_len == 0 &&
180 ssl->transform_in->out_cid_len == 0) {
181 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100185 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (peer_cid != NULL) {
187 memcpy(peer_cid, ssl->transform_in->out_cid,
188 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100189 }
190 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100191
192 *enabled = MBEDTLS_SSL_CID_ENABLED;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100195}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200201/*
202 * Convert max_fragment_length codes to length.
203 * RFC 6066 says:
204 * enum{
205 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
206 * } MaxFragmentLength;
207 * and we add 0 -> extension unused
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200210{
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 switch (mfl) {
212 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
213 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
214 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
215 return 512;
216 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
217 return 1024;
218 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
219 return 2048;
220 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
221 return 4096;
222 default:
223 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000224 }
225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
229 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200230{
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_ssl_session_free(dst);
232 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800233#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
234 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000235#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
236 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000237 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800238#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000239#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000242
243#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
248 if (dst->peer_cert == NULL) {
249 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
250 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
255 src->peer_cert->raw.len)) != 0) {
256 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200257 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200259 }
260 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000261#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000263 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_calloc(1, src->peer_cert_digest_len);
265 if (dst->peer_cert_digest == NULL) {
266 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
267 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
270 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000271 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000272 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 }
274#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200277
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200278#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (src->ticket != NULL) {
280 dst->ticket = mbedtls_calloc(1, src->ticket_len);
281 if (dst->ticket == NULL) {
282 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
283 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200286 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000287
288#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
289 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
293 if (ret != 0) {
294 return ret;
295 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000296 }
297#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
298 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200299#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200302}
303
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500304#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200305MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100306static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500307{
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
309 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800310 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500312
313 /* We want to copy len_new bytes when downsizing the buffer, and
314 * len_old bytes when upsizing, so we choose the smaller of two sizes,
315 * to fit one buffer into another. Size checks, ensuring that no data is
316 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 memcpy(resized_buffer, *buffer,
318 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100319 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500320
321 *buffer = resized_buffer;
322 *len_old = len_new;
323
324 return 0;
325}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
328 size_t in_buf_new_len,
329 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200330{
331 int modified = 0;
332 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
333 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200335 written_in = ssl->in_msg - ssl->in_buf;
336 iv_offset_in = ssl->in_iv - ssl->in_buf;
337 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200339 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 ssl->in_buf_len < in_buf_new_len) {
341 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
342 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
343 } else {
344 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
345 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200346 modified = 1;
347 }
348 }
349 }
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200352 written_out = ssl->out_msg - ssl->out_buf;
353 iv_offset_out = ssl->out_iv - ssl->out_buf;
354 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 ssl->out_buf_len < out_buf_new_len) {
358 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
359 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
360 } else {
361 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
362 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200363 modified = 1;
364 }
365 }
366 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200368 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Fields below might not be properly updated with record
371 * splitting or with CID, so they are manually updated here. */
372 ssl->out_msg = ssl->out_buf + written_out;
373 ssl->out_len = ssl->out_buf + len_offset_out;
374 ssl->out_iv = ssl->out_buf + iv_offset_out;
375
376 ssl->in_msg = ssl->in_buf + written_in;
377 ssl->in_len = ssl->in_buf + len_offset_in;
378 ssl->in_iv = ssl->in_buf + iv_offset_in;
379 }
380}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500381#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
382
Jerry Yudb8c48a2022-01-27 14:54:54 +0800383#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800384
385#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
387 const char *label,
388 const unsigned char *random, size_t rlen,
389 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800392
393#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
394
395/* Type for the TLS PRF */
396typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
397 const unsigned char *, size_t,
398 unsigned char *, size_t);
399
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200400MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100401static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
402 int ciphersuite,
403 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200404#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 ssl_tls_prf_t tls_prf,
408 const unsigned char randbytes[64],
409 mbedtls_ssl_protocol_version tls_version,
410 unsigned endpoint,
411 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800412
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100413#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200414MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300416 const char *label,
417 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100419static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
420static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100421
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100422#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100424#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425MBEDTLS_CHECK_RETURN_CRITICAL
426static int tls_prf_sha384(const unsigned char *secret, size_t slen,
427 const char *label,
428 const unsigned char *random, size_t rlen,
429 unsigned char *dstbuf, size_t dlen);
430
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100431static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
432static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100433#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100434
435static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
436 unsigned char *buf,
437 size_t buf_len);
438
439MBEDTLS_CHECK_RETURN_CRITICAL
440static int ssl_tls12_session_load(mbedtls_ssl_session *session,
441 const unsigned char *buf,
442 size_t len);
443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
444
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100445static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100446
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100447#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100448static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100449#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100450
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100451#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100452static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100453#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100454
455int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
456 const unsigned char *secret, size_t slen,
457 const char *label,
458 const unsigned char *random, size_t rlen,
459 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300460{
461 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100465#if defined(MBEDTLS_MD_CAN_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300466 case MBEDTLS_SSL_TLS_PRF_SHA384:
467 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100469#endif /* MBEDTLS_MD_CAN_SHA384*/
470#if defined(MBEDTLS_MD_CAN_SHA256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300471 case MBEDTLS_SSL_TLS_PRF_SHA256:
472 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 break;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100474#endif /* MBEDTLS_MD_CAN_SHA256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 default:
477 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300481}
482
Jerry Yuc73c6182022-02-08 20:29:25 +0800483#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100484static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800485{
486#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (session->peer_cert != NULL) {
488 mbedtls_x509_crt_free(session->peer_cert);
489 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800490 session->peer_cert = NULL;
491 }
492#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800494 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 session->peer_cert_digest = NULL;
497 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
498 session->peer_cert_digest_len = 0;
499 }
500#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
501}
502#endif /* MBEDTLS_X509_CRT_PARSE_C */
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800505{
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800507 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800509
510 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800512
513 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800515
516 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800518
519 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800521
522 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800524
525 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800527
528 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800530
531 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800533
534 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800536
537 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800539
540 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800542
543 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800545
546 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800548
549 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800551
552 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800554
555 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800557
558 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800560
561 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800563
564 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800566
567 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800569
570 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800572
573 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800575
576 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800578
579 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800581
582 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800584
Jan Bruckner151f6422023-02-10 12:45:19 +0100585 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
586 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
587
Jerry Yu7a485c12022-10-31 13:08:18 +0800588 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800590
591 }
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800594}
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800597{
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800599}
600
Jerry Yud25cab02022-10-31 12:48:30 +0800601#if defined(MBEDTLS_DEBUG_C)
602static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800603 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800604 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
605 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
606 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
607 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
608 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
609 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
610 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
611 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
612 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
613 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
615 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
616 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
617 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
618 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
619 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
620 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
621 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
622 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
623 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
624 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
625 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
626 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
627 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
628 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
629 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100630 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
631 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800632};
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800635 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
636 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
637 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
638 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
639 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
640 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
641 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
642 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
643 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
644 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
645 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
646 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
647 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
648 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
649 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
650 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
651 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
652 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
653 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
654 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
655 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
656 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
657 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
658 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
659 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
660 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
661 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100662 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
663 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800664};
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800667{
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return extension_name_table[
669 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800670}
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800675 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800677 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800679 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800681 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800683 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800691}
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
694 int level, const char *file, int line,
695 int hs_msg_type, unsigned int extension_type,
696 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800697{
698 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800700 mbedtls_debug_print_msg(
701 ssl, level, file, line,
702 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 ssl_tls13_get_hs_msg_name(hs_msg_type),
704 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800705 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800707 return;
708 }
709
710 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800712 mbedtls_debug_print_msg(
713 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
715 mbedtls_ssl_get_extension_name(extension_type), extension_type,
716 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800717 return;
718 }
719
720 mbedtls_debug_print_msg(
721 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
723 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800724}
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
727 int level, const char *file, int line,
728 int hs_msg_type, uint32_t extensions_mask,
729 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800730{
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 for (unsigned i = 0;
733 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
734 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800735 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800736 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800738 }
739}
740
Pengyu Lvee455c02023-01-12 14:37:24 +0800741#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800742static const char *ticket_flag_name_table[] =
743{
744 [0] = "ALLOW_PSK_RESUMPTION",
745 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
746 [3] = "ALLOW_EARLY_DATA",
747};
748
Pengyu Lv4938a562023-01-16 11:28:49 +0800749void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
750 int level, const char *file, int line,
751 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800752{
753 size_t i;
754
755 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800756 "print ticket_flags (0x%02x)", flags);
757
758 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800759
760 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800761 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800762 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
763 ticket_flag_name_table[i]);
764 }
765 }
766}
767#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
768
Jerry Yud25cab02022-10-31 12:48:30 +0800769#endif /* MBEDTLS_DEBUG_C */
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
772 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800773{
774 ((void) ciphersuite_info);
775
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100776#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800778 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800780#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100781#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800783 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800785#endif
786 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800788 return;
789 }
790}
791
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100792int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100793 unsigned hs_type,
794 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100795{
796 unsigned char hs_hdr[4];
797
798 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
800 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
801 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
802 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100803
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100804 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100805}
806
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100807int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100808 unsigned hs_type,
809 unsigned char const *msg,
810 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100811{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100812 int ret;
813 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100814 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100815 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100816 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100817 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100818}
819
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100820int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800821{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100822#if defined(MBEDTLS_MD_CAN_SHA256) || \
823 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100824#if defined(MBEDTLS_USE_PSA_CRYPTO)
825 psa_status_t status;
826#else
827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
828#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100829#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800830 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100831#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100832#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100834 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
835 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200836 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100837 }
838 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
839 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200840 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100841 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800842#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100843 mbedtls_md_free(&ssl->handshake->fin_sha256);
844 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100845 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
846 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
847 0);
848 if (ret != 0) {
849 return ret;
850 }
851 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100852 if (ret != 0) {
853 return ret;
854 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#endif
856#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100857#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
860 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200861 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100862 }
863 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
864 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200865 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100866 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800867#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100868 mbedtls_md_free(&ssl->handshake->fin_sha384);
869 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100870 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
871 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
872 if (ret != 0) {
873 return ret;
874 }
875 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100876 if (ret != 0) {
877 return ret;
878 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800879#endif
880#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100881 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800882}
883
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100884static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100885 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800886{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100887#if defined(MBEDTLS_MD_CAN_SHA256) || \
888 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100889#if defined(MBEDTLS_USE_PSA_CRYPTO)
890 psa_status_t status;
891#else
892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
893#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100894#else /* SHA-256 or SHA-384 */
895 ((void) ssl);
896 (void) buf;
897 (void) len;
898#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100899#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800900#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100901 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
902 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200903 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100904 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800905#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100906 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100907 if (ret != 0) {
908 return ret;
909 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800910#endif
911#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100912#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800913#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100914 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
915 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200916 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100917 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800918#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100919 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100920 if (ret != 0) {
921 return ret;
922 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800923#endif
924#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100925 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800926}
927
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100928#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100929static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100930 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800931{
932#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200933 return mbedtls_md_error_from_psa(psa_hash_update(
934 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800935#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100936 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800937#endif
938}
939#endif
940
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100941#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100942static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100943 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800944{
945#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200946 return mbedtls_md_error_from_psa(psa_hash_update(
947 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800948#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100949 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800950#endif
951}
952#endif
953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200955{
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200957
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100958#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500959#if defined(MBEDTLS_USE_PSA_CRYPTO)
960 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500961#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100962 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200963#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500964#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100965#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500966#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500967 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500968#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100969 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500971#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972
973 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200977#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200978#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200979 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200981#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200982#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200983#if defined(MBEDTLS_USE_PSA_CRYPTO)
984 handshake->psa_pake_ctx = psa_pake_operation_init();
985 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
986#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200988#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200989#if defined(MBEDTLS_SSL_CLI_C)
990 handshake->ecjpake_cache = NULL;
991 handshake->ecjpake_cache_len = 0;
992#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200993#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200994
Gilles Peskineeccd8882020-03-10 12:19:08 +0100995#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200997#endif
998
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200999#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1000 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1001#endif
Hanno Becker75173122019-02-06 16:18:31 +00001002
1003#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1004 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001006#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001007}
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010{
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001012
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001013#if defined(MBEDTLS_USE_PSA_CRYPTO)
1014 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1015 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001016#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1018 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001019#endif
1020
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001021#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001022#if defined(MBEDTLS_USE_PSA_CRYPTO)
1023 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1024 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001025#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_md_init(&transform->md_ctx_enc);
1027 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001028#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001029#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001030}
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001033{
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001035}
1036
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001038static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001039{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1041
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001042 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001043#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (ssl->transform_negotiate) {
1045 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1046 }
Jerry Yu2e199812022-12-01 18:57:19 +08001047#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ssl->session_negotiate) {
1049 mbedtls_ssl_session_free(ssl->session_negotiate);
1050 }
1051 if (ssl->handshake) {
1052 mbedtls_ssl_handshake_free(ssl);
1053 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001054
Jerry Yu2e199812022-12-01 18:57:19 +08001055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001056 /*
1057 * Either the pointers are now NULL or cleared properly and can be freed.
1058 * Now allocate missing structures.
1059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (ssl->transform_negotiate == NULL) {
1061 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001062 }
Jerry Yu2e199812022-12-01 18:57:19 +08001063#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (ssl->session_negotiate == NULL) {
1066 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001067 }
Paul Bakker48916f92012-09-16 19:57:18 +00001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->handshake == NULL) {
1070 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001071 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001072#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1073 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1076 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001077#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001078
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001079 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001081#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001082 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001083#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 ssl->session_negotiate == NULL) {
1085 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001088 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001089
1090#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001092 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001093#endif
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001096 ssl->session_negotiate = NULL;
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001099 }
1100
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001101 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_ssl_session_init(ssl->session_negotiate);
1103 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001104
Jerry Yu2e199812022-12-01 18:57:19 +08001105#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001107#endif
1108
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001109 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001110 ret = mbedtls_ssl_reset_checksum(ssl);
1111 if (ret != 0) {
1112 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1113 return ret;
1114 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001115
Jerry Yud0766ec2022-09-22 10:46:57 +08001116#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1117 defined(MBEDTLS_SSL_SRV_C) && \
1118 defined(MBEDTLS_SSL_SESSION_TICKETS)
1119 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001121#endif
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001125 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001128 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001130 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001134 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001135#endif
1136
Brett Warrene0edc842021-08-17 09:53:13 +01001137/*
1138 * curve_list is translated to IANA TLS group identifiers here because
1139 * mbedtls_ssl_conf_curves returns void and so can't return
1140 * any error codes.
1141 */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001142#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01001143#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1144 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001146 size_t length;
1147 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1148
Thomas Daubney850a0792023-05-22 12:05:03 +01001149 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 }
Brett Warrene0edc842021-08-17 09:53:13 +01001151
1152 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1154 if (group_list == NULL) {
1155 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1156 }
Brett Warrene0edc842021-08-17 09:53:13 +01001157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001159 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 curve_list[i]);
1161 if (tls_id == 0) {
1162 mbedtls_free(group_list);
1163 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001164 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001165 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001166 }
1167
1168 group_list[length] = 0;
1169
1170 ssl->handshake->group_list = group_list;
1171 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001173 ssl->handshake->group_list = ssl->conf->group_list;
1174 ssl->handshake->group_list_heap_allocated = 0;
1175 }
1176#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02001177#endif /* MBEDTLS_ECP_C */
Brett Warrene0edc842021-08-17 09:53:13 +01001178
Ronald Crone68ab4f2022-10-05 12:46:29 +02001179#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001180#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001181#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001182 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1183 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1185 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001186 const int *md;
1187 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001188 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001189 uint16_t *p;
1190
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001191 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1192 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1193 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1196 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001197 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001199#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001201#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001202
Jerry Yub476a442022-01-21 18:14:45 +08001203#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001205#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1207 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1208 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001209 }
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1212 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1213 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1216 sizeof(uint16_t));
1217 if (ssl->handshake->sig_algs == NULL) {
1218 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1219 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 p = (uint16_t *) ssl->handshake->sig_algs;
1222 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1223 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1224 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001225 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001227#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001229 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001230#endif
1231#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001233 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001234#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001236 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001237 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001239#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001240 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001241 ssl->handshake->sig_algs_heap_allocated = 0;
1242 }
Jerry Yucc539102022-06-27 16:27:35 +08001243#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001244#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001246}
1247
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001248#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001249/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001250MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001251static int ssl_cookie_write_dummy(void *ctx,
1252 unsigned char **p, unsigned char *end,
1253 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001254{
1255 ((void) ctx);
1256 ((void) p);
1257 ((void) end);
1258 ((void) cli_id);
1259 ((void) cli_id_len);
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001262}
1263
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001265static int ssl_cookie_check_dummy(void *ctx,
1266 const unsigned char *cookie, size_t cookie_len,
1267 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001268{
1269 ((void) ctx);
1270 ((void) cookie);
1271 ((void) cookie_len);
1272 ((void) cli_id);
1273 ((void) cli_id_len);
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001276}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001277#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001278
Paul Bakker5121ce52009-01-03 21:22:43 +00001279/*
1280 * Initialize an SSL context
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001283{
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001285}
1286
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001287MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001288static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001289{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001290 const mbedtls_ssl_config *conf = ssl->conf;
1291
Ronald Cron6f135e12021-12-08 16:57:54 +01001292#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1294 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1295 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1296 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1300 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001301 }
1302#endif
1303
1304#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1306 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1307 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001308 }
1309#endif
1310
Ronald Cron6f135e12021-12-08 16:57:54 +01001311#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1313 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1314 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1315 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001316 }
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1319 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001320 }
1321#endif
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1324 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001325}
1326
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001327MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001328static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1329{
1330 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 ret = ssl_conf_version_check(ssl);
1332 if (ret != 0) {
1333 return ret;
1334 }
Jerry Yu60835a82021-08-04 10:13:52 +08001335
Jerry Yudef7ae42022-10-30 14:13:19 +08001336#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1337 /* RFC 8446 section 4.4.3
1338 *
1339 * If the verification fails, the receiver MUST terminate the handshake with
1340 * a "decrypt_error" alert.
1341 *
1342 * If the client is configured as TLS 1.3 only with optional verify, return
1343 * bad config.
1344 *
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1347 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001348 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1349 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1350 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001352 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 1, ("Optional verify auth mode "
1354 "is not available for TLS 1.3 client"));
1355 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001356 }
1357#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1358
Jerry Yu60835a82021-08-04 10:13:52 +08001359 /* Space for further checks */
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001362}
1363
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001364/*
1365 * Setup an SSL context
1366 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1369 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001370{
Janos Follath865b3eb2019-12-16 11:46:15 +00001371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001372 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1373 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001375 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if ((ret = ssl_conf_check(ssl)) != 0) {
1378 return ret;
1379 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001380 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001381
Paul Bakker62f2dee2012-09-28 07:31:51 +00001382 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001383 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001384 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001385
1386 /* Set to NULL in case of an error condition */
1387 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001388
Darryl Greenb33cc762019-11-28 14:29:44 +00001389#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1390 ssl->in_buf_len = in_buf_len;
1391#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1393 if (ssl->in_buf == NULL) {
1394 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001395 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001396 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001397 }
1398
Darryl Greenb33cc762019-11-28 14:29:44 +00001399#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1400 ssl->out_buf_len = out_buf_len;
1401#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1403 if (ssl->out_buf == NULL) {
1404 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001405 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001406 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001410
Johan Pascalb62bb512015-12-03 21:56:45 +01001411#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001413#endif
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001416 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001420
1421error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_free(ssl->in_buf);
1423 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001424
1425 ssl->conf = NULL;
1426
Darryl Greenb33cc762019-11-28 14:29:44 +00001427#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1428 ssl->in_buf_len = 0;
1429 ssl->out_buf_len = 0;
1430#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001431 ssl->in_buf = NULL;
1432 ssl->out_buf = NULL;
1433
1434 ssl->in_hdr = NULL;
1435 ssl->in_ctr = NULL;
1436 ssl->in_len = NULL;
1437 ssl->in_iv = NULL;
1438 ssl->in_msg = NULL;
1439
1440 ssl->out_hdr = NULL;
1441 ssl->out_ctr = NULL;
1442 ssl->out_len = NULL;
1443 ssl->out_iv = NULL;
1444 ssl->out_msg = NULL;
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447}
1448
1449/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001450 * Reset an initialized and used SSL context for re-use while retaining
1451 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001452 *
1453 * If partial is non-zero, keep data in the input buffer and client ID.
1454 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1457 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001458{
Darryl Greenb33cc762019-11-28 14:29:44 +00001459#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1460 size_t in_buf_len = ssl->in_buf_len;
1461 size_t out_buf_len = ssl->out_buf_len;
1462#else
1463 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1464 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1465#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Hanno Beckerb0302c42021-08-03 09:39:42 +01001467#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1468 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001469#endif
1470
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001471 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001475
1476 /* Reset incoming message parsing */
1477 ssl->in_offt = NULL;
1478 ssl->nb_zero = 0;
1479 ssl->in_msgtype = 0;
1480 ssl->in_msglen = 0;
1481 ssl->in_hslen = 0;
1482 ssl->keep_current_message = 0;
1483 ssl->transform_in = NULL;
1484
1485#if defined(MBEDTLS_SSL_PROTO_DTLS)
1486 ssl->next_record_offset = 0;
1487 ssl->in_epoch = 0;
1488#endif
1489
1490 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001492 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494 }
1495
Ronald Cronad8c17b2022-06-10 17:18:09 +02001496 ssl->send_alert = 0;
1497
Hanno Beckerb0302c42021-08-03 09:39:42 +01001498 /* Reset outgoing message writing */
1499 ssl->out_msgtype = 0;
1500 ssl->out_msglen = 0;
1501 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 memset(ssl->out_buf, 0, out_buf_len);
1503 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504 ssl->transform_out = NULL;
1505
1506#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508#endif
1509
XiaokangQian2b01dc32022-01-21 02:53:13 +00001510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (ssl->transform) {
1512 mbedtls_ssl_transform_free(ssl->transform);
1513 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 ssl->transform = NULL;
1515 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1517
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 mbedtls_ssl_transform_free(ssl->transform_application);
1520 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001521 ssl->transform_application = NULL;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001524#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1526 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001527 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001528#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1531 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532 ssl->handshake->transform_handshake = NULL;
1533 }
1534
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001536}
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001539{
1540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1541
1542 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001545
1546 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_RENEGOTIATION)
1548 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001549 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001550
1551 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1553 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001556
Hanno Beckerb0302c42021-08-03 09:39:42 +01001557 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001558 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ssl->session) {
1560 mbedtls_ssl_session_free(ssl->session);
1561 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001562 ssl->session = NULL;
1563 }
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001566 ssl->alpn_chosen = NULL;
1567#endif
1568
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001569#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001570 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001571#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001573#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (free_cli_id) {
1575 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001576 ssl->cli_id = NULL;
1577 ssl->cli_id_len = 0;
1578 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001579#endif
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_handshake_init(ssl)) != 0) {
1582 return ret;
1583 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001586}
1587
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001588/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001589 * Reset an initialized and used SSL context for re-use while retaining
1590 * all application-set variables, function pointers and data.
1591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001593{
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595}
1596
1597/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 * SSL set accessors
1599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001602 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603}
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001607 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001608}
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001611void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001613 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001614}
1615#endif
1616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001620}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1625 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001626{
1627 ssl->disable_datagram_packing = !allow_packing;
1628}
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1631 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001633 conf->hs_timeout_min = min;
1634 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001635}
1636#endif
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001641}
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1645 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1646 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001648 conf->f_vrfy = f_vrfy;
1649 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1654 int (*f_rng)(void *, unsigned char *, size_t),
1655 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001657 conf->f_rng = f_rng;
1658 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659}
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1662 void (*f_dbg)(void *, int, const char *, int, const char *),
1663 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001665 conf->f_dbg = f_dbg;
1666 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667}
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1670 void *p_bio,
1671 mbedtls_ssl_send_t *f_send,
1672 mbedtls_ssl_recv_t *f_recv,
1673 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001674{
1675 ssl->p_bio = p_bio;
1676 ssl->f_send = f_send;
1677 ssl->f_recv = f_recv;
1678 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001679}
1680
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001682void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001683{
1684 ssl->mtu = mtu;
1685}
1686#endif
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001689{
1690 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001691}
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1694 void *p_timer,
1695 mbedtls_ssl_set_timer_t *f_set_timer,
1696 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001697{
1698 ssl->p_timer = p_timer;
1699 ssl->f_set_timer = f_set_timer;
1700 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001701
1702 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001704}
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001707void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1708 void *p_cache,
1709 mbedtls_ssl_cache_get_t *f_get_cache,
1710 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001712 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001713 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001714 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001719int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001720{
Janos Follath865b3eb2019-12-16 11:46:15 +00001721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001724 session == NULL ||
1725 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001728 }
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (ssl->handshake->resume == 1) {
1731 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1732 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001733
Jerry Yu21092062022-10-10 21:21:31 +08001734#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001736 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001740 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1742 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1743 session->ciphersuite));
1744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001745 }
Jerry Yu40afab62022-10-08 10:42:13 +08001746 }
Jerry Yu21092062022-10-10 21:21:31 +08001747#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1750 session)) != 0) {
1751 return ret;
1752 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001753
Paul Bakker0a597072012-09-25 21:55:46 +00001754 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1761 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001762{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001763 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764}
1765
Ronald Cron6f135e12021-12-08 16:57:54 +01001766#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001767void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1768 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001769{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001770 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001771}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001772
1773#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001774void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1775 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001776{
1777 conf->early_data_enabled = early_data_enabled;
1778}
Jerry Yucc4e0072022-11-22 17:22:22 +08001779
1780#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001781void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001783{
Jerry Yu39da9852022-12-06 16:58:36 +08001784 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001785}
1786#endif /* MBEDTLS_SSL_SRV_C */
1787
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001788#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1793 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001794{
1795 conf->cert_profile = profile;
1796}
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001799{
1800 mbedtls_ssl_key_cert *cur = key_cert, *next;
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001803 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001805 cur = next;
1806 }
1807}
1808
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001809/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001810MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001811static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1812 mbedtls_x509_crt *cert,
1813 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001814{
niisato8ee24222018-06-25 19:05:48 +09001815 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001818 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001820 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001822 }
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1825 if (new_cert == NULL) {
1826 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1827 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001828
niisato8ee24222018-06-25 19:05:48 +09001829 new_cert->cert = cert;
1830 new_cert->key = key;
1831 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001832
Glenn Strauss36872db2022-01-22 05:06:31 -05001833 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001835 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001837 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001839 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 }
niisato8ee24222018-06-25 19:05:48 +09001841 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001845}
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001848 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001850{
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001852}
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001855 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001858 conf->ca_chain = ca_chain;
1859 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001860
1861#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1862 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1863 * cannot be used together. */
1864 conf->f_ca_cb = NULL;
1865 conf->p_ca_cb = NULL;
1866#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001867}
Hanno Becker5adaad92019-03-27 16:54:37 +00001868
1869#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001870void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1871 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1872 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001873{
1874 conf->f_ca_cb = f_ca_cb;
1875 conf->p_ca_cb = p_ca_cb;
1876
1877 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1878 * cannot be used together. */
1879 conf->ca_chain = NULL;
1880 conf->ca_crl = NULL;
1881}
1882#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001884
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001886const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1887 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001888{
1889 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001891}
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1894 mbedtls_x509_crt *own_cert,
1895 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001896{
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1898 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001899}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1902 mbedtls_x509_crt *ca_chain,
1903 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001904{
1905 ssl->handshake->sni_ca_chain = ca_chain;
1906 ssl->handshake->sni_ca_crl = ca_crl;
1907}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001908
Glenn Strauss999ef702022-03-11 01:37:23 -05001909#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001910void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1911 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001912{
1913 ssl->handshake->dn_hints = crt;
1914}
1915#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1918 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001919{
1920 ssl->handshake->sni_authmode = authmode;
1921}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001922#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1923
Hanno Becker8927c832019-04-03 12:52:50 +01001924#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001925void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1926 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1927 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001928{
1929 ssl->f_vrfy = f_vrfy;
1930 ssl->p_vrfy = p_vrfy;
1931}
1932#endif
1933
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001934#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001935
Valerio Setti016f6822022-12-09 14:17:50 +01001936#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001937static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1938static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1939
Valerio Setti016f6822022-12-09 14:17:50 +01001940static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 mbedtls_ssl_context *ssl,
1942 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001943{
1944 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001945 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001946 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001947 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001948 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001949 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1951 psa_pake_cs_set_primitive(&cipher_suite,
1952 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1953 PSA_ECC_FAMILY_SECP_R1,
1954 256));
1955 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1958 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001959 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001963 user = jpake_server_id;
1964 user_len = sizeof(jpake_server_id);
1965 peer = jpake_client_id;
1966 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001968 user = jpake_client_id;
1969 user_len = sizeof(jpake_client_id);
1970 peer = jpake_server_id;
1971 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001973
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001974 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1975 if (status != PSA_SUCCESS) {
1976 return status;
1977 }
1978
1979 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001981 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 }
Valerio Setti016f6822022-12-09 14:17:50 +01001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1985 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001986 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 }
Valerio Setti016f6822022-12-09 14:17:50 +01001988
1989 ssl->handshake->psa_pake_ctx_is_ok = 1;
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001992}
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1995 const unsigned char *pw,
1996 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001997{
1998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1999 psa_status_t status;
2000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (ssl->handshake == NULL || ssl->conf == NULL) {
2002 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002003 }
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 /* Empty password is not valid */
2006 if ((pw == NULL) || (pw_len == 0)) {
2007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2008 }
2009
2010 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2011 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2012 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2013
2014 status = psa_import_key(&attributes, pw, pw_len,
2015 &ssl->handshake->psa_pake_password);
2016 if (status != PSA_SUCCESS) {
2017 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2018 }
2019
2020 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2021 ssl->handshake->psa_pake_password);
2022 if (status != PSA_SUCCESS) {
2023 psa_destroy_key(ssl->handshake->psa_pake_password);
2024 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2025 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2026 }
2027
2028 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002029}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2032 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002033{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002034 psa_status_t status;
2035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (ssl->handshake == NULL || ssl->conf == NULL) {
2037 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002038 }
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (mbedtls_svc_key_id_is_null(pwd)) {
2041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2042 }
2043
2044 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2045 if (status != PSA_SUCCESS) {
2046 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2047 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2048 }
2049
2050 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002051}
2052#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2054 const unsigned char *pw,
2055 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002056{
2057 mbedtls_ecjpake_role role;
2058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (ssl->handshake == NULL || ssl->conf == NULL) {
2060 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2061 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002062
Valerio Settic689ed82022-12-07 14:40:38 +01002063 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if ((pw == NULL) || (pw_len == 0)) {
2065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2066 }
Valerio Settic689ed82022-12-07 14:40:38 +01002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002069 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002071 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2075 role,
2076 MBEDTLS_MD_SHA256,
2077 MBEDTLS_ECP_DP_SECP256R1,
2078 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002079}
Valerio Setti02c25b52022-11-15 14:08:42 +01002080#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002081#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002082
Ronald Cron73fe8df2022-10-05 14:31:43 +02002083#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002084int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002085{
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (conf->psk_identity == NULL ||
2087 conf->psk_identity_len == 0) {
2088 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002089 }
2090
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2093 return 1;
2094 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002095#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (conf->psk != NULL && conf->psk_len != 0) {
2098 return 1;
2099 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002102}
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002105{
2106 /* Remove reference to existing PSK, if any. */
2107#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002109 /* The maintenance of the PSK key slot is the
2110 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002111 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002113#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002115 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002116 conf->psk = NULL;
2117 conf->psk_len = 0;
2118 }
2119
2120 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (conf->psk_identity != NULL) {
2122 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002123 conf->psk_identity = NULL;
2124 conf->psk_identity_len = 0;
2125 }
2126}
2127
Hanno Becker7390c712018-11-15 13:33:04 +00002128/* This function assumes that PSK identity in the SSL config is unset.
2129 * It checks that the provided identity is well-formed and attempts
2130 * to make a copy of it in the SSL config.
2131 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002132MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002133static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2134 unsigned char const *psk_identity,
2135 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002136{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002137 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002139 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 (psk_identity_len >> 16) != 0 ||
2141 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002143 }
2144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2146 if (conf->psk_identity == NULL) {
2147 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2148 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002149
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002150 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002154}
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2157 const unsigned char *psk, size_t psk_len,
2158 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002159{
Janos Follath865b3eb2019-12-16 11:46:15 +00002160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002161
2162 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2164 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2165 }
Hanno Becker7390c712018-11-15 13:33:04 +00002166
2167 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (psk == NULL) {
2169 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2170 }
2171 if (psk_len == 0) {
2172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2173 }
2174 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2175 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2176 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2179 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2180 }
Hanno Becker7390c712018-11-15 13:33:04 +00002181 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002183
2184 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2186 if (ret != 0) {
2187 ssl_conf_remove_psk(conf);
2188 }
Hanno Becker7390c712018-11-15 13:33:04 +00002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002191}
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002194{
2195#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002197 /* The maintenance of the external PSK key slot is the
2198 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (ssl->handshake->psk_opaque_is_internal) {
2200 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002201 ssl->handshake->psk_opaque_is_internal = 0;
2202 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002203 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002204 }
Neil Armstronge952a302022-05-03 10:22:14 +02002205#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (ssl->handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002207 mbedtls_zeroize_and_free(ssl->handshake->psk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 ssl->handshake->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002209 ssl->handshake->psk_len = 0;
2210 }
Neil Armstronge952a302022-05-03 10:22:14 +02002211#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002212}
2213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2215 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002216{
Neil Armstrong501c9322022-05-03 09:35:09 +02002217#if defined(MBEDTLS_USE_PSA_CRYPTO)
2218 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002220 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002221 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002222#endif /* MBEDTLS_USE_PSA_CRYPTO */
2223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (psk == NULL || ssl->handshake == NULL) {
2225 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2226 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2229 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2230 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002233
Neil Armstrong501c9322022-05-03 09:35:09 +02002234#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2237 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2238 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2239 } else {
2240 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2241 }
2242 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002243 }
2244#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002245
Jerry Yu568ec252022-07-22 21:27:34 +08002246#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2248 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2249 psa_set_key_usage_flags(&key_attributes,
2250 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002251 }
2252#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 psa_set_key_algorithm(&key_attributes, alg);
2255 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2258 if (status != PSA_SUCCESS) {
2259 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2260 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002261
2262 /* Allow calling psa_destroy_key() on psk remove */
2263 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002265#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2268 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002269
2270 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002274#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002275}
2276
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002277#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002278int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2279 mbedtls_svc_key_id_t psk,
2280 const unsigned char *psk_identity,
2281 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002282{
Janos Follath865b3eb2019-12-16 11:46:15 +00002283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002284
2285 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2287 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2288 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002289
Hanno Becker7390c712018-11-15 13:33:04 +00002290 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (mbedtls_svc_key_id_is_null(psk)) {
2292 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2293 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002294 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002295
2296 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2298 psk_identity_len);
2299 if (ret != 0) {
2300 ssl_conf_remove_psk(conf);
2301 }
Hanno Becker7390c712018-11-15 13:33:04 +00002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002304}
2305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2307 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002308{
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if ((mbedtls_svc_key_id_is_null(psk)) ||
2310 (ssl->handshake == NULL)) {
2311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2312 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002315 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002317}
2318#endif /* MBEDTLS_USE_PSA_CRYPTO */
2319
Jerry Yu8897c072022-08-12 13:56:53 +08002320#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002321void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2322 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2323 size_t),
2324 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002325{
2326 conf->f_psk = f_psk;
2327 conf->p_psk = p_psk;
2328}
Jerry Yu8897c072022-08-12 13:56:53 +08002329#endif /* MBEDTLS_SSL_SRV_C */
2330
Ronald Cron73fe8df2022-10-05 14:31:43 +02002331#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002332
2333#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002334static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002336{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002337#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (alg == PSA_ALG_CBC_NO_PADDING) {
2339 return MBEDTLS_SSL_MODE_CBC;
2340 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002341#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (PSA_ALG_IS_AEAD(alg)) {
2343 return MBEDTLS_SSL_MODE_AEAD;
2344 }
2345 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002346}
2347
2348#else /* MBEDTLS_USE_PSA_CRYPTO */
2349
2350static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002352{
2353#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if (mode == MBEDTLS_MODE_CBC) {
2355 return MBEDTLS_SSL_MODE_CBC;
2356 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002357#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2358
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002359#if defined(MBEDTLS_GCM_C) || \
2360 defined(MBEDTLS_CCM_C) || \
2361 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002363 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 mode == MBEDTLS_MODE_CHACHAPOLY) {
2365 return MBEDTLS_SSL_MODE_AEAD;
2366 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002367#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002370}
Gilles Peskine301711e2022-04-26 16:57:05 +02002371#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002372
Gilles Peskinee108d982022-04-26 16:50:40 +02002373static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2374 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002376{
2377#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2379 base_mode == MBEDTLS_SSL_MODE_CBC) {
2380 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002381 }
2382#else
2383 (void) encrypt_then_mac;
2384#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002386}
2387
Neil Armstrongab555e02022-04-04 11:07:59 +02002388mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002390{
Gilles Peskinee108d982022-04-26 16:50:40 +02002391 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002392#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002394#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002396#endif
2397 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002398
Gilles Peskinee108d982022-04-26 16:50:40 +02002399 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002400#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002401 encrypt_then_mac = transform->encrypt_then_mac;
2402#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002404}
2405
Neil Armstrongab555e02022-04-04 11:07:59 +02002406mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002407#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002409#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002411{
Gilles Peskinee108d982022-04-26 16:50:40 +02002412 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2413
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002414#if defined(MBEDTLS_USE_PSA_CRYPTO)
2415 psa_status_t status;
2416 psa_algorithm_t alg;
2417 psa_key_type_t type;
2418 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002419 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2420 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (status == PSA_SUCCESS) {
2422 base_mode = mbedtls_ssl_get_base_mode(alg);
2423 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002424#else
2425 const mbedtls_cipher_info_t *cipher =
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002426 mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002428 base_mode =
2429 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002431 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002432#endif /* MBEDTLS_USE_PSA_CRYPTO */
2433
Gilles Peskinee108d982022-04-26 16:50:40 +02002434#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2435 int encrypt_then_mac = 0;
2436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002438}
2439
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002440#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002441
2442#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002443/* Serialization of TLS 1.3 sessions:
2444 *
2445 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002446 * opaque hostname<0..2^16-1>;
Jerry Yu342a5552023-11-10 14:23:39 +08002447 * uint64 ticket_reception_time;
Jerry Yu438ddd82022-07-07 06:55:50 +00002448 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002449 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002450 * } ClientOnlyData;
2451 *
2452 * struct {
2453 * uint8 endpoint;
2454 * uint8 ciphersuite[2];
2455 * uint32 ticket_age_add;
2456 * uint8 ticket_flags;
2457 * opaque resumption_key<0..255>;
Jerry Yu02e3a072022-12-12 15:13:20 +08002458 * uint32 max_early_data_size;
Jerry Yu438ddd82022-07-07 06:55:50 +00002459 * select ( endpoint ) {
2460 * case client: ClientOnlyData;
Jerry Yucf913512023-11-14 11:06:52 +08002461 * case server: uint64 ticket_creation_time;
Jerry Yu438ddd82022-07-07 06:55:50 +00002462 * };
2463 * } serialized_session_tls13;
2464 *
2465 */
2466#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002468static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2469 unsigned char *buf,
2470 size_t buf_len,
2471 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002472{
2473 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002474#if defined(MBEDTLS_SSL_CLI_C) && \
2475 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 size_t hostname_len = (session->hostname == NULL) ?
2477 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002478#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002479 size_t needed = 1 /* endpoint */
2480 + 2 /* ciphersuite */
2481 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002482 + 1 /* ticket_flags */
2483 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002484 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2487 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2488 }
Jerry Yu34191072022-08-18 10:32:09 +08002489 needed += session->resumption_key_len; /* resumption_key */
2490
Jerry Yu02e3a072022-12-12 15:13:20 +08002491#if defined(MBEDTLS_SSL_EARLY_DATA)
2492 needed += 4; /* max_early_data_size */
2493#endif
2494
Jerry Yu438ddd82022-07-07 06:55:50 +00002495#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucf913512023-11-14 11:06:52 +08002496 needed += 8; /* ticket_creation_time or ticket_reception_time */
Jerry Yu438ddd82022-07-07 06:55:50 +00002497#endif
2498
2499#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002501#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002502 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002504#endif
2505
Jerry Yu438ddd82022-07-07 06:55:50 +00002506 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002507 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002508
2509 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (session->ticket_len > SIZE_MAX - needed) {
2511 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2512 }
Jerry Yu34191072022-08-18 10:32:09 +08002513
Jerry Yue28d9742022-08-18 15:44:03 +08002514 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002515 }
2516#endif /* MBEDTLS_SSL_CLI_C */
2517
Jerry Yue36fdd62022-08-17 21:31:36 +08002518 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 if (needed > buf_len) {
2520 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2521 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002522
2523 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2525 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002526 p[7] = session->ticket_flags;
2527
2528 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002529 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002530 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002532 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002533
Jerry Yu02e3a072022-12-12 15:13:20 +08002534#if defined(MBEDTLS_SSL_EARLY_DATA)
2535 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
2536 p += 4;
2537#endif
2538
Jerry Yu438ddd82022-07-07 06:55:50 +00002539#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu25ba4d42023-11-10 14:12:20 +08002541 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002542 p += 8;
2543 }
2544#endif /* MBEDTLS_HAVE_TIME */
2545
2546#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002548#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002550 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002552 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002554 p += hostname_len;
2555 }
2556#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2557
Jerry Yu438ddd82022-07-07 06:55:50 +00002558#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08002559 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002560 p += 8;
2561#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002563 p += 4;
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002566 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (session->ticket != NULL && session->ticket_len > 0) {
2569 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002570 p += session->ticket_len;
2571 }
2572 }
2573#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002575}
2576
2577MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002578static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2579 const unsigned char *buf,
2580 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002581{
2582 const unsigned char *p = buf;
2583 const unsigned char *end = buf + len;
2584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (end - p < 9) {
2586 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2587 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002588 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2590 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002591 session->ticket_flags = p[7];
2592
2593 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002594 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002595 p += 9;
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (end - p < session->resumption_key_len) {
2598 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2599 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2602 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2603 }
2604 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002605 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002606
Jerry Yu02e3a072022-12-12 15:13:20 +08002607#if defined(MBEDTLS_SSL_EARLY_DATA)
2608 if (end - p < 4) {
2609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2610 }
2611 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
2612 p += 4;
2613#endif
2614
Jerry Yu438ddd82022-07-07 06:55:50 +00002615#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2617 if (end - p < 8) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
Jerry Yu25ba4d42023-11-10 14:12:20 +08002620 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 8;
2622 }
2623#endif /* MBEDTLS_HAVE_TIME */
2624
2625#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Yanray Wangfd256542023-11-22 10:32:03 +08002627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002628 size_t hostname_len;
2629 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (end - p < 2) {
2631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2632 }
2633 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002634 p += 2;
2635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (end - p < (long int) hostname_len) {
2637 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2638 }
2639 if (hostname_len > 0) {
2640 session->hostname = mbedtls_calloc(1, hostname_len);
2641 if (session->hostname == NULL) {
2642 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2643 }
2644 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002645 p += hostname_len;
2646 }
Yanray Wangfd256542023-11-22 10:32:03 +08002647#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002648
Jerry Yu438ddd82022-07-07 06:55:50 +00002649#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 if (end - p < 8) {
2651 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2652 }
Jerry Yu342a5552023-11-10 14:23:39 +08002653 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002654 p += 8;
2655#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 if (end - p < 4) {
2657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2658 }
2659 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002660 p += 4;
2661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (end - p < 2) {
2663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2664 }
2665 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002666 p += 2;
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (end - p < (long int) session->ticket_len) {
2669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2670 }
2671 if (session->ticket_len > 0) {
2672 session->ticket = mbedtls_calloc(1, session->ticket_len);
2673 if (session->ticket == NULL) {
2674 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2675 }
2676 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002677 p += session->ticket_len;
2678 }
2679 }
2680#endif /* MBEDTLS_SSL_CLI_C */
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002683
2684}
2685#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002686MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002687static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2688 unsigned char *buf,
2689 size_t buf_len,
2690 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002691{
2692 ((void) session);
2693 ((void) buf);
2694 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002695 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002697}
Jerry Yu438ddd82022-07-07 06:55:50 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2700 unsigned char *buf,
2701 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002702{
2703 ((void) session);
2704 ((void) buf);
2705 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002707}
2708#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002709#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2712 size_t taglen,
2713 psa_algorithm_t *alg,
2714 psa_key_type_t *key_type,
2715 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002716{
Yanray Wang19e4dc82023-11-16 18:02:05 +08002717#if !defined(MBEDTLS_SSL_HAVE_CCM)
2718 (void) taglen;
2719#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 switch (mbedtls_cipher_type) {
Yanray Wang1a369d62023-11-16 15:17:31 +08002721#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002722 case MBEDTLS_CIPHER_AES_128_CBC:
2723 *alg = PSA_ALG_CBC_NO_PADDING;
2724 *key_type = PSA_KEY_TYPE_AES;
2725 *key_size = 128;
2726 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002727#endif
2728#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002729 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002731 *key_type = PSA_KEY_TYPE_AES;
2732 *key_size = 128;
2733 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002734#endif
2735#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002736 case MBEDTLS_CIPHER_AES_128_GCM:
2737 *alg = PSA_ALG_GCM;
2738 *key_type = PSA_KEY_TYPE_AES;
2739 *key_size = 128;
2740 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002741#endif
2742#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002743 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002745 *key_type = PSA_KEY_TYPE_AES;
2746 *key_size = 192;
2747 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002748#endif
2749#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002750 case MBEDTLS_CIPHER_AES_192_GCM:
2751 *alg = PSA_ALG_GCM;
2752 *key_type = PSA_KEY_TYPE_AES;
2753 *key_size = 192;
2754 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002755#endif
2756#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002757 case MBEDTLS_CIPHER_AES_256_CBC:
2758 *alg = PSA_ALG_CBC_NO_PADDING;
2759 *key_type = PSA_KEY_TYPE_AES;
2760 *key_size = 256;
2761 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002762#endif
2763#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002764 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002766 *key_type = PSA_KEY_TYPE_AES;
2767 *key_size = 256;
2768 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002769#endif
2770#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002771 case MBEDTLS_CIPHER_AES_256_GCM:
2772 *alg = PSA_ALG_GCM;
2773 *key_type = PSA_KEY_TYPE_AES;
2774 *key_size = 256;
2775 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002776#endif
2777#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002778 case MBEDTLS_CIPHER_ARIA_128_CBC:
2779 *alg = PSA_ALG_CBC_NO_PADDING;
2780 *key_type = PSA_KEY_TYPE_ARIA;
2781 *key_size = 128;
2782 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002783#endif
2784#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002785 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002787 *key_type = PSA_KEY_TYPE_ARIA;
2788 *key_size = 128;
2789 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002790#endif
2791#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002792 case MBEDTLS_CIPHER_ARIA_128_GCM:
2793 *alg = PSA_ALG_GCM;
2794 *key_type = PSA_KEY_TYPE_ARIA;
2795 *key_size = 128;
2796 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002797#endif
2798#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002799 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002800 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002801 *key_type = PSA_KEY_TYPE_ARIA;
2802 *key_size = 192;
2803 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002804#endif
2805#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002806 case MBEDTLS_CIPHER_ARIA_192_GCM:
2807 *alg = PSA_ALG_GCM;
2808 *key_type = PSA_KEY_TYPE_ARIA;
2809 *key_size = 192;
2810 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002811#endif
2812#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002813 case MBEDTLS_CIPHER_ARIA_256_CBC:
2814 *alg = PSA_ALG_CBC_NO_PADDING;
2815 *key_type = PSA_KEY_TYPE_ARIA;
2816 *key_size = 256;
2817 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002818#endif
2819#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002820 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002822 *key_type = PSA_KEY_TYPE_ARIA;
2823 *key_size = 256;
2824 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002825#endif
2826#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002827 case MBEDTLS_CIPHER_ARIA_256_GCM:
2828 *alg = PSA_ALG_GCM;
2829 *key_type = PSA_KEY_TYPE_ARIA;
2830 *key_size = 256;
2831 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002832#endif
2833#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002834 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2835 *alg = PSA_ALG_CBC_NO_PADDING;
2836 *key_type = PSA_KEY_TYPE_CAMELLIA;
2837 *key_size = 128;
2838 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002839#endif
2840#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002841 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002843 *key_type = PSA_KEY_TYPE_CAMELLIA;
2844 *key_size = 128;
2845 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002846#endif
2847#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002848 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2849 *alg = PSA_ALG_GCM;
2850 *key_type = PSA_KEY_TYPE_CAMELLIA;
2851 *key_size = 128;
2852 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002853#endif
2854#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002855 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002857 *key_type = PSA_KEY_TYPE_CAMELLIA;
2858 *key_size = 192;
2859 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002860#endif
2861#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002862 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2863 *alg = PSA_ALG_GCM;
2864 *key_type = PSA_KEY_TYPE_CAMELLIA;
2865 *key_size = 192;
2866 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002867#endif
2868#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002869 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2870 *alg = PSA_ALG_CBC_NO_PADDING;
2871 *key_type = PSA_KEY_TYPE_CAMELLIA;
2872 *key_size = 256;
2873 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002874#endif
2875#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002876 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002878 *key_type = PSA_KEY_TYPE_CAMELLIA;
2879 *key_size = 256;
2880 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002881#endif
2882#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002883 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2884 *alg = PSA_ALG_GCM;
2885 *key_type = PSA_KEY_TYPE_CAMELLIA;
2886 *key_size = 256;
2887 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002888#endif
2889#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002890 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2891 *alg = PSA_ALG_CHACHA20_POLY1305;
2892 *key_type = PSA_KEY_TYPE_CHACHA20;
2893 *key_size = 256;
2894 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002895#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002896 case MBEDTLS_CIPHER_NULL:
2897 *alg = MBEDTLS_SSL_NULL_CIPHER;
2898 *key_type = 0;
2899 *key_size = 0;
2900 break;
2901 default:
2902 return PSA_ERROR_NOT_SUPPORTED;
2903 }
2904
2905 return PSA_SUCCESS;
2906}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002907#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002908
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002909#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002910int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2911 const unsigned char *dhm_P, size_t P_len,
2912 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002913{
Janos Follath865b3eb2019-12-16 11:46:15 +00002914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 mbedtls_mpi_free(&conf->dhm_P);
2917 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2920 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2921 mbedtls_mpi_free(&conf->dhm_P);
2922 mbedtls_mpi_free(&conf->dhm_G);
2923 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002924 }
2925
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002927}
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002930{
Janos Follath865b3eb2019-12-16 11:46:15 +00002931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 mbedtls_mpi_free(&conf->dhm_P);
2934 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2937 &conf->dhm_P)) != 0 ||
2938 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2939 &conf->dhm_G)) != 0) {
2940 mbedtls_mpi_free(&conf->dhm_P);
2941 mbedtls_mpi_free(&conf->dhm_G);
2942 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002943 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002946}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002947#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002948
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002949#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2950/*
2951 * Set the minimum length for Diffie-Hellman parameters
2952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2954 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002955{
2956 conf->dhm_min_bitlen = bitlen;
2957}
2958#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2959
Ronald Crone68ab4f2022-10-05 12:46:29 +02002960#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002961#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002962/*
2963 * Set allowed/preferred hashes for handshake signatures
2964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2966 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002967{
2968 conf->sig_hashes = hashes;
2969}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002970#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002971
Jerry Yuf017ee42022-01-12 15:49:48 +08002972/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002973void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2974 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002975{
Jerry Yuf017ee42022-01-12 15:49:48 +08002976#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2977 conf->sig_hashes = NULL;
2978#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2979 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002980}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002981#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002982
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02002983#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002984#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002985/*
2986 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002987 *
2988 * mbedtls_ssl_setup() takes the provided list
2989 * and translates it to a list of IANA TLS group identifiers,
2990 * stored in ssl->handshake->group_list.
2991 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002992 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002993void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2994 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002995{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002996 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002997 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002998}
Brett Warrene0edc842021-08-17 09:53:13 +01002999#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02003000#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003001
Brett Warrene0edc842021-08-17 09:53:13 +01003002/*
3003 * Set the allowed groups
3004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
3006 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01003007{
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02003008#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01003009 conf->curve_list = NULL;
3010#endif
3011 conf->group_list = group_list;
3012}
3013
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01003014#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003015int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00003016{
Hanno Becker947194e2017-04-07 13:25:49 +01003017 /* Initialize to suppress unnecessary compiler warning */
3018 size_t hostname_len = 0;
3019
3020 /* Check if new hostname is valid before
3021 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (hostname != NULL) {
3023 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
3026 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3027 }
Hanno Becker947194e2017-04-07 13:25:49 +01003028 }
3029
3030 /* Now it's clear that we will overwrite the old hostname,
3031 * so we can free it safely */
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003034 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01003035 }
3036
3037 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01003038
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01003040 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 } else {
3042 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
3043 if (ssl->hostname == NULL) {
3044 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3045 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003048
Hanno Becker947194e2017-04-07 13:25:49 +01003049 ssl->hostname[hostname_len] = '\0';
3050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01003054#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01003056#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003057void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
3058 int (*f_sni)(void *, mbedtls_ssl_context *,
3059 const unsigned char *, size_t),
3060 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00003061{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003062 conf->f_sni = f_sni;
3063 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00003064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003068int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003069{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003070 size_t cur_len, tot_len;
3071 const char **p;
3072
3073 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003074 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3075 * MUST NOT be truncated."
3076 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003077 */
3078 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 for (p = protos; *p != NULL; p++) {
3080 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003081 tot_len += cur_len;
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if ((cur_len == 0) ||
3084 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3085 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3087 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003088 }
3089
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003090 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003093}
3094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003096{
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003100
Johan Pascalb62bb512015-12-03 21:56:45 +01003101#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3103 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003104{
3105 conf->dtls_srtp_mki_support = support_mki_value;
3106}
3107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3109 unsigned char *mki_value,
3110 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003111{
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003114 }
Ron Eldor591f1622018-01-22 12:30:04 +02003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3117 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003118 }
Ron Eldor591f1622018-01-22 12:30:04 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003121 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003123}
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3126 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003127{
Johan Pascal253d0262020-09-22 13:04:45 +02003128 const mbedtls_ssl_srtp_profile *p;
3129 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003130
Johan Pascal253d0262020-09-22 13:04:45 +02003131 /* check the profiles list: all entry must be valid,
3132 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3134 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3135 p++) {
3136 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003137 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003139 /* unsupported value, stop parsing and set the size to an error value */
3140 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003141 }
3142 }
3143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3145 conf->dtls_srtp_profile_list = NULL;
3146 conf->dtls_srtp_profile_list_len = 0;
3147 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003148 }
3149
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003150 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003151 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003154}
3155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3157 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003158{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003159 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3160 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003162 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003164 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3166 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003167 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003168}
Johan Pascalb62bb512015-12-03 21:56:45 +01003169#endif /* MBEDTLS_SSL_DTLS_SRTP */
3170
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303171#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003172void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003173{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003174 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00003175}
3176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003178{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003179 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00003180}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303181#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003182
Janos Follath088ce432017-04-10 12:42:31 +01003183#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003184void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3185 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003186{
3187 conf->cert_req_ca_list = cert_req_ca_list;
3188}
3189#endif
3190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003192void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003193{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003194 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003195}
3196#endif
3197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003199void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003200{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003201 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003202}
3203#endif
3204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003206int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003207{
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3209 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3210 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003211 }
3212
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003213 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003216}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003220{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003221 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003222}
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003225void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003226{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003227 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003228}
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003231{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003232 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003233}
3234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3236 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003237{
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003243#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003244void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003245{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003246 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003247}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003248#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003249
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003250#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003251
Jerry Yud0766ec2022-09-22 10:46:57 +08003252#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003253void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3254 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003255{
Jerry Yud0766ec2022-09-22 10:46:57 +08003256 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003257}
3258#endif
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3261 mbedtls_ssl_ticket_write_t *f_ticket_write,
3262 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3263 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003264{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003265 conf->f_ticket_write = f_ticket_write;
3266 conf->f_ticket_parse = f_ticket_parse;
3267 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003268}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3273 mbedtls_ssl_export_keys_t *f_export_keys,
3274 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003275{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003276 ssl->f_export_keys = f_export_keys;
3277 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003278}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003279
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003280#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003281void mbedtls_ssl_conf_async_private_cb(
3282 mbedtls_ssl_config *conf,
3283 mbedtls_ssl_async_sign_t *f_async_sign,
3284 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3285 mbedtls_ssl_async_resume_t *f_async_resume,
3286 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003288{
3289 conf->f_async_sign_start = f_async_sign;
3290 conf->f_async_decrypt_start = f_async_decrypt;
3291 conf->f_async_resume = f_async_resume;
3292 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003293 conf->p_async_config_data = async_config_data;
3294}
3295
Gilles Peskine449bd832023-01-11 14:50:10 +01003296void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003297{
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003299}
3300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003302{
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (ssl->handshake == NULL) {
3304 return NULL;
3305 } else {
3306 return ssl->handshake->user_async_ctx;
3307 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003308}
3309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3311 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003312{
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003314 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003316}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003317#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003318
Paul Bakker5121ce52009-01-03 21:22:43 +00003319/*
3320 * SSL get accessors
3321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003323{
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (ssl->session != NULL) {
3325 return ssl->session->verify_result;
3326 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 if (ssl->session_negotiate != NULL) {
3329 return ssl->session_negotiate->verify_result;
3330 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003333}
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003336{
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (ssl == NULL || ssl->session == NULL) {
3338 return 0;
3339 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003342}
3343
Gilles Peskine449bd832023-01-11 14:50:10 +01003344const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003345{
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl == NULL || ssl->session == NULL) {
3347 return NULL;
3348 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003351}
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003354{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3357 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003358 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003360 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003362 }
3363 }
3364#endif
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003367 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003369 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003371 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003373 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003374}
3375
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003376#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003377size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003378{
David Horstmann95d516f2021-05-04 18:36:56 +01003379 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003380 size_t read_mfl;
3381
Jerry Yuddda0502022-12-01 19:43:12 +08003382#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003383 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3385 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3386 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003387 }
Jerry Yuddda0502022-12-01 19:43:12 +08003388#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003389
3390 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 if (ssl->session_out != NULL) {
3392 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3393 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003394 max_len = read_mfl;
3395 }
3396 }
3397
Jerry Yuddda0502022-12-01 19:43:12 +08003398 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (ssl->session_negotiate != NULL) {
3400 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3401 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003402 max_len = read_mfl;
3403 }
3404 }
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003407}
3408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003410{
3411 size_t max_len;
3412
3413 /*
3414 * Assume mfl_code is correct since it was checked when set
3415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003417
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003418 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 if (ssl->session_out != NULL &&
3420 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3421 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003422 }
3423
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003424 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (ssl->session_negotiate != NULL &&
3426 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3427 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003428 }
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003431}
3432#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3433
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003435size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003436{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003437 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3439 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3440 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3441 return 0;
3442 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3445 return ssl->mtu;
3446 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 if (ssl->mtu == 0) {
3449 return ssl->handshake->mtu;
3450 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 return ssl->mtu < ssl->handshake->mtu ?
3453 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003454}
3455#endif /* MBEDTLS_SSL_PROTO_DTLS */
3456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003458{
3459 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3460
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003461#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3462 !defined(MBEDTLS_SSL_PROTO_DTLS)
3463 (void) ssl;
3464#endif
3465
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003466#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003470 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003472#endif
3473
3474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3476 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3477 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003478 const size_t overhead = (size_t) ret;
3479
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 if (ret < 0) {
3481 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003482 }
3483
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 if (mtu <= overhead) {
3485 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3486 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3487 }
3488
3489 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003490 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003492 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003493#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003494
Hanno Becker0defedb2018-08-10 12:35:02 +01003495#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3496 !defined(MBEDTLS_SSL_PROTO_DTLS)
3497 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003498#endif
3499
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003501}
3502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003504{
3505 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3506
3507#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3508 (void) ssl;
3509#endif
3510
3511#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003513
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003515 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003517#endif
3518
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003520}
3521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003523const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003524{
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 if (ssl == NULL || ssl->session == NULL) {
3526 return NULL;
3527 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003528
Hanno Beckere6824572019-02-07 13:18:46 +00003529#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003531#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003533#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003538int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3539 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003540{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003541 int ret;
3542
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003544 dst == NULL ||
3545 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3547 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003548 }
3549
Hanno Beckere810bbc2021-05-14 16:01:05 +01003550 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3551 * idempotent: Each session can only be exported once.
3552 *
3553 * (This is in preparation for TLS 1.3 support where we will
3554 * need the ability to export multiple sessions (aka tickets),
3555 * which will be achieved by calling mbedtls_ssl_get_session()
3556 * multiple times until it fails.)
3557 *
3558 * Check whether we have already exported the current session,
3559 * and fail if so.
3560 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 if (ssl->session->exported == 1) {
3562 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3563 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003564
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3566 if (ret != 0) {
3567 return ret;
3568 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003569
3570 /* Remember that we've exported the session. */
3571 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003575
Paul Bakker5121ce52009-01-03 21:22:43 +00003576/*
Hanno Beckera835da52019-05-16 12:39:07 +01003577 * Define ticket header determining Mbed TLS version
3578 * and structure of the ticket.
3579 */
3580
Hanno Becker94ef3b32019-05-16 12:50:45 +01003581/*
Hanno Becker50b59662019-05-28 14:30:45 +01003582 * Define bitflag determining compile-time settings influencing
3583 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003584 */
3585
Hanno Becker50b59662019-05-28 14:30:45 +01003586#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003587#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003588#else
Hanno Becker3e088662019-05-29 11:10:18 +01003589#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003590#endif /* MBEDTLS_HAVE_TIME */
3591
3592#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003593#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003594#else
Hanno Becker3e088662019-05-29 11:10:18 +01003595#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003596#endif /* MBEDTLS_X509_CRT_PARSE_C */
3597
3598#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003599#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003600#else
Hanno Becker3e088662019-05-29 11:10:18 +01003601#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003602#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3603
3604#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003605#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003606#else
Hanno Becker3e088662019-05-29 11:10:18 +01003607#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003608#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3609
Hanno Becker94ef3b32019-05-16 12:50:45 +01003610#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003611#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003612#else
Hanno Becker3e088662019-05-29 11:10:18 +01003613#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003614#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3615
Hanno Becker94ef3b32019-05-16 12:50:45 +01003616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3617#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3618#else
3619#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3620#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3621
Hanno Becker3e088662019-05-29 11:10:18 +01003622#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3623#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3624#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3625#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003626#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3627#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003628
Hanno Becker50b59662019-05-28 14:30:45 +01003629#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 ((uint16_t) ( \
3631 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3632 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3633 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3634 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3635 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3636 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3637 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003638
Hanno Beckerf8787072019-05-16 12:41:07 +01003639static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003640 MBEDTLS_VERSION_MAJOR,
3641 MBEDTLS_VERSION_MINOR,
3642 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3644 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003645};
Hanno Beckera835da52019-05-16 12:39:07 +01003646
3647/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003648 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003649 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003650 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003651 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003652 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003653 * opaque mbedtls_version[3]; // library version: major, minor, patch
3654 * opaque session_format[2]; // library-version specific 16-bit field
3655 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003656 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003657 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003658 * Note: When updating the format, remember to keep
3659 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003660 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661 * // In this version, `session_format` determines
3662 * // the setting of those compile-time
3663 * // configuration options which influence
3664 * // the structure of mbedtls_ssl_session.
3665 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003666 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003667 * // - TLS 1.2 (0x0303)
3668 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003669 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003670 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003671 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003672 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003673 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003674 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003675 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003676 *
3677 * };
3678 *
3679 * } serialized_session;
3680 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003681 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003682
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003684static int ssl_session_save(const mbedtls_ssl_session *session,
3685 unsigned char omit_header,
3686 unsigned char *buf,
3687 size_t buf_len,
3688 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003689{
3690 unsigned char *p = buf;
3691 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003692 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003693#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3694 size_t out_len;
3695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3696#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 if (session == NULL) {
3698 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3699 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003700
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003702 /*
3703 * Add Mbed TLS version identifier
3704 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003706
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 if (used <= buf_len) {
3708 memcpy(p, ssl_serialized_session_header,
3709 sizeof(ssl_serialized_session_header));
3710 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003711 }
3712 }
3713
3714 /*
3715 * TLS version identifier
3716 */
3717 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 if (used <= buf_len) {
3719 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003720 }
3721
3722 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003723 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 case MBEDTLS_SSL_VERSION_TLS1_2:
3727 used += ssl_tls12_session_save(session, p, remaining_len);
3728 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003729#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3730
Jerry Yu251a12e2022-07-13 15:15:48 +08003731#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 case MBEDTLS_SSL_VERSION_TLS1_3:
3733 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3734 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3735 return ret;
3736 }
3737 used += out_len;
3738 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003739#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 default:
3742 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003743 }
3744
3745 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 if (used > buf_len) {
3747 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3748 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003749
Gilles Peskine449bd832023-01-11 14:50:10 +01003750 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003751}
3752
3753/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003754 * Public wrapper for ssl_session_save()
3755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003756int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3757 unsigned char *buf,
3758 size_t buf_len,
3759 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003760{
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003762}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003763
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003764/*
3765 * Deserialize session, see mbedtls_ssl_session_save() for format.
3766 *
3767 * This internal version is wrapped by a public function that cleans up in
3768 * case of error, and has an extra option omit_header.
3769 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003770MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003771static int ssl_session_load(mbedtls_ssl_session *session,
3772 unsigned char omit_header,
3773 const unsigned char *buf,
3774 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003775{
3776 const unsigned char *p = buf;
3777 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003778 size_t remaining_len;
3779
3780
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 if (session == NULL) {
3782 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3783 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003784
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003786 /*
3787 * Check Mbed TLS version identifier
3788 */
3789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003792 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003793
3794 if (memcmp(p, ssl_serialized_session_header,
3795 sizeof(ssl_serialized_session_header)) != 0) {
3796 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3797 }
3798 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003799 }
3800
3801 /*
3802 * TLS version identifier
3803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (1 > (size_t) (end - p)) {
3805 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3806 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003807 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003808
3809 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003810 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003812#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 case MBEDTLS_SSL_VERSION_TLS1_2:
3814 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003815#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3816
Jerry Yu438ddd82022-07-07 06:55:50 +00003817#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 case MBEDTLS_SSL_VERSION_TLS1_3:
3819 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003820#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3821
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 default:
3823 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003824 }
3825}
3826
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003827/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003828 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3831 const unsigned char *buf,
3832 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003833{
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 if (ret != 0) {
3837 mbedtls_ssl_session_free(session);
3838 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003841}
3842
3843/*
Paul Bakker1961b702013-01-25 14:49:24 +01003844 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003845 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003846MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003847static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003848{
3849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3850
Ronald Cron66dbf912022-02-02 15:33:46 +01003851 /*
3852 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003853 * that were written into the output buffer by the previous handshake step,
3854 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003855 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3856 * We proceed to the next handshake step only when all data from the
3857 * previous one have been sent to the peer, thus we make sure that this is
3858 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3859 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3860 * we have to wait before to go ahead.
3861 * In the case of TLS 1.3, handshake step handlers do not send data to the
3862 * peer. Data are only sent here and through
3863 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003864 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3867 return ret;
3868 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003869
3870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3872 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3873 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3874 return ret;
3875 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003876 }
3877#endif /* MBEDTLS_SSL_PROTO_DTLS */
3878
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003880}
3881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
Hanno Becker41934dd2021-08-07 19:13:43 +01003884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003887 ssl->conf == NULL ||
3888 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3890 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003891 }
3892
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 ret = ssl_prepare_handshake_step(ssl);
3894 if (ret != 0) {
3895 return ret;
3896 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 ret = mbedtls_ssl_handle_pending_alert(ssl);
3899 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003900 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 }
Jerry Yue7047812021-09-13 19:26:39 +08003902
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003903 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3904 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3905 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3909 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003910 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003911
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003913 case MBEDTLS_SSL_HELLO_REQUEST:
3914 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003915 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003916 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003917
Ronald Cron9f0fba32022-02-10 16:45:15 +01003918 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003920 break;
3921
3922 default:
3923#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3925 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3926 } else {
3927 ret = mbedtls_ssl_handshake_client_step(ssl);
3928 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003929#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003931#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003933#endif
3934 }
Jerry Yub9930e72021-08-06 17:11:51 +08003935 }
Ronald Cron6291b232023-03-08 15:51:25 +01003936#endif /* MBEDTLS_SSL_CLI_C */
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01003940#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
3941 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01003943 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 ret = mbedtls_ssl_handshake_server_step(ssl);
3945 }
Ronald Cron6291b232023-03-08 15:51:25 +01003946#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3947 ret = mbedtls_ssl_handshake_server_step(ssl);
3948#else
3949 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003950#endif
Ronald Cron6291b232023-03-08 15:51:25 +01003951 }
3952#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003955 /* handshake_step return error. And it is same
3956 * with alert_reason.
3957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 if (ssl->send_alert) {
3959 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003960 goto cleanup;
3961 }
3962 }
3963
3964cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003966}
3967
3968/*
3969 * Perform the SSL handshake
3970 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003971int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003972{
3973 int ret = 0;
3974
Hanno Beckera817ea42020-10-20 15:20:23 +01003975 /* Sanity checks */
3976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 if (ssl == NULL || ssl->conf == NULL) {
3978 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3979 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003980
Hanno Beckera817ea42020-10-20 15:20:23 +01003981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3983 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3984 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3985 "mbedtls_ssl_set_timer_cb() for DTLS"));
3986 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003987 }
3988#endif /* MBEDTLS_SSL_PROTO_DTLS */
3989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003991
Hanno Beckera817ea42020-10-20 15:20:23 +01003992 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3994 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003995
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003997 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 }
Paul Bakker1961b702013-01-25 14:49:24 +01003999 }
4000
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004004}
4005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006#if defined(MBEDTLS_SSL_RENEGOTIATION)
4007#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004008/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004009 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004010 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004011MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004012static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004013{
Janos Follath865b3eb2019-12-16 11:46:15 +00004014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004015
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004017
4018 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004019 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4020 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4023 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4024 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004025 }
4026
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004030}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004032
4033/*
4034 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035 * - any side: calling mbedtls_ssl_renegotiate(),
4036 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4037 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004038 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004039 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004041 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004042int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004043{
Janos Follath865b3eb2019-12-16 11:46:15 +00004044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004045
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 if ((ret = ssl_handshake_init(ssl)) != 0) {
4049 return ret;
4050 }
Paul Bakker48916f92012-09-16 19:57:18 +00004051
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004052 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4053 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4056 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4057 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004058 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004060 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004062 }
4063#endif
4064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4066 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4069 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4070 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004071 }
4072
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004074
Gilles Peskine449bd832023-01-11 14:50:10 +01004075 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004076}
4077
4078/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004079 * Renegotiate current connection on client,
4080 * or request renegotiation on server
4081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004082int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004084 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004085
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (ssl == NULL || ssl->conf == NULL) {
4087 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4088 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004091 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004092 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4093 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4094 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4095 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004098
4099 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 if (ssl->out_left != 0) {
4101 return mbedtls_ssl_flush_output(ssl);
4102 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004103
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004109 /*
4110 * On client, either start the renegotiation process or,
4111 * if already in progress, continue the handshake
4112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4114 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004116 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004117
4118 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4120 return ret;
4121 }
4122 } else {
4123 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4124 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4125 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004126 }
4127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004129
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004133
Gilles Peskine449bd832023-01-11 14:50:10 +01004134void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004135{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004136 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004139 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004141
Valerio Setti6f0441d2023-07-03 12:11:36 +02004142#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Brett Warrene0edc842021-08-17 09:53:13 +01004143#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (ssl->handshake->group_list_heap_allocated) {
4145 mbedtls_free((void *) handshake->group_list);
4146 }
Brett Warrene0edc842021-08-17 09:53:13 +01004147 handshake->group_list = NULL;
4148#endif /* MBEDTLS_DEPRECATED_REMOVED */
Valerio Setti6f0441d2023-07-03 12:11:36 +02004149#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Brett Warrene0edc842021-08-17 09:53:13 +01004150
Ronald Crone68ab4f2022-10-05 12:46:29 +02004151#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004152#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004153 if (ssl->handshake->sig_algs_heap_allocated) {
4154 mbedtls_free((void *) handshake->sig_algs);
4155 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004156 handshake->sig_algs = NULL;
4157#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004158#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (ssl->handshake->certificate_request_context) {
4160 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004161 }
4162#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004163#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004164
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004165#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4167 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004168 handshake->async_in_progress = 0;
4169 }
4170#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4171
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004172#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004173#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004175#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004176 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004177#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004178#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004179#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004180#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004182#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004183 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004184#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004185#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004187#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004189#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004190#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004191 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004193#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004194
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004195#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004198 /*
4199 * Opaque keys are not stored in the handshake's data and it's the user
4200 * responsibility to destroy them. Clear ones, instead, are created by
4201 * the TLS library and should be destroyed at the same level
4202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4204 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004205 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004206 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4207#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004208 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004209#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004210#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004211 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004212 handshake->ecjpake_cache = NULL;
4213 handshake->ecjpake_cache_len = 0;
4214#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004215#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004216
Valerio Setti7aeec542023-07-05 18:57:21 +02004217#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004218 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004219 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004220 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004222#endif
4223
Ronald Cron73fe8df2022-10-05 14:31:43 +02004224#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004225#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004227 /* The maintenance of the external PSK key slot is the
4228 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 if (ssl->handshake->psk_opaque_is_internal) {
4230 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004231 ssl->handshake->psk_opaque_is_internal = 0;
4232 }
4233 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4234 }
Neil Armstronge952a302022-05-03 10:22:14 +02004235#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (handshake->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004237 mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004238 }
Neil Armstronge952a302022-05-03 10:22:14 +02004239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004240#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4243 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004244 /*
4245 * Free only the linked list wrapper, not the keys themselves
4246 * since the belong to the SNI callback
4247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004248 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004249#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004250
Gilles Peskineeccd8882020-03-10 12:19:08 +01004251#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4253 if (handshake->ecrs_peer_cert != NULL) {
4254 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4255 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004256 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004257#endif
4258
Hanno Becker75173122019-02-06 16:18:31 +00004259#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4260 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004262#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4263
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004264#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004265 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4266 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004267#endif /* MBEDTLS_SSL_CLI_C &&
4268 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004269
4270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 mbedtls_ssl_flight_free(handshake->flight);
4272 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004273#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004274
Valerio Settiea59c432023-07-25 11:14:03 +02004275#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004276 if (handshake->xxdh_psa_privkey_is_external == 0) {
4277 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 }
Valerio Settiea59c432023-07-25 11:14:03 +02004279#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004280
Ronald Cron6f135e12021-12-08 16:57:54 +01004281#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 mbedtls_ssl_transform_free(handshake->transform_handshake);
4283 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004284#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4286 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004287#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004288#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004289
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004290
4291#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4292 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4293 * processes datagrams and the fact that a datagram is allowed to have
4294 * several records in it, it is possible that the I/O buffers are not
4295 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4297 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004298#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004299
Jerry Yuba9c7272021-10-30 11:54:10 +08004300 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004301 mbedtls_platform_zeroize(handshake,
4302 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004303}
4304
Gilles Peskine449bd832023-01-11 14:50:10 +01004305void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004306{
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004308 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004313#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004314
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004315#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004316#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4317 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004319#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004321#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004324}
4325
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004326#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004327
4328#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4329#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4330#else
4331#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4332#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4333
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004334#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004335
4336#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4337#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4338#else
4339#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4340#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4341
4342#if defined(MBEDTLS_SSL_ALPN)
4343#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4344#else
4345#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4346#endif /* MBEDTLS_SSL_ALPN */
4347
4348#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4349#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4350#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4351#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4352
4353#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 ((uint32_t) ( \
4355 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4356 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4357 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4358 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4359 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4360 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4361 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4362 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004363
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004364static unsigned char ssl_serialized_context_header[] = {
4365 MBEDTLS_VERSION_MAJOR,
4366 MBEDTLS_VERSION_MINOR,
4367 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4369 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4370 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4371 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4372 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004373};
4374
Paul Bakker5121ce52009-01-03 21:22:43 +00004375/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004376 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004377 *
4378 * The format of the serialized data is:
4379 * (in the presentation language of TLS, RFC 8446 section 3)
4380 *
4381 * // header
4382 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004383 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004384 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004385 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004386 * Note: When updating the format, remember to keep these
4387 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004388 *
4389 * // session sub-structure
4390 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4391 * // transform sub-structure
4392 * uint8 random[64]; // ServerHello.random+ClientHello.random
4393 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4394 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4395 * // fields from ssl_context
4396 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4397 * uint64 in_window_top; // DTLS: last validated record seq_num
4398 * uint64 in_window; // DTLS: bitmask for replay protection
4399 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4400 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4401 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4402 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4403 *
4404 * Note that many fields of the ssl_context or sub-structures are not
4405 * serialized, as they fall in one of the following categories:
4406 *
4407 * 1. forced value (eg in_left must be 0)
4408 * 2. pointer to dynamically-allocated memory (eg session, transform)
4409 * 3. value can be re-derived from other data (eg session keys from MS)
4410 * 4. value was temporary (eg content of input buffer)
4411 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004413int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4414 unsigned char *buf,
4415 size_t buf_len,
4416 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004417{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004418 unsigned char *p = buf;
4419 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004420 size_t session_len;
4421 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004422
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004423 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004424 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4425 * this function's documentation.
4426 *
4427 * These are due to assumptions/limitations in the implementation. Some of
4428 * them are likely to stay (no handshake in progress) some might go away
4429 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004430 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004431 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4433 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4434 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (ssl->handshake != NULL) {
4437 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4438 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004439 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004440 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (ssl->transform == NULL || ssl->session == NULL) {
4442 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4443 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004444 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004445 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (mbedtls_ssl_check_pending(ssl) != 0) {
4447 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4448 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004449 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (ssl->out_left != 0) {
4451 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4452 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004453 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004454 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4456 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4457 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004458 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004459 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4461 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4462 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004463 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004464 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4466 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4467 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004468 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004469 /* Renegotiation must not be enabled */
4470#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4472 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4473 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004474 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004475#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004476
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004477 /*
4478 * Version and format identifier
4479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004481
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 if (used <= buf_len) {
4483 memcpy(p, ssl_serialized_context_header,
4484 sizeof(ssl_serialized_context_header));
4485 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004486 }
4487
4488 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004489 * Session (length + data)
4490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4492 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4493 return ret;
4494 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004495
4496 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 if (used <= buf_len) {
4498 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004499 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 ret = ssl_session_save(ssl->session, 1,
4502 p, session_len, &session_len);
4503 if (ret != 0) {
4504 return ret;
4505 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004506
4507 p += session_len;
4508 }
4509
4510 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004511 * Transform
4512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 used += sizeof(ssl->transform->randbytes);
4514 if (used <= buf_len) {
4515 memcpy(p, ssl->transform->randbytes,
4516 sizeof(ssl->transform->randbytes));
4517 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004518 }
4519
4520#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00004521 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004523 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004525 p += ssl->transform->in_cid_len;
4526
4527 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004529 p += ssl->transform->out_cid_len;
4530 }
4531#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4532
4533 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004534 * Saved fields from top-level ssl_context structure
4535 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004536 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if (used <= buf_len) {
4538 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004539 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004540 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004541
4542#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4543 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004544 if (used <= buf_len) {
4545 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004546 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004547
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004549 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004550 }
4551#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4552
4553#if defined(MBEDTLS_SSL_PROTO_DTLS)
4554 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004555 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004556 *p++ = ssl->disable_datagram_packing;
4557 }
4558#endif /* MBEDTLS_SSL_PROTO_DTLS */
4559
Jerry Yuae0b2e22021-10-08 15:21:19 +08004560 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 if (used <= buf_len) {
4562 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004563 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004564 }
4565
4566#if defined(MBEDTLS_SSL_PROTO_DTLS)
4567 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (used <= buf_len) {
4569 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004570 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004571 }
4572#endif /* MBEDTLS_SSL_PROTO_DTLS */
4573
4574#if defined(MBEDTLS_SSL_ALPN)
4575 {
4576 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004578 : 0;
4579
4580 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004582 *p++ = alpn_len;
4583
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 if (ssl->alpn_chosen != NULL) {
4585 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004586 p += alpn_len;
4587 }
4588 }
4589 }
4590#endif /* MBEDTLS_SSL_ALPN */
4591
4592 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004593 * Done
4594 */
4595 *olen = used;
4596
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if (used > buf_len) {
4598 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4599 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004604}
4605
4606/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004607 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004608 *
4609 * This internal version is wrapped by a public function that cleans up in
4610 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004611 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004612MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004613static int ssl_context_load(mbedtls_ssl_context *ssl,
4614 const unsigned char *buf,
4615 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004616{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004617 const unsigned char *p = buf;
4618 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004619 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004621#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004622 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004623#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004624
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004625 /*
4626 * The context should have been freshly setup or reset.
4627 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004628 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004629 * renegotiating, or if the user mistakenly loaded a session first.)
4630 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4632 ssl->session != NULL) {
4633 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004634 }
4635
4636 /*
4637 * We can't check that the config matches the initial one, but we can at
4638 * least check it matches the requirements for serializing.
4639 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01004640 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004641#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004642 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004643#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01004644 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
4645 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4646 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
4647 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004649 }
4650
Gilles Peskine449bd832023-01-11 14:50:10 +01004651 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004652
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004653 /*
4654 * Check version identifier
4655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004658 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004659
4660 if (memcmp(p, ssl_serialized_context_header,
4661 sizeof(ssl_serialized_context_header)) != 0) {
4662 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4663 }
4664 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004665
4666 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004667 * Session
4668 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < 4) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004672
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004673 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004674 p += 4;
4675
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004676 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004677 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004678 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004679 ssl->session_in = ssl->session;
4680 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004681 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if ((size_t) (end - p) < session_len) {
4684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4685 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 ret = ssl_session_load(ssl->session, 1, p, session_len);
4688 if (ret != 0) {
4689 mbedtls_ssl_session_free(ssl->session);
4690 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004691 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004692
4693 p += session_len;
4694
4695 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004696 * Transform
4697 */
4698
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004699 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004700 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004701#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004702 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004703 ssl->transform_in = ssl->transform;
4704 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004705 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004706#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004707
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4710 if (prf_func == NULL) {
4711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4712 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004713
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004714 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4717 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 ret = ssl_tls12_populate_transform(ssl->transform,
4720 ssl->session->ciphersuite,
4721 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004722#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004724#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004725 prf_func,
4726 p, /* currently pointing to randbytes */
4727 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4728 ssl->conf->endpoint,
4729 ssl);
4730 if (ret != 0) {
4731 return ret;
4732 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004733#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004735
4736#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4737 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 if ((size_t) (end - p) < 1) {
4739 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4740 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004741
4742 ssl->transform->in_cid_len = *p++;
4743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4746 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004749 p += ssl->transform->in_cid_len;
4750
4751 ssl->transform->out_cid_len = *p++;
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4754 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4755 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004756
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004758 p += ssl->transform->out_cid_len;
4759#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4760
4761 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004762 * Saved fields from top-level ssl_context structure
4763 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if ((size_t) (end - p) < 4) {
4765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4766 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004767
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004768 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004769 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004770
4771#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 if ((size_t) (end - p) < 16) {
4773 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4774 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004775
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004776 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004777 p += 8;
4778
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01004779 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004780 p += 8;
4781#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4782
4783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if ((size_t) (end - p) < 1) {
4785 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4786 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004787
4788 ssl->disable_datagram_packing = *p++;
4789#endif /* MBEDTLS_SSL_PROTO_DTLS */
4790
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4792 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4793 }
4794 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4795 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004796
4797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if ((size_t) (end - p) < 2) {
4799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4800 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004801
Dave Rodgmana3d0f612023-11-03 23:34:02 +00004802 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004803 p += 2;
4804#endif /* MBEDTLS_SSL_PROTO_DTLS */
4805
4806#if defined(MBEDTLS_SSL_ALPN)
4807 {
4808 uint8_t alpn_len;
4809 const char **cur;
4810
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 if ((size_t) (end - p) < 1) {
4812 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4813 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004814
4815 alpn_len = *p++;
4816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004818 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4820 if (strlen(*cur) == alpn_len &&
4821 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004822 ssl->alpn_chosen = *cur;
4823 break;
4824 }
4825 }
4826 }
4827
4828 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4830 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4831 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004832
4833 p += alpn_len;
4834 }
4835#endif /* MBEDTLS_SSL_ALPN */
4836
4837 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004838 * Forced fields from top-level ssl_context structure
4839 *
4840 * Most of them already set to the correct value by mbedtls_ssl_init() and
4841 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4842 */
4843 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004844 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004845
Hanno Becker361b10d2019-08-30 10:42:49 +01004846 /* Adjust pointers for header fields of outgoing records to
4847 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004849
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004850#if defined(MBEDTLS_SSL_PROTO_DTLS)
4851 ssl->in_epoch = 1;
4852#endif
4853
4854 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4855 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004856 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4857 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 if (ssl->handshake != NULL) {
4859 mbedtls_ssl_handshake_free(ssl);
4860 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004861 ssl->handshake = NULL;
4862 }
4863
4864 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004865 * Done - should have consumed entire buffer
4866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (p != end) {
4868 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4869 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004872}
4873
4874/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004875 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004877int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4878 const unsigned char *buf,
4879 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004880{
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (ret != 0) {
4884 mbedtls_ssl_free(context);
4885 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004888}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004889#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004890
4891/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 * Free an SSL context
4893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004894void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004895{
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004897 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004903#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4904 size_t out_buf_len = ssl->out_buf_len;
4905#else
4906 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4907#endif
4908
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004909 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004910 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 }
4912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004914#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4915 size_t in_buf_len = ssl->in_buf_len;
4916#else
4917 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4918#endif
4919
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004920 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004921 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004922 }
4923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (ssl->transform) {
4925 mbedtls_ssl_transform_free(ssl->transform);
4926 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004927 }
4928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 if (ssl->handshake) {
4930 mbedtls_ssl_handshake_free(ssl);
4931 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004932
4933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4935 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004936#endif
4937
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 mbedtls_ssl_session_free(ssl->session_negotiate);
4939 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004940 }
4941
Ronald Cron6f135e12021-12-08 16:57:54 +01004942#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 mbedtls_ssl_transform_free(ssl->transform_application);
4944 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004945#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004946
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 if (ssl->session) {
4948 mbedtls_ssl_session_free(ssl->session);
4949 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004950 }
4951
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004952#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01004954 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004956#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004957
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004958#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004960#endif
4961
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004963
Paul Bakker86f04f42013-02-14 11:20:09 +01004964 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004966}
4967
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004968/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004969 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004970 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004971void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004972{
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004974}
4975
Gilles Peskineae270bf2021-06-02 00:05:29 +02004976/* The selection should be the same as mbedtls_x509_crt_profile_default in
4977 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004978 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004979 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004980 * about this list.
4981 */
Brett Warrene0edc842021-08-17 09:53:13 +01004982static uint16_t ssl_preset_default_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02004983#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Brett Warrene0edc842021-08-17 09:53:13 +01004984 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004985#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02004986#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01004987 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004988#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02004989#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01004990 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004991#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02004992#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Brett Warrene0edc842021-08-17 09:53:13 +01004993 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004994#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02004995#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Brett Warrene0edc842021-08-17 09:53:13 +01004996 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004997#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02004998#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01004999 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005000#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005001#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005002 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005003#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005004#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005005 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005006#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005007#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005008 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5009 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5010 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5011 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5012 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5013#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005014 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005015};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005016
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005017static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005018 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5019 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5020 0
5021};
5022
Ronald Crone68ab4f2022-10-05 12:46:29 +02005023#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005024
Jerry Yu909df7b2022-01-22 11:56:27 +08005025/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005026 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005027 * rules SHOULD be upheld.
5028 * - No duplicate entries.
5029 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005030 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005031 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005032 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005033static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005034
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005035#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005036 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005037 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005038 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005039 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5040#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005041
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005042#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005043 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005044 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005045 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005046 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5047#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005048
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005049#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005050 defined(MBEDTLS_MD_CAN_SHA512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005051 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005052 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005053 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5054#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005055
Gilles Peskine449bd832023-01-11 14:50:10 +01005056#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005057 defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005058 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005059#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005060 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005063 defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005064 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005065#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005066 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005067
Gilles Peskine449bd832023-01-11 14:50:10 +01005068#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005069 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005070 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005071#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005072 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005073
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005074#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005075 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005076#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005077
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005078#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005079 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005080#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005081
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005082#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005083 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005084#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005085
Gabor Mezei15b95a62022-05-09 16:37:58 +02005086 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005087};
5088
5089/* NOTICE: see above */
5090#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5091static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005092#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005093#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005095#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005096#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5097 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5098#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005099#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005101#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005102#endif /* MBEDTLS_MD_CAN_SHA512*/
5103#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005104#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005106#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005107#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5108 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5109#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005110#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005112#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005113#endif /* MBEDTLS_MD_CAN_SHA384*/
5114#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005115#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005117#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005118#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5119 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5120#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005121#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005123#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005124#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005125 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005126};
5127#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5128/* NOTICE: see above */
5129static uint16_t ssl_preset_suiteb_sig_algs[] = {
5130
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005131#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005132 defined(MBEDTLS_MD_CAN_SHA256) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005133 defined(MBEDTLS_ECP_HAVE_SECP256R1)
Jerry Yu909df7b2022-01-22 11:56:27 +08005134 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005135 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5136#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005137
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005138#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Valerio Setti45d56f32023-07-13 17:23:20 +02005139 defined(MBEDTLS_MD_CAN_SHA384) && \
Valerio Settidb6b4db2023-09-01 09:20:51 +02005140 defined(MBEDTLS_ECP_HAVE_SECP384R1)
Jerry Yu53037892022-01-25 11:02:06 +08005141 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005142 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5143#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005146 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005147 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005148#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005149 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08005150
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005151#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu53037892022-01-25 11:02:06 +08005152 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005153#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu53037892022-01-25 11:02:06 +08005154
Gabor Mezei15b95a62022-05-09 16:37:58 +02005155 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005156};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005157
Jerry Yu909df7b2022-01-22 11:56:27 +08005158/* NOTICE: see above */
5159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5160static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005161#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005162#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005164#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005165#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005167#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005168#endif /* MBEDTLS_MD_CAN_SHA256*/
5169#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005170#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005172#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005173#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005175#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005176#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005177 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005178};
Jerry Yu909df7b2022-01-22 11:56:27 +08005179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5180
Ronald Crone68ab4f2022-10-05 12:46:29 +02005181#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005182
Brett Warrene0edc842021-08-17 09:53:13 +01005183static uint16_t ssl_preset_suiteb_groups[] = {
Valerio Settidb6b4db2023-09-01 09:20:51 +02005184#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005185 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005186#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005187#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Brett Warrene0edc842021-08-17 09:53:13 +01005188 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005189#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005190 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005191};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005192
Ronald Crone68ab4f2022-10-05 12:46:29 +02005193#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005194/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005195 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005197static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005198{
5199 size_t i, j;
5200 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5203 for (j = 0; j < i; j++) {
5204 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005205 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 }
5207 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5208 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5209 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005210 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005211 }
5212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005213 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005214}
5215
Ronald Crone68ab4f2022-10-05 12:46:29 +02005216#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005217
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005218/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005219 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5222 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005223{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005224#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005226#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005227
Ronald Crone68ab4f2022-10-05 12:46:29 +02005228#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005229 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5230 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5231 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005232 }
5233
Gilles Peskine449bd832023-01-11 14:50:10 +01005234 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5235 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5236 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005237 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005238
5239#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5241 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5242 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005243 }
5244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5246 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5247 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005248 }
5249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005250#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005251
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005252 /* Use the functions here so that they are covered in tests,
5253 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 mbedtls_ssl_conf_endpoint(conf, endpoint);
5255 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005256
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005257 /*
5258 * Things that are common to all presets
5259 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005260#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005262 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5263#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5264 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5265#endif
5266 }
5267#endif
5268
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005269#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5270 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5271#endif
5272
5273#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5274 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5275#endif
5276
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005277#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005278 conf->f_cookie_write = ssl_cookie_write_dummy;
5279 conf->f_cookie_check = ssl_cookie_check_dummy;
5280#endif
5281
5282#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5283 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5284#endif
5285
Janos Follath088ce432017-04-10 12:42:31 +01005286#if defined(MBEDTLS_SSL_SRV_C)
5287 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005288 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005289#endif
5290
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005291#if defined(MBEDTLS_SSL_PROTO_DTLS)
5292 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5293 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5294#endif
5295
5296#if defined(MBEDTLS_SSL_RENEGOTIATION)
5297 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 memset(conf->renego_period, 0x00, 2);
5299 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005300#endif
5301
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005302#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005304 const unsigned char dhm_p[] =
5305 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5306 const unsigned char dhm_g[] =
5307 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005308
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5310 dhm_p, sizeof(dhm_p),
5311 dhm_g, sizeof(dhm_g))) != 0) {
5312 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005313 }
5314 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005315#endif
5316
Ronald Cron6f135e12021-12-08 16:57:54 +01005317#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005318
5319#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005320 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005321#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005322 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005323#endif
5324#endif /* MBEDTLS_SSL_EARLY_DATA */
5325
Jerry Yud0766ec2022-09-22 10:46:57 +08005326#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005327 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005329#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005330 /*
5331 * Allow all TLS 1.3 key exchange modes by default.
5332 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005333 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005334#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005338 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005339 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005340#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005342#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005344#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005345 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5346 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005347#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5348 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5349 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5350#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5351 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5352 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5353#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005355#endif
5356 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005357
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005358 /*
5359 * Preset-specific defaults
5360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005362 /*
5363 * NSA Suite B
5364 */
5365 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005366
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005367 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005368
5369#if defined(MBEDTLS_X509_CRT_PARSE_C)
5370 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005371#endif
5372
Ronald Crone68ab4f2022-10-05 12:46:29 +02005373#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005374#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005376 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005378#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005380#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005381
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02005382#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01005383 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005384#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005385 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005386 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005387
5388 /*
5389 * Default
5390 */
5391 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005392
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005393 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005394
5395#if defined(MBEDTLS_X509_CRT_PARSE_C)
5396 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5397#endif
5398
Ronald Crone68ab4f2022-10-05 12:46:29 +02005399#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005400#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005402 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005404#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005406#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005407
Manuel Pégourié-Gonnardf07ce3b2023-09-22 11:53:41 +02005408#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Brett Warrene0edc842021-08-17 09:53:13 +01005409 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005410#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005411 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005412
5413#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5414 conf->dhm_min_bitlen = 1024;
5415#endif
5416 }
5417
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005419}
5420
5421/*
5422 * Free mbedtls_ssl_config
5423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005424void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005425{
5426#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 mbedtls_mpi_free(&conf->dhm_P);
5428 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005429#endif
5430
Ronald Cron73fe8df2022-10-05 14:31:43 +02005431#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005434 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5435 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005436#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005438 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005439 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005440 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005441 }
5442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005444 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005445 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005446 conf->psk_identity_len = 0;
5447 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005448#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005449
5450#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005452#endif
5453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005455}
5456
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005457#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02005458 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005459/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005463{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5466 return MBEDTLS_SSL_SIG_RSA;
5467 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005468#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02005469#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5471 return MBEDTLS_SSL_SIG_ECDSA;
5472 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005473#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005475}
5476
Gilles Peskine449bd832023-01-11 14:50:10 +01005477unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005478{
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005480 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005482 case MBEDTLS_PK_ECDSA:
5483 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005485 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005487 }
5488}
5489
Gilles Peskine449bd832023-01-11 14:50:10 +01005490mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005491{
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493#if defined(MBEDTLS_RSA_C)
5494 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005496#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02005497#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005500#endif
5501 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005503 }
5504}
Valerio Settie9646ec2023-08-02 20:02:28 +02005505#endif /* MBEDTLS_PK_C &&
5506 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005507
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005508/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005509 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005511mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005512{
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005514#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005517#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005518#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005519 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005521#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005522#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005523 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005525#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005526#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005527 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005529#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005530#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005533#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005534#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005535 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005537#endif
5538 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005540 }
5541}
5542
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005543/*
5544 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005546unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005547{
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005549#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005550 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005552#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005553#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005554 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005556#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005557#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005558 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005560#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005561#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005562 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005564#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005565#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005566 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005568#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005569#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005570 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005572#endif
5573 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005575 }
5576}
5577
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005578/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005579 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005580 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005581 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005582int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005583{
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 if (group_list == NULL) {
5587 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005588 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 for (; *group_list != 0; group_list++) {
5591 if (*group_list == tls_id) {
5592 return 0;
5593 }
5594 }
5595
5596 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005597}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005598
Valerio Setti49e69072023-06-27 17:27:51 +02005599#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005600/*
5601 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005603int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005604{
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005608 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005612}
Valerio Setti49e69072023-06-27 17:27:51 +02005613#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti67419f02023-01-04 16:12:42 +01005614
Valerio Setti18c9fed2022-12-30 17:44:24 +01005615static const struct {
5616 uint16_t tls_id;
5617 mbedtls_ecp_group_id ecp_group_id;
5618 psa_ecc_family_t psa_family;
5619 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005620} tls_id_match_table[] =
5621{
Valerio Settidb6b4db2023-09-01 09:20:51 +02005622#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005623 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005624#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005625#if defined(MBEDTLS_ECP_HAVE_BP512R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005626 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005627#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005628#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005629 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005630#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005631#if defined(MBEDTLS_ECP_HAVE_BP384R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005632 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005633#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005634#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005635 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005636#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005637#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005638 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005639#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005640#if defined(MBEDTLS_ECP_HAVE_BP256R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005641 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005642#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005643#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005644 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005645#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005646#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005647 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005648#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005649#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005650 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005651#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005652#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
Valerio Settiacd32c02023-06-29 18:06:29 +02005653 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005654#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005655#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
Valerio Settiacd32c02023-06-29 18:06:29 +02005656 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005657#endif
Valerio Settidb6b4db2023-09-01 09:20:51 +02005658#if defined(MBEDTLS_ECP_HAVE_CURVE448)
Valerio Settiacd32c02023-06-29 18:06:29 +02005659 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005660#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02005661 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005662};
5663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02005665 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005667{
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5669 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02005670 if (type != NULL) {
5671 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 }
5673 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005674 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005676 return PSA_SUCCESS;
5677 }
5678 }
5679
5680 return PSA_ERROR_NOT_SUPPORTED;
5681}
5682
Gilles Peskine449bd832023-01-11 14:50:10 +01005683mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005684{
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5686 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005687 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005689 }
5690
5691 return MBEDTLS_ECP_DP_NONE;
5692}
5693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005695{
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5697 i++) {
5698 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005699 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005701 }
5702
5703 return 0;
5704}
5705
Valerio Setti67419f02023-01-04 16:12:42 +01005706#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02005707static const struct {
5708 uint16_t tls_id;
5709 const char *name;
5710} tls_id_curve_name_table[] =
5711{
Valerio Setti54e23792023-07-07 10:49:27 +02005712 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
5713 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
5714 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
5715 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
5716 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
5717 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
5718 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
5719 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
5720 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
5721 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
5722 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
5723 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
5724 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02005725 { 0, NULL },
5726};
5727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005729{
Valerio Settiacd32c02023-06-29 18:06:29 +02005730 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
5731 if (tls_id_curve_name_table[i].tls_id == tls_id) {
5732 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005734 }
5735
5736 return NULL;
5737}
Valerio Setti67419f02023-01-04 16:12:42 +01005738#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005740#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005741int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5742 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5743 int cert_endpoint,
5744 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005745{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005746 int ret = 0;
Dave Rodgmanc37ad442023-11-03 23:36:06 +00005747 unsigned int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005748 const char *ext_oid;
5749 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005752 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754 case MBEDTLS_KEY_EXCHANGE_RSA:
5755 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005756 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005757 break;
5758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5760 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5761 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5762 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005763 break;
5764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005765 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5766 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005767 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005768 break;
5769
5770 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 case MBEDTLS_KEY_EXCHANGE_NONE:
5772 case MBEDTLS_KEY_EXCHANGE_PSK:
5773 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5774 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005775 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005776 usage = 0;
5777 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5780 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005781 }
5782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005784 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005785 ret = -1;
5786 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005789 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5791 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005792 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005794 }
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005797 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005798 ret = -1;
5799 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005804
Jerry Yu148165c2021-09-24 23:20:59 +08005805#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005806int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5807 const mbedtls_md_type_t md,
5808 unsigned char *dst,
5809 size_t dst_len,
5810 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005811{
Ronald Cronf6893e12022-01-07 22:09:01 +01005812 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5813 psa_hash_operation_t *hash_operation_to_clone;
5814 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5815
Jerry Yu148165c2021-09-24 23:20:59 +08005816 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005819#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 case MBEDTLS_MD_SHA384:
5821 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5822 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005823#endif
5824
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005825#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 case MBEDTLS_MD_SHA256:
5827 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5828 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005829#endif
5830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 default:
5832 goto exit;
5833 }
5834
5835 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5836 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005837 goto exit;
5838 }
5839
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5841 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005842 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005844
5845exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005846#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5847 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005848 (void) ssl;
5849#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005850 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005851}
5852#else /* MBEDTLS_USE_PSA_CRYPTO */
5853
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005854#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005855MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005856static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5857 unsigned char *dst,
5858 size_t dst_len,
5859 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005860{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005861 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005862 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 if (dst_len < 48) {
5865 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5866 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005867
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005868 mbedtls_md_init(&sha384);
5869 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005870 if (ret != 0) {
5871 goto exit;
5872 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005873 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005874 if (ret != 0) {
5875 goto exit;
5876 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005877
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005878 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005880 goto exit;
5881 }
5882
5883 *olen = 48;
5884
5885exit:
5886
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005887 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005889}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005890#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005891
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005892#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005893MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005894static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5895 unsigned char *dst,
5896 size_t dst_len,
5897 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005898{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005899 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005900 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 if (dst_len < 32) {
5903 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5904 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005905
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005906 mbedtls_md_init(&sha256);
5907 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5908 if (ret != 0) {
5909 goto exit;
5910 }
5911 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5912 if (ret != 0) {
5913 goto exit;
5914 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005915
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005916 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5917 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005918 goto exit;
5919 }
5920
5921 *olen = 32;
5922
5923exit:
5924
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005925 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005926 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005927}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005928#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5931 const mbedtls_md_type_t md,
5932 unsigned char *dst,
5933 size_t dst_len,
5934 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005935{
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005937
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005938#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 case MBEDTLS_MD_SHA384:
5940 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005941#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005942
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005943#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 case MBEDTLS_MD_SHA256:
5945 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005946#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005949#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5950 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 (void) ssl;
5952 (void) dst;
5953 (void) dst_len;
5954 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005955#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005957 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005959}
XiaokangQian647719a2021-12-07 09:16:29 +00005960
Jerry Yu148165c2021-09-24 23:20:59 +08005961#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005962
Ronald Crone68ab4f2022-10-05 12:46:29 +02005963#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005964/* mbedtls_ssl_parse_sig_alg_ext()
5965 *
5966 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5967 * value (TLS 1.3 RFC8446):
5968 * enum {
5969 * ....
5970 * ecdsa_secp256r1_sha256( 0x0403 ),
5971 * ecdsa_secp384r1_sha384( 0x0503 ),
5972 * ecdsa_secp521r1_sha512( 0x0603 ),
5973 * ....
5974 * } SignatureScheme;
5975 *
5976 * struct {
5977 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5978 * } SignatureSchemeList;
5979 *
5980 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5981 * value (TLS 1.2 RFC5246):
5982 * enum {
5983 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5984 * sha512(6), (255)
5985 * } HashAlgorithm;
5986 *
5987 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5988 * SignatureAlgorithm;
5989 *
5990 * struct {
5991 * HashAlgorithm hash;
5992 * SignatureAlgorithm signature;
5993 * } SignatureAndHashAlgorithm;
5994 *
5995 * SignatureAndHashAlgorithm
5996 * supported_signature_algorithms<2..2^16-2>;
5997 *
5998 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5999 * generalization of the TLS 1.2 signature algorithm extension.
6000 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6001 * `SignatureScheme` field of TLS 1.3
6002 *
6003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006004int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6005 const unsigned char *buf,
6006 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006007{
6008 const unsigned char *p = buf;
6009 size_t supported_sig_algs_len = 0;
6010 const unsigned char *supported_sig_algs_end;
6011 uint16_t sig_alg;
6012 uint32_t common_idx = 0;
6013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6015 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006016 p += 2;
6017
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 memset(ssl->handshake->received_sig_algs, 0,
6019 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006022 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 while (p < supported_sig_algs_end) {
6024 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6025 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006026 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6028 sig_alg,
6029 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006030#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6032 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6033 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006034 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006035 }
6036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006037
Gilles Peskine449bd832023-01-11 14:50:10 +01006038 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6039 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006042 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6043 common_idx += 1;
6044 }
6045 }
6046 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 if (p != end) {
6048 MBEDTLS_SSL_DEBUG_MSG(1,
6049 ("Signature algorithms extension length misaligned"));
6050 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6051 MBEDTLS_ERR_SSL_DECODE_ERROR);
6052 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006053 }
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 if (common_idx == 0) {
6056 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6057 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6058 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6059 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006060 }
6061
Gabor Mezei15b95a62022-05-09 16:37:58 +02006062 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006064}
6065
Ronald Crone68ab4f2022-10-05 12:46:29 +02006066#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006067
Jerry Yudc7bd172022-02-17 13:44:15 +08006068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6069
6070#if defined(MBEDTLS_USE_PSA_CRYPTO)
6071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6073 mbedtls_svc_key_id_t key,
6074 psa_algorithm_t alg,
6075 const unsigned char *raw_psk, size_t raw_psk_length,
6076 const unsigned char *seed, size_t seed_length,
6077 const unsigned char *label, size_t label_length,
6078 const unsigned char *other_secret,
6079 size_t other_secret_length,
6080 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006081{
6082 psa_status_t status;
6083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 status = psa_key_derivation_setup(derivation, alg);
6085 if (status != PSA_SUCCESS) {
6086 return status;
6087 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6090 status = psa_key_derivation_input_bytes(derivation,
6091 PSA_KEY_DERIVATION_INPUT_SEED,
6092 seed, seed_length);
6093 if (status != PSA_SUCCESS) {
6094 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006095 }
6096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if (other_secret != NULL) {
6098 status = psa_key_derivation_input_bytes(derivation,
6099 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6100 other_secret, other_secret_length);
6101 if (status != PSA_SUCCESS) {
6102 return status;
6103 }
6104 }
6105
6106 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006107 status = psa_key_derivation_input_bytes(
6108 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 raw_psk, raw_psk_length);
6110 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006111 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 if (status != PSA_SUCCESS) {
6115 return status;
6116 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006117
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 status = psa_key_derivation_input_bytes(derivation,
6119 PSA_KEY_DERIVATION_INPUT_LABEL,
6120 label, label_length);
6121 if (status != PSA_SUCCESS) {
6122 return status;
6123 }
6124 } else {
6125 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006126 }
6127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 status = psa_key_derivation_set_capacity(derivation, capacity);
6129 if (status != PSA_SUCCESS) {
6130 return status;
6131 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006134}
6135
Andrzej Kurek57d10632022-10-24 10:32:01 -04006136#if defined(PSA_WANT_ALG_SHA_384) || \
6137 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006139static int tls_prf_generic(mbedtls_md_type_t md_type,
6140 const unsigned char *secret, size_t slen,
6141 const char *label,
6142 const unsigned char *random, size_t rlen,
6143 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006144{
6145 psa_status_t status;
6146 psa_algorithm_t alg;
6147 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6148 psa_key_derivation_operation_t derivation =
6149 PSA_KEY_DERIVATION_OPERATION_INIT;
6150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006152 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006154 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006156
6157 /* Normally a "secret" should be long enough to be impossible to
6158 * find by brute force, and in particular should not be empty. But
6159 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6160 * and for this use case it makes sense to have a 0-length "secret".
6161 * Since the key API doesn't allow importing a key of length 0,
6162 * keep master_key=0, which setup_psa_key_derivation() understands
6163 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006165 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6167 psa_set_key_algorithm(&key_attributes, alg);
6168 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6171 if (status != PSA_SUCCESS) {
6172 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6173 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006174 }
6175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 status = setup_psa_key_derivation(&derivation,
6177 master_key, alg,
6178 NULL, 0,
6179 random, rlen,
6180 (unsigned char const *) label,
6181 (size_t) strlen(label),
6182 NULL, 0,
6183 dlen);
6184 if (status != PSA_SUCCESS) {
6185 psa_key_derivation_abort(&derivation);
6186 psa_destroy_key(master_key);
6187 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006188 }
6189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6191 if (status != PSA_SUCCESS) {
6192 psa_key_derivation_abort(&derivation);
6193 psa_destroy_key(master_key);
6194 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006195 }
6196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 status = psa_key_derivation_abort(&derivation);
6198 if (status != PSA_SUCCESS) {
6199 psa_destroy_key(master_key);
6200 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006201 }
6202
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 if (!mbedtls_svc_key_id_is_null(master_key)) {
6204 status = psa_destroy_key(master_key);
6205 }
6206 if (status != PSA_SUCCESS) {
6207 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6208 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006211}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006212#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006213#else /* MBEDTLS_USE_PSA_CRYPTO */
6214
Andrzej Kurek57d10632022-10-24 10:32:01 -04006215#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006216 (defined(MBEDTLS_MD_CAN_SHA256) || \
6217 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006219static int tls_prf_generic(mbedtls_md_type_t md_type,
6220 const unsigned char *secret, size_t slen,
6221 const char *label,
6222 const unsigned char *random, size_t rlen,
6223 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006224{
6225 size_t nb;
6226 size_t i, j, k, md_len;
6227 unsigned char *tmp;
6228 size_t tmp_len = 0;
6229 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6230 const mbedtls_md_info_t *md_info;
6231 mbedtls_md_context_t md_ctx;
6232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6233
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006235
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6237 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6238 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 tmp_len = md_len + strlen(label) + rlen;
6243 tmp = mbedtls_calloc(1, tmp_len);
6244 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006245 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6246 goto exit;
6247 }
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 nb = strlen(label);
6250 memcpy(tmp + md_len, label, nb);
6251 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006252 nb += rlen;
6253
6254 /*
6255 * Compute P_<hash>(secret, label + random)[0..dlen]
6256 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006258 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006260
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6262 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006263 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006264 }
6265 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6266 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006267 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 }
6269 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6270 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006271 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 for (i = 0; i < dlen; i += md_len) {
6275 ret = mbedtls_md_hmac_reset(&md_ctx);
6276 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006277 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 }
6279 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6280 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 }
6283 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6284 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006285 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006287
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 ret = mbedtls_md_hmac_reset(&md_ctx);
6289 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006290 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 }
6292 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6293 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006294 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006295 }
6296 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6297 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006298 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006300
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006302
Gilles Peskine449bd832023-01-11 14:50:10 +01006303 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006304 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006306 }
6307
6308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 if (tmp != NULL) {
6312 mbedtls_platform_zeroize(tmp, tmp_len);
6313 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006314
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006316
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006320}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006321#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006322#endif /* MBEDTLS_USE_PSA_CRYPTO */
6323
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006324#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006325MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006326static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6327 const char *label,
6328 const unsigned char *random, size_t rlen,
6329 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006330{
Gilles Peskine449bd832023-01-11 14:50:10 +01006331 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6332 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006333}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006334#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006335
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006336#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006337MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006338static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6339 const char *label,
6340 const unsigned char *random, size_t rlen,
6341 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006342{
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6344 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006345}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006346#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006347
Jerry Yuf009d862022-02-17 14:01:37 +08006348/*
6349 * Set appropriate PRF function and other SSL / TLS1.2 functions
6350 *
6351 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006352 * - hash associated with the ciphersuite (only used by TLS 1.2)
6353 *
6354 * Outputs:
6355 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6356 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006358static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6359 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006360{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006361#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006363 handshake->tls_prf = tls_prf_sha384;
6364 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6365 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006367#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006368#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006369 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006370 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006371 handshake->tls_prf = tls_prf_sha256;
6372 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6373 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6374 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006375#else
Jerry Yuf009d862022-02-17 14:01:37 +08006376 {
Jerry Yuf009d862022-02-17 14:01:37 +08006377 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006378 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006380 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006381#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006382
Gilles Peskine449bd832023-01-11 14:50:10 +01006383 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006384}
Jerry Yud6ab2352022-02-17 14:03:43 +08006385
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006386/*
6387 * Compute master secret if needed
6388 *
6389 * Parameters:
6390 * [in/out] handshake
6391 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6392 * (PSA-PSK) ciphersuite_info, psk_opaque
6393 * [out] premaster (cleared)
6394 * [out] master
6395 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6396 * debug: conf->f_dbg, conf->p_dbg
6397 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006398 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006399 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006400MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006401static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6402 unsigned char *master,
6403 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006404{
6405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6406
6407 /* cf. RFC 5246, Section 8.1:
6408 * "The master secret is always exactly 48 bytes in length." */
6409 size_t const master_secret_len = 48;
6410
6411#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6412 unsigned char session_hash[48];
6413#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6414
6415 /* The label for the KDF used for key expansion.
6416 * This is either "master secret" or "extended master secret"
6417 * depending on whether the Extended Master Secret extension
6418 * is used. */
6419 char const *lbl = "master secret";
6420
Przemek Stekielae4ed302022-04-05 17:15:55 +02006421 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006422 * - If the Extended Master Secret extension is not used,
6423 * this is ClientHello.Random + ServerHello.Random
6424 * (see Sect. 8.1 in RFC 5246).
6425 * - If the Extended Master Secret extension is used,
6426 * this is the transcript of the handshake so far.
6427 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006428 unsigned char const *seed = handshake->randbytes;
6429 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006430
6431#if !defined(MBEDTLS_DEBUG_C) && \
6432 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6433 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006435 ssl = NULL; /* make sure we don't use it except for those cases */
6436 (void) ssl;
6437#endif
6438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 if (handshake->resume != 0) {
6440 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6441 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006442 }
6443
6444#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006446 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006447 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006448 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6449 if (ret != 0) {
6450 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6451 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006452
Gilles Peskine449bd832023-01-11 14:50:10 +01006453 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6454 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006455 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006456#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006457
Przemek Stekiel99114f32022-04-22 11:20:09 +02006458#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006459 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006461 /* Perform PSK-to-MS expansion in a single step. */
6462 psa_status_t status;
6463 psa_algorithm_t alg;
6464 mbedtls_svc_key_id_t psk;
6465 psa_key_derivation_operation_t derivation =
6466 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01006467 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006470
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006472
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006474 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006476 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006478
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006479 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006481
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006483 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006484 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006485 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006486 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006487 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006488 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006489 other_secret_len = 48;
6490 other_secret = handshake->premaster + 2;
6491 break;
6492 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006493 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006494 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6495 other_secret = handshake->premaster + 2;
6496 break;
6497 default:
6498 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006499 }
6500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 status = setup_psa_key_derivation(&derivation, psk, alg,
6502 ssl->conf->psk, ssl->conf->psk_len,
6503 seed, seed_len,
6504 (unsigned char const *) lbl,
6505 (size_t) strlen(lbl),
6506 other_secret, other_secret_len,
6507 master_secret_len);
6508 if (status != PSA_SUCCESS) {
6509 psa_key_derivation_abort(&derivation);
6510 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006511 }
6512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 status = psa_key_derivation_output_bytes(&derivation,
6514 master,
6515 master_secret_len);
6516 if (status != PSA_SUCCESS) {
6517 psa_key_derivation_abort(&derivation);
6518 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006519 }
6520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 status = psa_key_derivation_abort(&derivation);
6522 if (status != PSA_SUCCESS) {
6523 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6524 }
6525 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006526#endif
6527 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006528#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6530 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006531 psa_status_t status;
6532 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6533 psa_key_derivation_operation_t derivation =
6534 PSA_KEY_DERIVATION_OPERATION_INIT;
6535
Gilles Peskine449bd832023-01-11 14:50:10 +01006536 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006537
6538 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 status = psa_key_derivation_setup(&derivation, alg);
6541 if (status != PSA_SUCCESS) {
6542 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006543 }
6544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 status = psa_key_derivation_set_capacity(&derivation,
6546 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6547 if (status != PSA_SUCCESS) {
6548 psa_key_derivation_abort(&derivation);
6549 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006550 }
6551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6553 &derivation);
6554 if (status != PSA_SUCCESS) {
6555 psa_key_derivation_abort(&derivation);
6556 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006557 }
6558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 status = psa_key_derivation_output_bytes(&derivation,
6560 handshake->premaster,
6561 handshake->pmslen);
6562 if (status != PSA_SUCCESS) {
6563 psa_key_derivation_abort(&derivation);
6564 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6565 }
6566
6567 status = psa_key_derivation_abort(&derivation);
6568 if (status != PSA_SUCCESS) {
6569 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006570 }
6571 }
6572#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6574 lbl, seed, seed_len,
6575 master,
6576 master_secret_len);
6577 if (ret != 0) {
6578 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6579 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006580 }
6581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6583 handshake->premaster,
6584 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 mbedtls_platform_zeroize(handshake->premaster,
6587 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006588 }
6589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006591}
6592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006594{
6595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6596 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6597 ssl->handshake->ciphersuite_info;
6598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006600
6601 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006603 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 if (ret != 0) {
6605 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6606 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006607 }
6608
6609 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 ret = ssl_compute_master(ssl->handshake,
6611 ssl->session_negotiate->master,
6612 ssl);
6613 if (ret != 0) {
6614 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6615 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006616 }
6617
6618 /* Swap the client and server random values:
6619 * - MS derivation wanted client+server (RFC 5246 8.1)
6620 * - key derivation wants server+client (RFC 5246 6.3) */
6621 {
6622 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 memcpy(tmp, ssl->handshake->randbytes, 64);
6624 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6625 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6626 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006627 }
6628
6629 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6631 ssl->session_negotiate->ciphersuite,
6632 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006633#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006635#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 ssl->handshake->tls_prf,
6637 ssl->handshake->randbytes,
6638 ssl->tls_version,
6639 ssl->conf->endpoint,
6640 ssl);
6641 if (ret != 0) {
6642 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6643 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006644 }
6645
6646 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6648 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006653}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006654
Gilles Peskine449bd832023-01-11 14:50:10 +01006655int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006656{
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006658#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006659 case MBEDTLS_SSL_HASH_SHA384:
6660 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6661 break;
6662#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006663#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006664 case MBEDTLS_SSL_HASH_SHA256:
6665 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6666 break;
6667#endif
6668 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006670 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006671#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6672 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006673 (void) ssl;
6674#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006676}
6677
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006678#if defined(MBEDTLS_USE_PSA_CRYPTO)
6679static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
6680 const psa_hash_operation_t *hs_op,
6681 size_t buffer_size,
6682 unsigned char *hash,
6683 size_t *hlen)
6684{
6685 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006686 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006687
6688#if !defined(MBEDTLS_DEBUG_C)
6689 (void) ssl;
6690#endif
6691 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006692 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006693 if (status != PSA_SUCCESS) {
6694 goto exit;
6695 }
6696
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006697 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006698 if (status != PSA_SUCCESS) {
6699 goto exit;
6700 }
6701
6702 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6703 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
6704
6705exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006706 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006707 return mbedtls_md_error_from_psa(status);
6708}
6709#else
6710static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
6711 const mbedtls_md_context_t *hs_ctx,
6712 unsigned char *hash,
6713 size_t *hlen)
6714{
6715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006716 mbedtls_md_context_t cloned_ctx;
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006717
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006718 mbedtls_md_init(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006719
6720#if !defined(MBEDTLS_DEBUG_C)
6721 (void) ssl;
6722#endif
6723 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
6724
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006725 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006726 if (ret != 0) {
6727 goto exit;
6728 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006729 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006730 if (ret != 0) {
6731 goto exit;
6732 }
6733
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006734 ret = mbedtls_md_finish(&cloned_ctx, hash);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006735 if (ret != 0) {
6736 goto exit;
6737 }
6738
6739 *hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
6740
6741 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6742 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
6743
6744exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006745 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006746 return ret;
6747}
6748#endif /* MBEDTLS_USE_PSA_CRYPTO */
6749
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006750#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006751int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6752 unsigned char *hash,
6753 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006754{
6755#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006756 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
6757 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006758#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006759 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
6760 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006761#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006762}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006763#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006764
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006765#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006766int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6767 unsigned char *hash,
6768 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006769{
6770#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006771 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
6772 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006773#else
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006774 return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
6775 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006776#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006777}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006778#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006779
Neil Armstrong80f6f322022-05-03 17:56:38 +02006780#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6781 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006782int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006783{
6784 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006786 const unsigned char *psk = NULL;
6787 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006791 /*
6792 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006793 * checked before calling this function.
6794 *
6795 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006796 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6799 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006801 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006802 }
6803
6804 /*
6805 * PMS = struct {
6806 * opaque other_secret<0..2^16-1>;
6807 * opaque psk<0..2^16-1>;
6808 * };
6809 * with "other_secret" depending on the particular key exchange
6810 */
6811#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6813 if (end - p < 2) {
6814 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6815 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006816
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006818 p += 2;
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (end < p || (size_t) (end - p) < psk_len) {
6821 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6822 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006825 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006827#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6828#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006830 /*
6831 * other_secret already set by the ClientKeyExchange message,
6832 * and is 48 bytes long
6833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 if (end - p < 2) {
6835 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6836 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006837
6838 *p++ = 0;
6839 *p++ = 48;
6840 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006842#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6843#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6846 size_t len;
6847
6848 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006849 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006850 p + 2, (size_t) (end - (p + 2)), &len,
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6852 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6853 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006856 p += 2 + len;
6857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6859 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006860#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006861#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6864 size_t zlen;
6865
Gilles Peskine449bd832023-01-11 14:50:10 +01006866 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006867 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6869 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6870 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006871 }
6872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006874 p += 2 + zlen;
6875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6877 MBEDTLS_DEBUG_ECDH_Z);
6878 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006879#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006880 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6882 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006883 }
6884
6885 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 if (end - p < 2) {
6887 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6888 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006891 p += 2;
6892
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (end < p || (size_t) (end - p) < psk_len) {
6894 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6895 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006898 p += psk_len;
6899
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006900 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006903}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006904#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006905
6906#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006908static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006909
6910#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006911int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006912{
6913 /* If renegotiation is not enforced, retransmit until we would reach max
6914 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006916 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6917 unsigned char doublings = 1;
6918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006920 ++doublings;
6921 ratio >>= 1;
6922 }
6923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 if (++ssl->renego_records_seen > doublings) {
6925 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6926 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006927 }
6928 }
6929
Gilles Peskine449bd832023-01-11 14:50:10 +01006930 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006931}
6932#endif
6933#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006934
Jerry Yud9526692022-02-17 14:23:47 +08006935/*
6936 * Handshake functions
6937 */
6938#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6939/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006940int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006941{
6942 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6943 ssl->handshake->ciphersuite_info;
6944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6948 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006949 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006951 }
6952
Gilles Peskine449bd832023-01-11 14:50:10 +01006953 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6954 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006955}
6956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006958{
6959 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6960 ssl->handshake->ciphersuite_info;
6961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006966 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006967 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006968 }
6969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6971 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006972}
6973
6974#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6975/* Some certificate support -> implement write and parse */
6976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006978{
6979 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6980 size_t i, n;
6981 const mbedtls_x509_crt *crt;
6982 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6983 ssl->handshake->ciphersuite_info;
6984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006986
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6988 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006989 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006991 }
6992
6993#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006994 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6995 if (ssl->handshake->client_auth == 0) {
6996 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006997 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006998 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006999 }
7000 }
7001#endif /* MBEDTLS_SSL_CLI_C */
7002#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7004 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007005 /* Should never happen because we shouldn't have picked the
7006 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007008 }
7009 }
7010#endif
7011
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08007013
7014 /*
7015 * 0 . 0 handshake type
7016 * 1 . 3 handshake length
7017 * 4 . 6 length of all certs
7018 * 7 . 9 length of cert. 1
7019 * 10 . n-1 peer certificate
7020 * n . n+2 length of cert. 2
7021 * n+3 . ... upper level cert, etc.
7022 */
7023 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007027 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7029 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7030 " > %" MBEDTLS_PRINTF_SIZET,
7031 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7032 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007033 }
7034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7036 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7037 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007040 i += n; crt = crt->next;
7041 }
7042
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7044 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7045 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007046
7047 ssl->out_msglen = i;
7048 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7049 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7050
7051 ssl->state++;
7052
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7055 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007056 }
7057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007061}
7062
7063#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7064
7065#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007067static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7068 unsigned char *crt_buf,
7069 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007070{
7071 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7072
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 if (peer_crt == NULL) {
7074 return -1;
7075 }
Jerry Yud9526692022-02-17 14:23:47 +08007076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 if (peer_crt->raw.len != crt_buf_len) {
7078 return -1;
7079 }
Jerry Yud9526692022-02-17 14:23:47 +08007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007082}
7083#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007084MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007085static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7086 unsigned char *crt_buf,
7087 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007088{
7089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7090 unsigned char const * const peer_cert_digest =
7091 ssl->session->peer_cert_digest;
7092 mbedtls_md_type_t const peer_cert_digest_type =
7093 ssl->session->peer_cert_digest_type;
7094 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007096 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7097 size_t digest_len;
7098
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 if (peer_cert_digest == NULL || digest_info == NULL) {
7100 return -1;
7101 }
Jerry Yud9526692022-02-17 14:23:47 +08007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 digest_len = mbedtls_md_get_size(digest_info);
7104 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7105 return -1;
7106 }
Jerry Yud9526692022-02-17 14:23:47 +08007107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7109 if (ret != 0) {
7110 return -1;
7111 }
Jerry Yud9526692022-02-17 14:23:47 +08007112
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007114}
7115#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7116#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7117
7118/*
7119 * Once the certificate message is read, parse it into a cert chain and
7120 * perform basic checks, but leave actual verification to the caller
7121 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007123static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7124 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007125{
7126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7127#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007129#endif
7130 size_t i, n;
7131 uint8_t alert;
7132
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7134 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7135 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7136 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7137 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7141 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7142 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7143 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007144 }
7145
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7147 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7148 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7149 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7150 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007151 }
7152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007154
7155 /*
7156 * Same message structure as in mbedtls_ssl_write_certificate()
7157 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007158 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007159
Gilles Peskine449bd832023-01-11 14:50:10 +01007160 if (ssl->in_msg[i] != 0 ||
7161 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7162 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7163 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7164 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7165 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007166 }
7167
7168 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7169 i += 3;
7170
7171 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007173 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 if (i + 3 > ssl->in_hslen) {
7175 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7176 mbedtls_ssl_send_alert_message(ssl,
7177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7178 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7179 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007180 }
7181 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7182 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 if (ssl->in_msg[i] != 0) {
7184 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7185 mbedtls_ssl_send_alert_message(ssl,
7186 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7187 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7188 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007189 }
7190
7191 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007192 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007193 i += 3;
7194
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 if (n < 128 || i + n > ssl->in_hslen) {
7196 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7197 mbedtls_ssl_send_alert_message(ssl,
7198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7199 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7200 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007201 }
7202
7203 /* Check if we're handling the first CRT in the chain. */
7204#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007206 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007208 /* During client-side renegotiation, check that the server's
7209 * end-CRTs hasn't changed compared to the initial handshake,
7210 * mitigating the triple handshake attack. On success, reuse
7211 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7213 if (ssl_check_peer_crt_unchanged(ssl,
7214 &ssl->in_msg[i],
7215 n) != 0) {
7216 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7217 mbedtls_ssl_send_alert_message(ssl,
7218 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7219 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7220 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007221 }
7222
7223 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007225 }
7226#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7227
7228 /* Parse the next certificate in the chain. */
7229#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007231#else
7232 /* If we don't need to store the CRT chain permanently, parse
7233 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007234 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007235#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007237 case 0: /*ok*/
7238 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7239 /* Ignore certificate with an unknown algorithm: maybe a
7240 prior certificate was already trusted. */
7241 break;
7242
7243 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7244 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7245 goto crt_parse_der_failed;
7246
7247 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7248 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7249 goto crt_parse_der_failed;
7250
7251 default:
7252 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007253crt_parse_der_failed:
7254 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7255 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7256 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007257 }
7258
7259 i += n;
7260 }
7261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7263 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007264}
7265
7266#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007267MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007268static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007269{
Gilles Peskine449bd832023-01-11 14:50:10 +01007270 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7271 return -1;
7272 }
Jerry Yud9526692022-02-17 14:23:47 +08007273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007275 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7276 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7278 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7279 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007280 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007282}
7283#endif /* MBEDTLS_SSL_SRV_C */
7284
7285/* Check if a certificate message is expected.
7286 * Return either
7287 * - SSL_CERTIFICATE_EXPECTED, or
7288 * - SSL_CERTIFICATE_SKIP
7289 * indicating whether a Certificate message is expected or not.
7290 */
7291#define SSL_CERTIFICATE_EXPECTED 0
7292#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007294static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7295 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007296{
7297 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7298 ssl->handshake->ciphersuite_info;
7299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7301 return SSL_CERTIFICATE_SKIP;
7302 }
Jerry Yud9526692022-02-17 14:23:47 +08007303
7304#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7306 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7307 return SSL_CERTIFICATE_SKIP;
7308 }
Jerry Yud9526692022-02-17 14:23:47 +08007309
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007311 ssl->session_negotiate->verify_result =
7312 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007314 }
7315 }
7316#else
7317 ((void) authmode);
7318#endif /* MBEDTLS_SSL_SRV_C */
7319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007321}
7322
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007323MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007324static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7325 int authmode,
7326 mbedtls_x509_crt *chain,
7327 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007328{
7329 int ret = 0;
7330 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7331 ssl->handshake->ciphersuite_info;
7332 int have_ca_chain = 0;
7333
7334 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7335 void *p_vrfy;
7336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7338 return 0;
7339 }
Jerry Yud9526692022-02-17 14:23:47 +08007340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 if (ssl->f_vrfy != NULL) {
7342 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007343 f_vrfy = ssl->f_vrfy;
7344 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 } else {
7346 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007347 f_vrfy = ssl->conf->f_vrfy;
7348 p_vrfy = ssl->conf->p_vrfy;
7349 }
7350
7351 /*
7352 * Main check: verify certificate
7353 */
7354#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007356 ((void) rs_ctx);
7357 have_ca_chain = 1;
7358
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007360 ret = mbedtls_x509_crt_verify_with_ca_cb(
7361 chain,
7362 ssl->conf->f_ca_cb,
7363 ssl->conf->p_ca_cb,
7364 ssl->conf->cert_profile,
7365 ssl->hostname,
7366 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 f_vrfy, p_vrfy);
7368 } else
Jerry Yud9526692022-02-17 14:23:47 +08007369#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7370 {
7371 mbedtls_x509_crt *ca_chain;
7372 mbedtls_x509_crl *ca_crl;
7373
7374#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007376 ca_chain = ssl->handshake->sni_ca_chain;
7377 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 } else
Jerry Yud9526692022-02-17 14:23:47 +08007379#endif
7380 {
7381 ca_chain = ssl->conf->ca_chain;
7382 ca_crl = ssl->conf->ca_crl;
7383 }
7384
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007386 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 }
Jerry Yud9526692022-02-17 14:23:47 +08007388
7389 ret = mbedtls_x509_crt_verify_restartable(
7390 chain,
7391 ca_chain, ca_crl,
7392 ssl->conf->cert_profile,
7393 ssl->hostname,
7394 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007396 }
7397
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 if (ret != 0) {
7399 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007400 }
7401
7402#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7404 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7405 }
Jerry Yud9526692022-02-17 14:23:47 +08007406#endif
7407
7408 /*
7409 * Secondary checks: always done, but change 'ret' only if it was 0
7410 */
7411
Valerio Settiacd32c02023-06-29 18:06:29 +02007412#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Jerry Yud9526692022-02-17 14:23:47 +08007413 {
7414 const mbedtls_pk_context *pk = &chain->pk;
7415
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007416 /* If certificate uses an EC key, make sure the curve is OK.
7417 * This is a public key, so it can't be opaque, so can_do() is a good
7418 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007420 /* and in the unlikely case the above assumption no longer holds
7421 * we are making sure that pk_ec() here does not return a NULL
7422 */
Valerio Settif9362b72023-11-29 08:42:27 +01007423 mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(pk);
Valerio Settid0405092023-05-24 13:16:40 +02007424 if (grp_id == MBEDTLS_ECP_DP_NONE) {
7425 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid group ID"));
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007427 }
Valerio Setti97207782023-05-18 18:59:06 +02007428 if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007429 ssl->session_negotiate->verify_result |=
7430 MBEDTLS_X509_BADCERT_BAD_KEY;
7431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7433 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007434 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007436 }
Jerry Yud9526692022-02-17 14:23:47 +08007437 }
7438 }
Valerio Settiacd32c02023-06-29 18:06:29 +02007439#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Jerry Yud9526692022-02-17 14:23:47 +08007440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (mbedtls_ssl_check_cert_usage(chain,
7442 ciphersuite_info,
7443 !ssl->conf->endpoint,
7444 &ssl->session_negotiate->verify_result) != 0) {
7445 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7446 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007447 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 }
Jerry Yud9526692022-02-17 14:23:47 +08007449 }
7450
7451 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7452 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7453 * with details encoded in the verification flags. All other kinds
7454 * of error codes, including those from the user provided f_vrfy
7455 * functions, are treated as fatal and lead to a failure of
7456 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7458 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7459 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007460 ret = 0;
7461 }
7462
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7464 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007465 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7466 }
7467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007469 uint8_t alert;
7470
7471 /* The certificate may have been rejected for several reasons.
7472 Pick one and send the corresponding alert. Which alert to send
7473 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007475 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007477 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007479 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007481 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007483 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007485 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007487 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007488 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007489 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007491 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007493 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007495 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 }
7497 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7498 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007499 }
7500
7501#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (ssl->session_negotiate->verify_result != 0) {
7503 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7504 (unsigned int) ssl->session_negotiate->verify_result));
7505 } else {
7506 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007507 }
7508#endif /* MBEDTLS_DEBUG_C */
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007511}
7512
7513#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007515static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7516 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007517{
7518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7519 /* Remember digest of the peer's end-CRT. */
7520 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7522 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7523 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7524 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7525 mbedtls_ssl_send_alert_message(ssl,
7526 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7527 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007528
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007530 }
7531
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 ret = mbedtls_md(mbedtls_md_info_from_type(
7533 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7534 start, len,
7535 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007536
7537 ssl->session_negotiate->peer_cert_digest_type =
7538 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7539 ssl->session_negotiate->peer_cert_digest_len =
7540 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007543}
7544
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007545MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007546static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7547 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007548{
7549 unsigned char *end = start + len;
7550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7551
7552 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7554 ret = mbedtls_pk_parse_subpubkey(&start, end,
7555 &ssl->handshake->peer_pubkey);
7556 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007557 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007559 }
7560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007562}
7563#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007566{
7567 int ret = 0;
7568 int crt_expected;
7569#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7570 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7571 ? ssl->handshake->sni_authmode
7572 : ssl->conf->authmode;
7573#else
7574 const int authmode = ssl->conf->authmode;
7575#endif
7576 void *rs_ctx = NULL;
7577 mbedtls_x509_crt *chain = NULL;
7578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7582 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7583 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007584 goto exit;
7585 }
7586
7587#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 if (ssl->handshake->ecrs_enabled &&
7589 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007590 chain = ssl->handshake->ecrs_peer_cert;
7591 ssl->handshake->ecrs_peer_cert = NULL;
7592 goto crt_verify;
7593 }
7594#endif
7595
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007597 /* mbedtls_ssl_read_record may have sent an alert already. We
7598 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007600 goto exit;
7601 }
7602
7603#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007605 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007608 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 }
Jerry Yud9526692022-02-17 14:23:47 +08007610
7611 goto exit;
7612 }
7613#endif /* MBEDTLS_SSL_SRV_C */
7614
7615 /* Clear existing peer CRT structure in case we tried to
7616 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7620 if (chain == NULL) {
7621 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7622 sizeof(mbedtls_x509_crt)));
7623 mbedtls_ssl_send_alert_message(ssl,
7624 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7625 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007626
7627 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7628 goto exit;
7629 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007631
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 ret = ssl_parse_certificate_chain(ssl, chain);
7633 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007634 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 }
Jerry Yud9526692022-02-17 14:23:47 +08007636
7637#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007639 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 }
Jerry Yud9526692022-02-17 14:23:47 +08007641
7642crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007644 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 }
Jerry Yud9526692022-02-17 14:23:47 +08007646#endif
7647
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 ret = ssl_parse_certificate_verify(ssl, authmode,
7649 chain, rs_ctx);
7650 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007651 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 }
Jerry Yud9526692022-02-17 14:23:47 +08007653
7654#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7655 {
7656 unsigned char *crt_start, *pk_start;
7657 size_t crt_len, pk_len;
7658
7659 /* We parse the CRT chain without copying, so
7660 * these pointers point into the input buffer,
7661 * and are hence still valid after freeing the
7662 * CRT chain. */
7663
7664 crt_start = chain->raw.p;
7665 crt_len = chain->raw.len;
7666
7667 pk_start = chain->pk_raw.p;
7668 pk_len = chain->pk_raw.len;
7669
7670 /* Free the CRT structures before computing
7671 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 mbedtls_x509_crt_free(chain);
7673 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007674 chain = NULL;
7675
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7677 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007678 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 }
Jerry Yud9526692022-02-17 14:23:47 +08007680
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7682 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007683 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 }
Jerry Yud9526692022-02-17 14:23:47 +08007685 }
7686#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7687 /* Pass ownership to session structure. */
7688 ssl->session_negotiate->peer_cert = chain;
7689 chain = NULL;
7690#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007693
7694exit:
7695
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007697 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 }
Jerry Yud9526692022-02-17 14:23:47 +08007699
7700#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007702 ssl->handshake->ecrs_peer_cert = chain;
7703 chain = NULL;
7704 }
7705#endif
7706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 if (chain != NULL) {
7708 mbedtls_x509_crt_free(chain);
7709 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007710 }
7711
Gilles Peskine449bd832023-01-11 14:50:10 +01007712 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007713}
7714#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7715
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007716static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
7717 unsigned char *padbuf, size_t hlen,
7718 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007719{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00007720 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007721 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007722#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007723 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007724 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007725 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007726 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007727#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007729 mbedtls_md_context_t *hs_ctx = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007730 mbedtls_md_context_t cloned_ctx;
7731 mbedtls_md_init(&cloned_ctx);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007732#endif
7733
7734 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007736 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007738
Gilles Peskine449bd832023-01-11 14:50:10 +01007739 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007740 ? "client finished"
7741 : "server finished";
7742
7743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007744 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007745
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007746 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007748 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007749 }
7750
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007751 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007753 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007754 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007755 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007756#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007757 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007758
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007759 ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007760 if (ret != 0) {
7761 goto exit;
7762 }
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007763 ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007764 if (ret != 0) {
7765 goto exit;
7766 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007767
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007768 ret = mbedtls_md_finish(&cloned_ctx, padbuf);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007769 if (ret != 0) {
7770 goto exit;
7771 }
7772#endif /* MBEDTLS_USE_PSA_CRYPTO */
7773
7774 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
7775
Jerry Yu615bd6f2022-02-17 14:25:15 +08007776 /*
7777 * TLSv1.2:
7778 * hash = PRF( master, finished_label,
7779 * Hash( handshake ) )[0.11]
7780 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007781 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007782 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007785
Paul Elliottecb95be2023-08-11 16:41:04 +01007786 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007787
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007788 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007789
7790exit:
7791#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007792 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02007793 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007794#else
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007795 mbedtls_md_free(&cloned_ctx);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007796 return ret;
7797#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007798}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007799
7800#if defined(MBEDTLS_MD_CAN_SHA256)
7801static int ssl_calc_finished_tls_sha256(
7802 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
7803{
7804 unsigned char padbuf[32];
7805 return ssl_calc_finished_tls_generic(ssl,
7806#if defined(MBEDTLS_USE_PSA_CRYPTO)
7807 &ssl->handshake->fin_sha256_psa,
7808#else
7809 &ssl->handshake->fin_sha256,
7810#endif
7811 padbuf, sizeof(padbuf),
7812 buf, from);
7813}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007814#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007815
Jerry Yub7ba49e2022-02-17 14:25:53 +08007816
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007817#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007818static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007820{
Jerry Yub7ba49e2022-02-17 14:25:53 +08007821 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007822 return ssl_calc_finished_tls_generic(ssl,
Jerry Yub7ba49e2022-02-17 14:25:53 +08007823#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007824 &ssl->handshake->fin_sha384_psa,
Jerry Yub7ba49e2022-02-17 14:25:53 +08007825#else
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007826 &ssl->handshake->fin_sha384,
Jerry Yub7ba49e2022-02-17 14:25:53 +08007827#endif
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007828 padbuf, sizeof(padbuf),
7829 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007830}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007831#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007832
Gilles Peskine449bd832023-01-11 14:50:10 +01007833void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007834{
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007836
7837 /*
7838 * Free our handshake params
7839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 mbedtls_ssl_handshake_free(ssl);
7841 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007842 ssl->handshake = NULL;
7843
7844 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007845 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 if (ssl->transform) {
7848 mbedtls_ssl_transform_free(ssl->transform);
7849 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007850 }
7851 ssl->transform = ssl->transform_negotiate;
7852 ssl->transform_negotiate = NULL;
7853
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007855}
7856
Gilles Peskine449bd832023-01-11 14:50:10 +01007857void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007858{
7859 int resume = ssl->handshake->resume;
7860
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007862
7863#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007865 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7866 ssl->renego_records_seen = 0;
7867 }
7868#endif
7869
7870 /*
7871 * Free the previous session and switch in the current one
7872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007874#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7875 /* RFC 7366 3.1: keep the EtM state */
7876 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007878#endif
7879
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 mbedtls_ssl_session_free(ssl->session);
7881 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007882 }
7883 ssl->session = ssl->session_negotiate;
7884 ssl->session_negotiate = NULL;
7885
7886 /*
7887 * Add cache entry
7888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007890 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 resume == 0) {
7892 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7893 ssl->session->id,
7894 ssl->session->id_len,
7895 ssl->session) != 0) {
7896 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7897 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007898 }
7899
7900#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7902 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007903 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007905
7906 /* Keep last flight around in case we need to resend it:
7907 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007908 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7909 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007910#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007912
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007913 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007916}
7917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007919{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00007920 int ret;
7921 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007922
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007924
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007926
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007927 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7928 if (ret != 0) {
7929 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7930 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007931
7932 /*
7933 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7934 * may define some other value. Currently (early 2016), no defined
7935 * ciphersuite does this (and this is unlikely to change as activity has
7936 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7937 */
7938 hash_len = 12;
7939
7940#if defined(MBEDTLS_SSL_RENEGOTIATION)
7941 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007943#endif
7944
7945 ssl->out_msglen = 4 + hash_len;
7946 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7947 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7948
7949 /*
7950 * In case of session resuming, invert the client and server
7951 * ChangeCipherSpec messages order.
7952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007954#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007956 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007957 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007958#endif
7959#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007961 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007963#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007965 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007967
7968 /*
7969 * Switch to our negotiated transform and session parameters for outbound
7970 * data.
7971 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007973
7974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007976 unsigned char i;
7977
7978 /* Remember current epoch settings for resending */
7979 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7981 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007982
7983 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007984 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007985
7986
7987 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 for (i = 2; i > 0; i--) {
7989 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007990 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 }
7992 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007993
7994 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 if (i == 0) {
7996 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7997 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007998 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008000#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008002
8003 ssl->transform_out = ssl->transform_negotiate;
8004 ssl->session_out = ssl->session_negotiate;
8005
8006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8008 mbedtls_ssl_send_flight_completed(ssl);
8009 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008010#endif
8011
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8013 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8014 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008015 }
8016
8017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8019 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8020 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8021 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008022 }
8023#endif
8024
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008026
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008028}
8029
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008030#define SSL_MAX_HASH_LEN 12
8031
Gilles Peskine449bd832023-01-11 14:50:10 +01008032int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008033{
8034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8035 unsigned int hash_len = 12;
8036 unsigned char buf[SSL_MAX_HASH_LEN];
8037
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008039
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008040 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8041 if (ret != 0) {
8042 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8043 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8046 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008047 goto exit;
8048 }
8049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8051 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8052 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8053 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008054 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8055 goto exit;
8056 }
8057
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8059 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8060 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008061 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8062 goto exit;
8063 }
8064
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8066 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8067 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8068 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008069 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8070 goto exit;
8071 }
8072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8074 buf, hash_len) != 0) {
8075 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8076 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8077 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008078 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8079 goto exit;
8080 }
8081
8082#if defined(MBEDTLS_SSL_RENEGOTIATION)
8083 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008084 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008085#endif
8086
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008088#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008089 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008090 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008092#endif
8093#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008094 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008095 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008097#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008099 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008100 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008101
8102#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8104 mbedtls_ssl_recv_flight_completed(ssl);
8105 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008106#endif
8107
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008109
8110exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 mbedtls_platform_zeroize(buf, hash_len);
8112 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008113}
8114
Jerry Yu392112c2022-02-17 14:34:10 +08008115#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8116/*
8117 * Helper to get TLS 1.2 PRF from ciphersuite
8118 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008120static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008121{
Jerry Yu392112c2022-02-17 14:34:10 +08008122 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008124#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8126 return tls_prf_sha384;
8127 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008128#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008129#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008130 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8132 return tls_prf_sha256;
8133 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008134 }
8135#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008136#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8137 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008138 (void) ciphersuite_info;
8139#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008140
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008142}
8143#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008144
Gilles Peskine449bd832023-01-11 14:50:10 +01008145static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008146{
8147 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008148#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 if (tls_prf == tls_prf_sha384) {
8150 return MBEDTLS_SSL_TLS_PRF_SHA384;
8151 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008152#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008153#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 if (tls_prf == tls_prf_sha256) {
8155 return MBEDTLS_SSL_TLS_PRF_SHA256;
8156 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008157#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008159}
8160
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161/*
8162 * Populate a transform structure with session keys and all the other
8163 * necessary information.
8164 *
8165 * Parameters:
8166 * - [in/out]: transform: structure to populate
8167 * [in] must be just initialised with mbedtls_ssl_transform_init()
8168 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8169 * - [in] ciphersuite
8170 * - [in] master
8171 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008172 * - [in] tls_prf: pointer to PRF to use for key derivation
8173 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008174 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008175 * - [in] endpoint: client or server
8176 * - [in] ssl: used for:
8177 * - ssl->conf->{f,p}_export_keys
8178 * [in] optionally used for:
8179 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8180 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008182static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8183 int ciphersuite,
8184 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008185#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008187#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 ssl_tls_prf_t tls_prf,
8189 const unsigned char randbytes[64],
8190 mbedtls_ssl_protocol_version tls_version,
8191 unsigned endpoint,
8192 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008193{
8194 int ret = 0;
8195 unsigned char keyblk[256];
8196 unsigned char *key1;
8197 unsigned char *key2;
8198 unsigned char *mac_enc;
8199 unsigned char *mac_dec;
8200 size_t mac_key_len = 0;
8201 size_t iv_copy_len;
8202 size_t keylen;
8203 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008204 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008205#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008206 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008207 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008208#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209
8210#if defined(MBEDTLS_USE_PSA_CRYPTO)
8211 psa_key_type_t key_type;
8212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8213 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008214 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008215 size_t key_bits;
8216 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8217#endif
8218
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008219 /*
8220 * Some data just needs copying into the structure
8221 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008222#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008223 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008224#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008225 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226
8227#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008229#endif
8230
8231#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8234 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008236 }
8237#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8238
8239 /*
8240 * Get various info structures
8241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8243 if (ciphersuite_info == NULL) {
8244 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8245 ciphersuite));
8246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008247 }
8248
Neil Armstrongab555e02022-04-04 11:07:59 +02008249 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008250#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008251 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008252#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008254
Gilles Peskine449bd832023-01-11 14:50:10 +01008255 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008256 transform->taglen =
8257 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008259
8260#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008261 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 transform->taglen,
8263 &alg,
8264 &key_type,
8265 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008266 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008268 goto end;
8269 }
8270#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008271 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
Gilles Peskine449bd832023-01-11 14:50:10 +01008272 if (cipher_info == NULL) {
8273 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8274 ciphersuite_info->cipher));
8275 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008277#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278
Neil Armstronge4512952022-03-08 09:08:22 +01008279#if defined(MBEDTLS_USE_PSA_CRYPTO)
Dave Rodgman2eab4622023-10-05 13:30:37 +01008280 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008282 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 (unsigned) ciphersuite_info->mac));
8284 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008285 }
8286#else
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01008287 md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 if (md_info == NULL) {
8289 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8290 (unsigned) ciphersuite_info->mac));
8291 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008292 }
Neil Armstronge4512952022-03-08 09:08:22 +01008293#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008294
8295#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8296 /* Copy own and peer's CID if the use of the CID
8297 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8299 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300
8301 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8303 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8304 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305
8306 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8308 ssl->handshake->peer_cid_len);
8309 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8310 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311 }
8312#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8313
8314 /*
8315 * Compute key block using the PRF
8316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8318 if (ret != 0) {
8319 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8320 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008321 }
8322
Gilles Peskine449bd832023-01-11 14:50:10 +01008323 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8324 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8325 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8326 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8327 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008328
8329 /*
8330 * Determine the appropriate key, IV and MAC length.
8331 */
8332
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008333#if defined(MBEDTLS_USE_PSA_CRYPTO)
8334 keylen = PSA_BITS_TO_BYTES(key_bits);
8335#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008337#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008338
Valerio Settie5707042023-10-11 11:54:42 +02008339#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008341 size_t explicit_ivlen;
8342
8343 transform->maclen = 0;
8344 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008345
8346 /* All modes haves 96-bit IVs, but the length of the static parts vary
8347 * with mode and version:
8348 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8349 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8350 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8351 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8352 * sequence number).
8353 */
8354 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008355
8356 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008359#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8361 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008362#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008363
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008365 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008367 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369
8370 /* Minimum length of encrypted record */
8371 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8372 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 } else
Valerio Settie5707042023-10-11 11:54:42 +02008374#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008375#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008377 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008379#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008381#else
Dave Rodgman85a88132023-06-24 11:41:50 +01008382 size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008383#endif /* MBEDTLS_USE_PSA_CRYPTO */
8384
8385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008386 /* Get MAC length */
8387 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8388#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008389 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8391 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8392 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008393 goto end;
8394 }
8395
8396 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008398#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008399 transform->maclen = mac_key_len;
8400
8401 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008402#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008404#else
Dave Rodgmanbb521fd2023-06-24 11:21:25 +01008405 transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008406#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407
8408 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008410 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008412 /*
8413 * GenericBlockCipher:
8414 * 1. if EtM is in use: one block plus MAC
8415 * otherwise: * first multiple of blocklen greater than maclen
8416 * 2. IV
8417 */
8418#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008420 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008421 + block_size;
8422 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008423#endif
8424 {
8425 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 + block_size
8427 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008428 }
8429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008431 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008432 } else {
8433 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008434 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8435 goto end;
8436 }
8437 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008439#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8440 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8442 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443 }
8444
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8446 (unsigned) keylen,
8447 (unsigned) transform->minlen,
8448 (unsigned) transform->ivlen,
8449 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450
8451 /*
8452 * Finally setup the cipher contexts, IVs and MAC secrets.
8453 */
8454#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008456 key1 = keyblk + mac_key_len * 2;
8457 key2 = keyblk + mac_key_len * 2 + keylen;
8458
8459 mac_enc = keyblk;
8460 mac_dec = keyblk + mac_key_len;
8461
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 iv_copy_len = (transform->fixed_ivlen) ?
8463 transform->fixed_ivlen : transform->ivlen;
8464 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8465 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8466 iv_copy_len);
8467 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008468#endif /* MBEDTLS_SSL_CLI_C */
8469#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008471 key1 = keyblk + mac_key_len * 2 + keylen;
8472 key2 = keyblk + mac_key_len * 2;
8473
8474 mac_enc = keyblk + mac_key_len;
8475 mac_dec = keyblk;
8476
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 iv_copy_len = (transform->fixed_ivlen) ?
8478 transform->fixed_ivlen : transform->ivlen;
8479 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8480 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8481 iv_copy_len);
8482 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008483#endif /* MBEDTLS_SSL_SRV_C */
8484 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008486 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8487 goto end;
8488 }
8489
Paul Elliott4fb19552023-10-18 12:15:30 +01008490 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 ssl->f_export_keys(ssl->p_export_keys,
8492 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8493 master, 48,
8494 randbytes + 32,
8495 randbytes,
8496 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008497 }
8498
8499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500 transform->psa_alg = alg;
8501
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8503 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8504 psa_set_key_algorithm(&attributes, alg);
8505 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 if ((status = psa_import_key(&attributes,
8508 key1,
8509 PSA_BITS_TO_BYTES(key_bits),
8510 &transform->psa_key_enc)) != PSA_SUCCESS) {
8511 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008512 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008514 goto end;
8515 }
8516
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 if ((status = psa_import_key(&attributes,
8520 key2,
8521 PSA_BITS_TO_BYTES(key_bits),
8522 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008523 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008524 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008525 goto end;
8526 }
8527 }
8528#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008529 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8530 cipher_info)) != 0) {
8531 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008532 goto end;
8533 }
8534
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8536 cipher_info)) != 0) {
8537 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008538 goto end;
8539 }
8540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8542 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8543 MBEDTLS_ENCRYPT)) != 0) {
8544 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008545 goto end;
8546 }
8547
Gilles Peskine449bd832023-01-11 14:50:10 +01008548 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8549 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8550 MBEDTLS_DECRYPT)) != 0) {
8551 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008552 goto end;
8553 }
8554
8555#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8557 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8558 MBEDTLS_PADDING_NONE)) != 0) {
8559 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008560 goto end;
8561 }
8562
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8564 MBEDTLS_PADDING_NONE)) != 0) {
8565 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008566 goto end;
8567 }
8568 }
8569#endif /* MBEDTLS_CIPHER_MODE_CBC */
8570#endif /* MBEDTLS_USE_PSA_CRYPTO */
8571
Neil Armstrong29c0c042022-03-17 17:47:28 +01008572#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8573 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8574 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008576#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008577 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008578
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8580 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8581 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008582
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 if ((status = psa_import_key(&attributes,
8584 mac_enc, mac_key_len,
8585 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008586 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008588 goto end;
8589 }
8590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8592 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008593#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008595#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008597 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8599 PSA_KEY_USAGE_VERIFY_HASH);
8600 } else {
8601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8602 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if ((status = psa_import_key(&attributes,
8605 mac_dec, mac_key_len,
8606 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008607 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008609 goto end;
8610 }
8611#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8613 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008614 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 }
8616 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8617 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008618 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008620#endif /* MBEDTLS_USE_PSA_CRYPTO */
8621 }
8622#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8623
8624 ((void) mac_dec);
8625 ((void) mac_enc);
8626
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008627end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8629 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008630}
8631
Valerio Settia08b1a42022-11-17 15:10:02 +01008632#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8633 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008634int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 psa_pake_operation_t *pake_ctx,
8636 const unsigned char *buf,
8637 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008638{
8639 psa_status_t status;
8640 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008641 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008642 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8643 * At round two perform a single cycle
8644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008646
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 for (; remaining_steps > 0; remaining_steps--) {
8648 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008649 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008651 /* Length is stored at the first byte */
8652 size_t length = buf[input_offset];
8653 input_offset += 1;
8654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008656 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8657 }
8658
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 status = psa_pake_input(pake_ctx, step,
8660 buf + input_offset, length);
8661 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008662 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008663 }
8664
8665 input_offset += length;
8666 }
8667 }
8668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008670 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008672
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008674}
8675
Valerio Setti6b3dab02022-11-17 17:14:54 +01008676int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 psa_pake_operation_t *pake_ctx,
8678 unsigned char *buf,
8679 size_t len, size_t *olen,
8680 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008681{
8682 psa_status_t status;
8683 size_t output_offset = 0;
8684 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008685 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008686 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8687 * At round two perform a single cycle
8688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008690
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 for (; remaining_steps > 0; remaining_steps--) {
8692 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8693 step <= PSA_PAKE_STEP_ZK_PROOF;
8694 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008695 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008696 * For each step, prepend 1 byte with the length of the data as
8697 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 status = psa_pake_output(pake_ctx, step,
8700 buf + output_offset + 1,
8701 len - output_offset - 1,
8702 &output_len);
8703 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008704 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008705 }
8706
Valerio Setti99d88c12022-11-22 16:03:43 +01008707 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008708
8709 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008710 }
8711 }
8712
8713 *olen = output_offset;
8714
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008716}
Valerio Settia08b1a42022-11-17 15:10:02 +01008717#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8718
Jerry Yuee40f9d2022-02-17 14:55:16 +08008719#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008720int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8721 unsigned char *hash, size_t *hashlen,
8722 unsigned char *data, size_t data_len,
8723 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008724{
8725 psa_status_t status;
8726 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008727 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008728
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008730
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if ((status = psa_hash_setup(&hash_operation,
8732 hash_alg)) != PSA_SUCCESS) {
8733 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008734 goto exit;
8735 }
8736
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8738 64)) != PSA_SUCCESS) {
8739 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008740 goto exit;
8741 }
8742
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 if ((status = psa_hash_update(&hash_operation,
8744 data, data_len)) != PSA_SUCCESS) {
8745 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008746 goto exit;
8747 }
8748
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8750 hashlen)) != PSA_SUCCESS) {
8751 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8752 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008753 }
8754
8755exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008756 if (status != PSA_SUCCESS) {
8757 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8758 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8759 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008760 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008762 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8763 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008765 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008768 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008769 }
8770 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008771 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008772}
8773
8774#else
8775
Gilles Peskine449bd832023-01-11 14:50:10 +01008776int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8777 unsigned char *hash, size_t *hashlen,
8778 unsigned char *data, size_t data_len,
8779 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008780{
8781 int ret = 0;
8782 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8784 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008785
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008787
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789
8790 /*
8791 * digitally-signed struct {
8792 * opaque client_random[32];
8793 * opaque server_random[32];
8794 * ServerDHParams params;
8795 * };
8796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8798 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008799 goto exit;
8800 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8802 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008803 goto exit;
8804 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8806 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008807 goto exit;
8808 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8810 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008811 goto exit;
8812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8814 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008815 goto exit;
8816 }
8817
8818exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 if (ret != 0) {
8822 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8823 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8824 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008827}
8828#endif /* MBEDTLS_USE_PSA_CRYPTO */
8829
Jerry Yud9d91da2022-02-17 14:57:06 +08008830#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8831
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008832/* Find the preferred hash for a given signature algorithm. */
8833unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 mbedtls_ssl_context *ssl,
8835 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008836{
Gabor Mezei078e8032022-04-27 21:17:56 +02008837 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008838 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8841 return MBEDTLS_SSL_HASH_NONE;
8842 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008843
Gilles Peskine449bd832023-01-11 14:50:10 +01008844 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008845 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8847 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008848 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8850 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008851
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02008852 mbedtls_md_type_t md_alg =
8853 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
8854 if (md_alg == MBEDTLS_MD_NONE) {
8855 continue;
8856 }
8857
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008859#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008861 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02008862 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008863
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8865 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8866 PSA_ALG_ECDSA(psa_hash_alg),
8867 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008868 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8872 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8873 PSA_ALG_RSA_PKCS1V15_SIGN(
8874 psa_hash_alg),
8875 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008876 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008878 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008879#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008882 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008883 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008886}
8887
8888#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8889
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008890/* Serialization of TLS 1.2 sessions:
8891 *
8892 * struct {
8893 * uint64 start_time;
8894 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895 * uint8 session_id_len; // at most 32
8896 * opaque session_id[32];
8897 * opaque master[48]; // fixed length in the standard
8898 * uint32 verify_result;
8899 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8900 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8901 * uint32 ticket_lifetime;
8902 * uint8 mfl_code; // up to 255 according to standard
8903 * uint8 encrypt_then_mac; // 0 or 1
8904 * } serialized_session_tls12;
8905 *
8906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008907static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8908 unsigned char *buf,
8909 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008910{
8911 unsigned char *p = buf;
8912 size_t used = 0;
8913
8914#if defined(MBEDTLS_HAVE_TIME)
8915 uint64_t start;
8916#endif
8917#if defined(MBEDTLS_X509_CRT_PARSE_C)
8918#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8919 size_t cert_len;
8920#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8921#endif /* MBEDTLS_X509_CRT_PARSE_C */
8922
8923 /*
8924 * Time
8925 */
8926#if defined(MBEDTLS_HAVE_TIME)
8927 used += 8;
8928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008930 start = (uint64_t) session->start;
8931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008933 p += 8;
8934 }
8935#endif /* MBEDTLS_HAVE_TIME */
8936
8937 /*
8938 * Basic mandatory fields
8939 */
8940 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008941 + 1 /* id_len */
8942 + sizeof(session->id)
8943 + sizeof(session->master)
8944 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 if (used <= buf_len) {
8947 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008948 p += 2;
8949
Gilles Peskine449bd832023-01-11 14:50:10 +01008950 *p++ = MBEDTLS_BYTE_0(session->id_len);
8951 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008952 p += 32;
8953
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955 p += 48;
8956
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958 p += 4;
8959 }
8960
8961 /*
8962 * Peer's end-entity certificate
8963 */
8964#if defined(MBEDTLS_X509_CRT_PARSE_C)
8965#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008969 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008971
8972 used += 3 + cert_len;
8973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if (used <= buf_len) {
8975 *p++ = MBEDTLS_BYTE_2(cert_len);
8976 *p++ = MBEDTLS_BYTE_1(cert_len);
8977 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 if (session->peer_cert != NULL) {
8980 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008981 p += cert_len;
8982 }
8983 }
8984#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008986 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008988 *p++ = (unsigned char) session->peer_cert_digest_type;
8989 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 memcpy(p, session->peer_cert_digest,
8991 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992 p += session->peer_cert_digest_len;
8993 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008995 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008996 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008997 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8998 *p++ = 0;
8999 }
9000 }
9001#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9002#endif /* MBEDTLS_X509_CRT_PARSE_C */
9003
9004 /*
9005 * Session ticket if any, plus associated data
9006 */
9007#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9008 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 if (used <= buf_len) {
9011 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9012 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9013 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if (session->ticket != NULL) {
9016 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009017 p += session->ticket_len;
9018 }
9019
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009021 p += 4;
9022 }
9023#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9024
9025 /*
9026 * Misc extension-related info
9027 */
9028#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9029 used += 1;
9030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009034#endif
9035
9036#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9037 used += 1;
9038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 if (used <= buf_len) {
9040 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9041 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009042#endif
9043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009045}
9046
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009048static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9049 const unsigned char *buf,
9050 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009051{
9052#if defined(MBEDTLS_HAVE_TIME)
9053 uint64_t start;
9054#endif
9055#if defined(MBEDTLS_X509_CRT_PARSE_C)
9056#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9057 size_t cert_len;
9058#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9059#endif /* MBEDTLS_X509_CRT_PARSE_C */
9060
9061 const unsigned char *p = buf;
9062 const unsigned char * const end = buf + len;
9063
9064 /*
9065 * Time
9066 */
9067#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 if (8 > (size_t) (end - p)) {
9069 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9070 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009072 start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009073 p += 8;
9074
9075 session->start = (time_t) start;
9076#endif /* MBEDTLS_HAVE_TIME */
9077
9078 /*
9079 * Basic mandatory fields
9080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9083 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009084
Dave Rodgmana3d0f612023-11-03 23:34:02 +00009085 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086 p += 2;
9087
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009090 p += 32;
9091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009093 p += 48;
9094
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009095 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009096 p += 4;
9097
9098 /* Immediately clear invalid pointer values that have been read, in case
9099 * we exit early before we replaced them with valid ones. */
9100#if defined(MBEDTLS_X509_CRT_PARSE_C)
9101#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9102 session->peer_cert = NULL;
9103#else
9104 session->peer_cert_digest = NULL;
9105#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9106#endif /* MBEDTLS_X509_CRT_PARSE_C */
9107#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9108 session->ticket = NULL;
9109#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9110
9111 /*
9112 * Peer certificate
9113 */
9114#if defined(MBEDTLS_X509_CRT_PARSE_C)
9115#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9116 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 if (3 > (size_t) (end - p)) {
9118 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9119 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009120
Dave Rodgmana3d0f612023-11-03 23:34:02 +00009121 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122 p += 3;
9123
Gilles Peskine449bd832023-01-11 14:50:10 +01009124 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if (cert_len > (size_t) (end - p)) {
9128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9129 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009130
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009132
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 if (session->peer_cert == NULL) {
9134 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9135 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009136
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009138
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9140 p, cert_len)) != 0) {
9141 mbedtls_x509_crt_free(session->peer_cert);
9142 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009143 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145 }
9146
9147 p += cert_len;
9148 }
9149#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9150 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if (2 > (size_t) (end - p)) {
9152 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9153 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009154
9155 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9156 session->peer_cert_digest_len = (size_t) *p++;
9157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9161 if (md_info == NULL) {
9162 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9163 }
9164 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9165 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9166 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009167
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9169 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9170 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009171
9172 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 mbedtls_calloc(1, session->peer_cert_digest_len);
9174 if (session->peer_cert_digest == NULL) {
9175 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9176 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 memcpy(session->peer_cert_digest, p,
9179 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009180 p += session->peer_cert_digest_len;
9181 }
9182#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9183#endif /* MBEDTLS_X509_CRT_PARSE_C */
9184
9185 /*
9186 * Session ticket and associated data
9187 */
9188#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 if (3 > (size_t) (end - p)) {
9190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9191 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009192
Dave Rodgmana3d0f612023-11-03 23:34:02 +00009193 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009194 p += 3;
9195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 if (session->ticket_len != 0) {
9197 if (session->ticket_len > (size_t) (end - p)) {
9198 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9199 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009200
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 session->ticket = mbedtls_calloc(1, session->ticket_len);
9202 if (session->ticket == NULL) {
9203 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9204 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009205
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009207 p += session->ticket_len;
9208 }
9209
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 if (4 > (size_t) (end - p)) {
9211 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9212 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009213
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01009214 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009215 p += 4;
9216#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9217
9218 /*
9219 * Misc extension-related info
9220 */
9221#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 if (1 > (size_t) (end - p)) {
9223 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9224 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009225
9226 session->mfl_code = *p++;
9227#endif
9228
9229#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 if (1 > (size_t) (end - p)) {
9231 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9232 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009233
9234 session->encrypt_then_mac = *p++;
9235#endif
9236
9237 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 if (p != end) {
9239 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9240 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009241
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009243}
Jerry Yudc7bd172022-02-17 13:44:15 +08009244#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9245
XiaokangQian75d40ef2022-04-20 11:05:24 +00009246int mbedtls_ssl_validate_ciphersuite(
9247 const mbedtls_ssl_context *ssl,
9248 const mbedtls_ssl_ciphersuite_t *suite_info,
9249 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009251{
9252 (void) ssl;
9253
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 if (suite_info == NULL) {
9255 return -1;
9256 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009257
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 if ((suite_info->min_tls_version > max_tls_version) ||
9259 (suite_info->max_tls_version < min_tls_version)) {
9260 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009261 }
9262
XiaokangQian060d8672022-04-21 09:24:56 +00009263#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009264#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9267 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009268#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9270 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009271#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009272 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009274 }
9275#endif
9276
9277 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9278#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9280 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9281 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009282 }
9283#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9285
Gilles Peskine449bd832023-01-11 14:50:10 +01009286 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009287}
9288
Ronald Crone68ab4f2022-10-05 12:46:29 +02009289#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009290/*
9291 * Function for writing a signature algorithm extension.
9292 *
9293 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9294 * value (TLS 1.3 RFC8446):
9295 * enum {
9296 * ....
9297 * ecdsa_secp256r1_sha256( 0x0403 ),
9298 * ecdsa_secp384r1_sha384( 0x0503 ),
9299 * ecdsa_secp521r1_sha512( 0x0603 ),
9300 * ....
9301 * } SignatureScheme;
9302 *
9303 * struct {
9304 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9305 * } SignatureSchemeList;
9306 *
9307 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9308 * value (TLS 1.2 RFC5246):
9309 * enum {
9310 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9311 * sha512(6), (255)
9312 * } HashAlgorithm;
9313 *
9314 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9315 * SignatureAlgorithm;
9316 *
9317 * struct {
9318 * HashAlgorithm hash;
9319 * SignatureAlgorithm signature;
9320 * } SignatureAndHashAlgorithm;
9321 *
9322 * SignatureAndHashAlgorithm
9323 * supported_signature_algorithms<2..2^16-2>;
9324 *
9325 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9326 * generalization of the TLS 1.2 signature algorithm extension.
9327 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9328 * `SignatureScheme` field of TLS 1.3
9329 *
9330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009331int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9332 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009333{
9334 unsigned char *p = buf;
9335 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9336 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9337
9338 *out_len = 0;
9339
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009341
9342 /* Check if we have space for header and length field:
9343 * - extension_type (2 bytes)
9344 * - extension_data_length (2 bytes)
9345 * - supported_signature_algorithms_length (2 bytes)
9346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009347 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009348 p += 6;
9349
9350 /*
9351 * Write supported_signature_algorithms
9352 */
9353 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009354 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9355 if (sig_alg == NULL) {
9356 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9357 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009358
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9360 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9361 *sig_alg,
9362 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9363 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009364 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009365 }
9366 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9367 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009368 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009369 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9370 *sig_alg,
9371 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009372 }
9373
9374 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009375 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 if (supported_sig_alg_len == 0) {
9377 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9378 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009379 }
9380
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9382 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9383 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009384
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00009385 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00009386
9387#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009389#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009390
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009392}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009393#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009394
XiaokangQian40a35232022-05-07 09:02:40 +00009395#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009396/*
9397 * mbedtls_ssl_parse_server_name_ext
9398 *
9399 * Structure of server_name extension:
9400 *
9401 * enum {
9402 * host_name(0), (255)
9403 * } NameType;
9404 * opaque HostName<1..2^16-1>;
9405 *
9406 * struct {
9407 * NameType name_type;
9408 * select (name_type) {
9409 * case host_name: HostName;
9410 * } name;
9411 * } ServerName;
9412 * struct {
9413 * ServerName server_name_list<1..2^16-1>
9414 * } ServerNameList;
9415 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009417int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9418 const unsigned char *buf,
9419 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009420{
9421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9422 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009423 size_t server_name_list_len, hostname_len;
9424 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009425
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9429 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009430 p += 2;
9431
Gilles Peskine449bd832023-01-11 14:50:10 +01009432 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009433 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009434 while (p < server_name_list_end) {
9435 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9436 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9437 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9438 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009439
Gilles Peskine449bd832023-01-11 14:50:10 +01009440 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009441 /* sni_name is intended to be used only during the parsing of the
9442 * ClientHello message (it is reset to NULL before the end of
9443 * the message parsing). Thus it is ok to just point to the
9444 * reception buffer and not make a copy of it.
9445 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009446 ssl->handshake->sni_name = p + 3;
9447 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 if (ssl->conf->f_sni == NULL) {
9449 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009450 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009451 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9452 ssl, p + 3, hostname_len);
9453 if (ret != 0) {
9454 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9455 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9456 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9457 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9458 }
9459 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009460 }
9461
9462 p += hostname_len + 3;
9463 }
9464
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009466}
9467#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9468
XiaokangQianacb39922022-06-17 10:18:48 +00009469#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009470MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009471int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9472 const unsigned char *buf,
9473 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009474{
9475 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009476 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009477 const unsigned char *protocol_name_list;
9478 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009479 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009480
9481 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 if (ssl->conf->alpn_list == NULL) {
9483 return 0;
9484 }
XiaokangQianacb39922022-06-17 10:18:48 +00009485
9486 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009487 * RFC7301, section 3.1
9488 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009489 *
XiaokangQianc7403452022-06-23 03:24:12 +00009490 * struct {
9491 * ProtocolName protocol_name_list<2..2^16-1>
9492 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009493 */
9494
XiaokangQianc7403452022-06-23 03:24:12 +00009495 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009496 * protocol_name_list_len 2 bytes
9497 * protocol_name_len 1 bytes
9498 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009501
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009503 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009505 protocol_name_list = p;
9506 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009507
9508 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009509 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9512 protocol_name_len);
9513 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009514 MBEDTLS_SSL_PEND_FATAL_ALERT(
9515 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009516 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9517 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009518 }
9519
9520 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009521 }
9522
9523 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009524 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9525 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009526 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009527 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009528 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009529 if (protocol_name_len == alpn_len &&
9530 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009531 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009532 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009533 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009534
9535 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009536 }
9537 }
9538
XiaokangQian95d5f542022-06-24 02:29:26 +00009539 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009540 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9542 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9543 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009544}
9545
Gilles Peskine449bd832023-01-11 14:50:10 +01009546int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9547 unsigned char *buf,
9548 unsigned char *end,
9549 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009550{
9551 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009552 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009553 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009554
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 if (ssl->alpn_chosen == NULL) {
9556 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009557 }
9558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 protocol_name_len = strlen(ssl->alpn_chosen);
9560 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009561
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009563 /*
9564 * 0 . 1 ext identifier
9565 * 2 . 3 ext length
9566 * 4 . 5 protocol list length
9567 * 6 . 6 protocol name length
9568 * 7 . 7+n protocol name
9569 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009570 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009571
XiaokangQian95d5f542022-06-24 02:29:26 +00009572 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009573
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9575 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009576 /* Note: the length of the chosen protocol has been checked to be less
9577 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009579 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009580
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009582
9583#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009585#endif
9586
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009588}
9589#endif /* MBEDTLS_SSL_ALPN */
9590
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009591#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009592 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009593 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009594 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009595int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9596 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009597{
9598 /* Initialize to suppress unnecessary compiler warning */
9599 size_t hostname_len = 0;
9600
9601 /* Check if new hostname is valid before
9602 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009603 if (hostname != NULL) {
9604 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009605
Gilles Peskine449bd832023-01-11 14:50:10 +01009606 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9608 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009609 }
9610
9611 /* Now it's clear that we will overwrite the old hostname,
9612 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01009614 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01009615 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009616 }
9617
9618 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009620 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 } else {
9622 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9623 if (session->hostname == NULL) {
9624 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9625 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009626
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009628 }
9629
Gilles Peskine449bd832023-01-11 14:50:10 +01009630 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009631}
Xiaokang Qian03409292022-10-12 02:49:52 +00009632#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9633 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009634 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009635 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637#endif /* MBEDTLS_SSL_TLS_C */