blob: 82ff2ccb5247bcb3fa85fa544f1efd12f478d973 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * http://www.ietf.org/rfc/rfc2246.txt
9 * http://www.ietf.org/rfc/rfc4346.txt
10 */
11
Harry Ramsey0f6bc412024-10-04 10:36:54 +010012#include "ssl_misc.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000015
SimonBd5800b72016-04-26 07:43:27 +010016#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010019#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010020#include "ssl_debug_helpers.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040021
Valerio Settib4f50762024-01-17 10:24:52 +010022#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000023#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050024#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010025#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020026#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <string.h>
29
Valerio Setti384fbde2024-01-02 13:26:40 +010030#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020031#include "md_psa.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020032#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050033#include "psa/crypto.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050034
Janos Follath23bdca02016-10-07 14:47:14 +010035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020037#endif
38
Andrzej Kurek00644842023-05-30 05:45:00 -040039/* Define local translating functions to save code size by not using too many
40 * arguments in each translating place. */
41static int local_err_translation(psa_status_t status)
42{
43 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040044 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040045 psa_generic_status_to_mbedtls);
46}
47#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050048
Ronald Cronad8c17b2022-06-10 17:18:09 +020049#if defined(MBEDTLS_TEST_HOOKS)
50static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
51
52void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010053 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020054{
55 chk_buf_ptr_fail_args.cur = cur;
56 chk_buf_ptr_fail_args.end = end;
57 chk_buf_ptr_fail_args.need = need;
58}
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020061{
Gilles Peskine449bd832023-01-11 14:50:10 +010062 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020063}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020066{
Gilles Peskine449bd832023-01-11 14:50:10 +010067 return (chk_buf_ptr_fail_args.cur != args->cur) ||
68 (chk_buf_ptr_fail_args.end != args->end) ||
69 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020070}
71#endif /* MBEDTLS_TEST_HOOKS */
72
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010074
Hanno Beckera0e20d02019-05-15 14:03:01 +010075#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010076/* Top-level Connection ID API */
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
79 size_t len,
80 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010081{
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
83 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
84 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
87 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
88 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010089 }
90
91 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010092 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +010094}
95
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
97 int enable,
98 unsigned char const *own_cid,
99 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
102 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
103 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100104
Hanno Beckerca092242019-04-25 16:01:49 +0100105 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (enable == MBEDTLS_SSL_CID_DISABLED) {
107 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
108 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100109 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
111 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (own_cid_len != ssl->conf->cid_len) {
114 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
115 (unsigned) own_cid_len,
116 (unsigned) ssl->conf->cid_len));
117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100121 /* Truncation is not an issue here because
122 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
123 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100126}
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
129 int *enabled,
Sam Berry3504c882024-06-11 14:34:17 +0100130 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX],
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000132{
133 *enabled = MBEDTLS_SSL_CID_DISABLED;
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
136 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
137 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000138
139 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
140 * zero as this is indistinguishable from not requesting to use
141 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
143 return 0;
144 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000147 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (own_cid != NULL) {
149 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151 }
152
153 *enabled = MBEDTLS_SSL_CID_ENABLED;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000156}
157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
159 int *enabled,
160 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
161 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100162{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
166 mbedtls_ssl_is_handshake_over(ssl) == 0) {
167 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100168 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100169
Hanno Beckerc5f24222019-05-03 12:54:52 +0100170 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
171 * were used, but client and server requested the empty CID.
172 * This is indistinguishable from not using the CID extension
173 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (ssl->transform_in->in_cid_len == 0 &&
175 ssl->transform_in->out_cid_len == 0) {
176 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100177 }
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100180 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (peer_cid != NULL) {
182 memcpy(peer_cid, ssl->transform_in->out_cid,
183 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100184 }
185 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100186
187 *enabled = MBEDTLS_SSL_CID_ENABLED;
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100190}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100191#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200196/*
197 * Convert max_fragment_length codes to length.
198 * RFC 6066 says:
199 * enum{
200 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
201 * } MaxFragmentLength;
202 * and we add 0 -> extension unused
203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 switch (mfl) {
207 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
208 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
209 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
210 return 512;
211 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
212 return 1024;
213 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
214 return 2048;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
216 return 4096;
217 default:
218 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000219 }
220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
224 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200225{
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_ssl_session_free(dst);
227 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800228#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
229 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000230#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
231 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000232 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800233#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000234#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800235
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000236#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
237 defined(MBEDTLS_SSL_EARLY_DATA)
238 dst->ticket_alpn = NULL;
239#endif
240
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
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000278#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_ALPN) && \
279 defined(MBEDTLS_SSL_EARLY_DATA)
280 {
281 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
282 if (ret != 0) {
283 return ret;
284 }
285 }
286#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_ALPN && MBEDTLS_SSL_EARLY_DATA */
287
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200288#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (src->ticket != NULL) {
290 dst->ticket = mbedtls_calloc(1, src->ticket_len);
291 if (dst->ticket == NULL) {
292 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
293 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297
298#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
299 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
303 if (ret != 0) {
304 return ret;
305 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306 }
307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
308 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200309#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312}
313
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100316static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317{
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
319 if (resized_buffer == NULL) {
Yanray Wang365ee3e2023-11-22 10:28:28 +0800320 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 /* We want to copy len_new bytes when downsizing the buffer, and
324 * len_old bytes when upsizing, so we choose the smaller of two sizes,
325 * to fit one buffer into another. Size checks, ensuring that no data is
326 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memcpy(resized_buffer, *buffer,
328 (len_new < *len_old) ? len_new : *len_old);
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100329 mbedtls_zeroize_and_free(*buffer, *len_old);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500330
331 *buffer = resized_buffer;
332 *len_old = len_new;
333
334 return 0;
335}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
338 size_t in_buf_new_len,
339 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340{
341 int modified = 0;
342 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
343 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345 written_in = ssl->in_msg - ssl->in_buf;
346 iv_offset_in = ssl->in_iv - ssl->in_buf;
347 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200349 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 ssl->in_buf_len < in_buf_new_len) {
351 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
352 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
353 } else {
354 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
355 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200356 modified = 1;
357 }
358 }
359 }
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200362 written_out = ssl->out_msg - ssl->out_buf;
363 iv_offset_out = ssl->out_iv - ssl->out_buf;
364 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200366 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 ssl->out_buf_len < out_buf_new_len) {
368 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
369 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
370 } else {
371 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
372 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200373 modified = 1;
374 }
375 }
376 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200380 /* Fields below might not be properly updated with record
381 * splitting or with CID, so they are manually updated here. */
382 ssl->out_msg = ssl->out_buf + written_out;
383 ssl->out_len = ssl->out_buf + len_offset_out;
384 ssl->out_iv = ssl->out_buf + iv_offset_out;
385
386 ssl->in_msg = ssl->in_buf + written_in;
387 ssl->in_len = ssl->in_buf + len_offset_in;
388 ssl->in_iv = ssl->in_buf + iv_offset_in;
389 }
390}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500391#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
392
Jerry Yudb8c48a2022-01-27 14:54:54 +0800393#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100396typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
397 const char *label,
398 const unsigned char *random, size_t rlen,
399 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800402
403#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
404
405/* Type for the TLS PRF */
406typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
407 const unsigned char *, size_t,
408 unsigned char *, size_t);
409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200410MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100411static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
412 int ciphersuite,
413 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200416#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 ssl_tls_prf_t tls_prf,
418 const unsigned char randbytes[64],
419 mbedtls_ssl_protocol_version tls_version,
420 unsigned endpoint,
421 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800422
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100423#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200424MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100425static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300426 const char *label,
427 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100429static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
430static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100431
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100432#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100433
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100434#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100435MBEDTLS_CHECK_RETURN_CRITICAL
436static int tls_prf_sha384(const unsigned char *secret, size_t slen,
437 const char *label,
438 const unsigned char *random, size_t rlen,
439 unsigned char *dstbuf, size_t dlen);
440
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100441static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
442static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100443#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445MBEDTLS_CHECK_RETURN_CRITICAL
446static int ssl_tls12_session_load(mbedtls_ssl_session *session,
447 const unsigned char *buf,
448 size_t len);
449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
450
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100451static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100452
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100453#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100455#endif /* PSA_WANT_ALG_SHA_256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100456
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100457#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100458static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100459#endif /* PSA_WANT_ALG_SHA_384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100460
461int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
462 const unsigned char *secret, size_t slen,
463 const char *label,
464 const unsigned char *random, size_t rlen,
465 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300466{
467 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100471#if defined(PSA_WANT_ALG_SHA_384)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300472 case MBEDTLS_SSL_TLS_PRF_SHA384:
473 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 break;
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100475#endif /* PSA_WANT_ALG_SHA_384*/
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100476#if defined(PSA_WANT_ALG_SHA_256)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA256:
478 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 break;
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100480#endif /* PSA_WANT_ALG_SHA_256*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300481#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 default:
483 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300487}
488
Jerry Yuc73c6182022-02-08 20:29:25 +0800489#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800491{
492#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (session->peer_cert != NULL) {
494 mbedtls_x509_crt_free(session->peer_cert);
495 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 session->peer_cert = NULL;
497 }
498#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800500 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800502 session->peer_cert_digest = NULL;
503 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
504 session->peer_cert_digest_len = 0;
505 }
506#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
507}
508#endif /* MBEDTLS_X509_CRT_PARSE_C */
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800511{
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800513 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800515
516 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800518
519 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800521
522 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800524
525 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800527
528 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800530
531 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800533
534 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800536
537 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800539
540 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800542
543 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800545
546 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800548
549 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800551
552 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800554
555 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800557
558 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800560
561 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800563
564 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800566
567 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800569
570 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800572
573 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800575
576 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800578
579 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800581
582 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800584
585 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800587
588 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800590
Jan Bruckner151f6422023-02-10 12:45:19 +0100591 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
592 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
593
Jerry Yu7a485c12022-10-31 13:08:18 +0800594 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800596
597 }
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800600}
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800603{
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800605}
606
Jerry Yud25cab02022-10-31 12:48:30 +0800607#if defined(MBEDTLS_DEBUG_C)
608static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800609 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800610 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
611 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
612 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
613 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
614 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
615 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
616 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
617 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
618 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
619 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
620 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
621 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
622 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
623 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
624 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
625 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
626 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
627 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
628 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
629 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
630 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
631 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
632 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
633 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
634 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
635 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100636 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
637 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800638};
639
Chien Wong4e9683e2023-12-28 17:07:43 +0800640static const unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800641 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
642 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
643 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
644 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
645 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
646 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
647 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
648 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
649 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
650 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
651 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
652 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
653 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
654 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
655 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
656 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
657 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
658 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
659 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
660 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
661 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
662 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
663 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
664 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
665 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
666 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
667 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100668 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
669 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800670};
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return extension_name_table[
675 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800676}
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800679{
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800681 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800683 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800697}
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
700 int level, const char *file, int line,
701 int hs_msg_type, unsigned int extension_type,
702 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800703{
704 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800706 mbedtls_debug_print_msg(
707 ssl, level, file, line,
708 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ssl_tls13_get_hs_msg_name(hs_msg_type),
710 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800711 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800713 return;
714 }
715
716 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
721 mbedtls_ssl_get_extension_name(extension_type), extension_type,
722 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800723 return;
724 }
725
726 mbedtls_debug_print_msg(
727 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
729 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800730}
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
733 int level, const char *file, int line,
734 int hs_msg_type, uint32_t extensions_mask,
735 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800736{
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 for (unsigned i = 0;
739 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
740 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800741 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800742 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800744 }
745}
746
Pengyu Lvee455c02023-01-12 14:37:24 +0800747#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lvee455c02023-01-12 14:37:24 +0800748static const char *ticket_flag_name_table[] =
749{
750 [0] = "ALLOW_PSK_RESUMPTION",
751 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
752 [3] = "ALLOW_EARLY_DATA",
753};
754
Pengyu Lv4938a562023-01-16 11:28:49 +0800755void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
756 int level, const char *file, int line,
757 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800758{
759 size_t i;
760
761 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800762 "print ticket_flags (0x%02x)", flags);
763
764 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800765
766 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800767 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800768 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
769 ticket_flag_name_table[i]);
770 }
771 }
772}
773#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
774
Jerry Yud25cab02022-10-31 12:48:30 +0800775#endif /* MBEDTLS_DEBUG_C */
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
778 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800779{
780 ((void) ciphersuite_info);
781
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100782#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800784 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800786#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100787#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800789 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800791#endif
792 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800794 return;
795 }
796}
797
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100798int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100799 unsigned hs_type,
800 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100801{
802 unsigned char hs_hdr[4];
803
804 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
806 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
807 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
808 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100809
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100810 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100811}
812
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100813int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100814 unsigned hs_type,
815 unsigned char const *msg,
816 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100818 int ret;
819 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100820 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100821 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100822 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100823 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100824}
825
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100826int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800827{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100828#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100829 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100830 psa_status_t status;
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100831#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800832 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100833#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100834#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100835 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
836 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200837 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100838 }
839 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
840 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200841 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100842 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100844#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
846 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200847 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100848 }
849 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
850 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200851 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100852 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800853#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100854 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800855}
856
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100857static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100858 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800859{
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100860#if defined(PSA_WANT_ALG_SHA_256) || \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100861 defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100862 psa_status_t status;
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100863#else /* SHA-256 or SHA-384 */
864 ((void) ssl);
865 (void) buf;
866 (void) len;
867#endif /* SHA-256 or SHA-384 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100868#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100869 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
870 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200871 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100872 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800873#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100874#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100875 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
876 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200877 return mbedtls_md_error_from_psa(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100878 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800879#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100880 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800881}
882
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100883#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100884static int ssl_update_checksum_sha256(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é-Gonnard3761e9e2023-03-28 12:54:56 +0200887 return mbedtls_md_error_from_psa(psa_hash_update(
888 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800889}
890#endif
891
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100892#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100893static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100894 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800895{
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +0200896 return mbedtls_md_error_from_psa(psa_hash_update(
897 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800898}
899#endif
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200902{
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200904
Elena Uziunaite0916cd72024-05-23 17:01:07 +0100905#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500906 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500907#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +0100908#if defined(PSA_WANT_ALG_SHA_384)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500909 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500910#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200911
912 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200916#endif
Valerio Setti7aeec542023-07-05 18:57:21 +0200917#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +0200918 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200920#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200921#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200922 handshake->psa_pake_ctx = psa_pake_operation_init();
923 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200924#if defined(MBEDTLS_SSL_CLI_C)
925 handshake->ecjpake_cache = NULL;
926 handshake->ecjpake_cache_len = 0;
927#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200928#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200929
Gilles Peskineeccd8882020-03-10 12:19:08 +0100930#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200932#endif
933
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200934#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
935 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
936#endif
Hanno Becker75173122019-02-06 16:18:31 +0000937
938#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
939 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000941#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200942}
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200945{
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200947
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100948 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
949 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100950
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000951#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100952 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
953 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100954#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200955}
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200958{
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960}
961
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200962MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100963static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000964{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
966
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (ssl->transform_negotiate) {
970 mbedtls_ssl_transform_free(ssl->transform_negotiate);
971 }
Jerry Yu2e199812022-12-01 18:57:19 +0800972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (ssl->session_negotiate) {
974 mbedtls_ssl_session_free(ssl->session_negotiate);
975 }
976 if (ssl->handshake) {
977 mbedtls_ssl_handshake_free(ssl);
978 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979
Jerry Yu2e199812022-12-01 18:57:19 +0800980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200981 /*
982 * Either the pointers are now NULL or cleared properly and can be freed.
983 * Now allocate missing structures.
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (ssl->transform_negotiate == NULL) {
986 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200987 }
Jerry Yu2e199812022-12-01 18:57:19 +0800988#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (ssl->session_negotiate == NULL) {
991 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200992 }
Paul Bakker48916f92012-09-16 19:57:18 +0000993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (ssl->handshake == NULL) {
995 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200996 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500997#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
998 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1001 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001002#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001004 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001006#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001007 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001008#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 ssl->session_negotiate == NULL) {
1010 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001014
1015#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001017 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001018#endif
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001021 ssl->session_negotiate = NULL;
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001024 }
1025
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001026#if defined(MBEDTLS_SSL_EARLY_DATA)
1027#if defined(MBEDTLS_SSL_CLI_C)
Ronald Cron05d7cfb2024-03-03 15:39:30 +01001028 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IDLE;
Ronald Cron5d0ae902024-01-05 14:20:35 +01001029#endif
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001030#if defined(MBEDTLS_SSL_SRV_C)
1031 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
1032#endif
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001033 ssl->total_early_data_size = 0;
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001034#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron5d0ae902024-01-05 14:20:35 +01001035
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001036 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_ssl_session_init(ssl->session_negotiate);
1038 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001039
Jerry Yu2e199812022-12-01 18:57:19 +08001040#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001042#endif
1043
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001044 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001045 ret = mbedtls_ssl_reset_checksum(ssl);
1046 if (ret != 0) {
1047 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1048 return ret;
1049 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001050
Jerry Yud0766ec2022-09-22 10:46:57 +08001051#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1052 defined(MBEDTLS_SSL_SRV_C) && \
1053 defined(MBEDTLS_SSL_SESSION_TICKETS)
1054 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001056#endif
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001060 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001063 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001065 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001069 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001070#endif
1071
Ronald Crone68ab4f2022-10-05 12:46:29 +02001072#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001073#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001074#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001075 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1076 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1078 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001079 const int *md;
1080 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001081 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001082 uint16_t *p;
1083
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001084 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1085 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1086 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1089 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001090 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001092#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001094#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001095
Jerry Yub476a442022-01-21 18:14:45 +08001096#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001098#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1100 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1101 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001102 }
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1105 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1106 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1109 sizeof(uint16_t));
1110 if (ssl->handshake->sig_algs == NULL) {
1111 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1112 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 p = (uint16_t *) ssl->handshake->sig_algs;
1115 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1116 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1117 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001118 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Valerio Settie9646ec2023-08-02 20:02:28 +02001120#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001122 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001123#endif
1124#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001126 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001127#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001128 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001129 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001130 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001132#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001133 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001134 ssl->handshake->sig_algs_heap_allocated = 0;
1135 }
Jerry Yucc539102022-06-27 16:27:35 +08001136#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001137#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001139}
1140
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001141#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001142/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001143MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001144static int ssl_cookie_write_dummy(void *ctx,
1145 unsigned char **p, unsigned char *end,
1146 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001147{
1148 ((void) ctx);
1149 ((void) p);
1150 ((void) end);
1151 ((void) cli_id);
1152 ((void) cli_id_len);
1153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001155}
1156
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001157MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001158static int ssl_cookie_check_dummy(void *ctx,
1159 const unsigned char *cookie, size_t cookie_len,
1160 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001161{
1162 ((void) ctx);
1163 ((void) cookie);
1164 ((void) cookie_len);
1165 ((void) cli_id);
1166 ((void) cli_id_len);
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001169}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001170#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001171
Paul Bakker5121ce52009-01-03 21:22:43 +00001172/*
1173 * Initialize an SSL context
1174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001176{
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001178}
1179
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001180MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001181static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001182{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001183 const mbedtls_ssl_config *conf = ssl->conf;
1184
Ronald Cron6f135e12021-12-08 16:57:54 +01001185#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1187 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1188 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1189 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1193 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001194 }
1195#endif
1196
1197#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1199 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1200 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001201 }
1202#endif
1203
Ronald Cron6f135e12021-12-08 16:57:54 +01001204#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1206 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1207 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1208 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001209 }
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1212 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001213 }
1214#endif
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1217 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001218}
1219
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001220MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001221static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1222{
1223 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 ret = ssl_conf_version_check(ssl);
1225 if (ret != 0) {
1226 return ret;
1227 }
Jerry Yu60835a82021-08-04 10:13:52 +08001228
Yanray Wangb422cab2023-12-01 16:18:10 +08001229 if (ssl->conf->f_rng == NULL) {
1230 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1231 return MBEDTLS_ERR_SSL_NO_RNG;
1232 }
1233
Jerry Yu60835a82021-08-04 10:13:52 +08001234 /* Space for further checks */
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001237}
1238
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001239/*
1240 * Setup an SSL context
1241 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1244 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001245{
Janos Follath865b3eb2019-12-16 11:46:15 +00001246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001247 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1248 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001250 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = ssl_conf_check(ssl)) != 0) {
1253 return ret;
1254 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001255 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001256
Paul Bakker62f2dee2012-09-28 07:31:51 +00001257 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001258 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001259 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001260
1261 /* Set to NULL in case of an error condition */
1262 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001263
Darryl Greenb33cc762019-11-28 14:29:44 +00001264#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1265 ssl->in_buf_len = in_buf_len;
1266#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1268 if (ssl->in_buf == NULL) {
1269 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001270 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001271 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001272 }
1273
Darryl Greenb33cc762019-11-28 14:29:44 +00001274#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1275 ssl->out_buf_len = out_buf_len;
1276#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1278 if (ssl->out_buf == NULL) {
1279 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001280 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001281 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 }
1283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001285
Johan Pascalb62bb512015-12-03 21:56:45 +01001286#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001288#endif
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001291 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001295
1296error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 mbedtls_free(ssl->in_buf);
1298 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001299
1300 ssl->conf = NULL;
1301
Darryl Greenb33cc762019-11-28 14:29:44 +00001302#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1303 ssl->in_buf_len = 0;
1304 ssl->out_buf_len = 0;
1305#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001306 ssl->in_buf = NULL;
1307 ssl->out_buf = NULL;
1308
1309 ssl->in_hdr = NULL;
1310 ssl->in_ctr = NULL;
1311 ssl->in_len = NULL;
1312 ssl->in_iv = NULL;
1313 ssl->in_msg = NULL;
1314
1315 ssl->out_hdr = NULL;
1316 ssl->out_ctr = NULL;
1317 ssl->out_len = NULL;
1318 ssl->out_iv = NULL;
1319 ssl->out_msg = NULL;
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001322}
1323
1324/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001325 * Reset an initialized and used SSL context for re-use while retaining
1326 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001327 *
1328 * If partial is non-zero, keep data in the input buffer and client ID.
1329 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1332 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001333{
Darryl Greenb33cc762019-11-28 14:29:44 +00001334#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1335 size_t in_buf_len = ssl->in_buf_len;
1336 size_t out_buf_len = ssl->out_buf_len;
1337#else
1338 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1339 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1340#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001341
Hanno Beckerb0302c42021-08-03 09:39:42 +01001342#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1343 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001344#endif
1345
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001346 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001350
1351 /* Reset incoming message parsing */
1352 ssl->in_offt = NULL;
1353 ssl->nb_zero = 0;
1354 ssl->in_msgtype = 0;
1355 ssl->in_msglen = 0;
1356 ssl->in_hslen = 0;
1357 ssl->keep_current_message = 0;
1358 ssl->transform_in = NULL;
1359
1360#if defined(MBEDTLS_SSL_PROTO_DTLS)
1361 ssl->next_record_offset = 0;
1362 ssl->in_epoch = 0;
1363#endif
1364
1365 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001367 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001369 }
1370
Ronald Cronad8c17b2022-06-10 17:18:09 +02001371 ssl->send_alert = 0;
1372
Hanno Beckerb0302c42021-08-03 09:39:42 +01001373 /* Reset outgoing message writing */
1374 ssl->out_msgtype = 0;
1375 ssl->out_msglen = 0;
1376 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 memset(ssl->out_buf, 0, out_buf_len);
1378 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001379 ssl->transform_out = NULL;
1380
1381#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001383#endif
1384
XiaokangQian2b01dc32022-01-21 02:53:13 +00001385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 if (ssl->transform) {
1387 mbedtls_ssl_transform_free(ssl->transform);
1388 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001389 ssl->transform = NULL;
1390 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001391#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1392
XiaokangQian2b01dc32022-01-21 02:53:13 +00001393#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 mbedtls_ssl_transform_free(ssl->transform_application);
1395 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001396 ssl->transform_application = NULL;
1397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001399#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1401 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001402 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001403#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1406 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001407 ssl->handshake->transform_handshake = NULL;
1408 }
1409
XiaokangQian2b01dc32022-01-21 02:53:13 +00001410#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001411}
1412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001414{
1415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1416
1417 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Ronald Cron195c0bc2024-02-08 08:51:20 +01001418 ssl->tls_version = ssl->conf->max_tls_version;
Hanno Beckerb0302c42021-08-03 09:39:42 +01001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001421
1422 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_RENEGOTIATION)
1424 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001425 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001426
1427 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1429 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001432
Hanno Beckerb0302c42021-08-03 09:39:42 +01001433 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001434 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (ssl->session) {
1436 mbedtls_ssl_session_free(ssl->session);
1437 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001438 ssl->session = NULL;
1439 }
1440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001442 ssl->alpn_chosen = NULL;
1443#endif
1444
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001445#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001446 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001447#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001449#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (free_cli_id) {
1451 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001452 ssl->cli_id = NULL;
1453 ssl->cli_id_len = 0;
1454 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001455#endif
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if ((ret = ssl_handshake_init(ssl)) != 0) {
1458 return ret;
1459 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001462}
1463
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001464/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001465 * Reset an initialized and used SSL context for re-use while retaining
1466 * all application-set variables, function pointers and data.
1467 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001469{
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001471}
1472
1473/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 * SSL set accessors
1475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001477{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001478 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001479}
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001482{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001483 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001484}
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001488{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001489 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001490}
1491#endif
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001494{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001495 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001496}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1501 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001502{
1503 ssl->disable_datagram_packing = !allow_packing;
1504}
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1507 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001508{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001509 conf->hs_timeout_min = min;
1510 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001511}
1512#endif
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001516 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001517}
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001520void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1521 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1522 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001523{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001524 conf->f_vrfy = f_vrfy;
1525 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1530 int (*f_rng)(void *, unsigned char *, size_t),
1531 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001532{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001533 conf->f_rng = f_rng;
1534 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001535}
1536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1538 void (*f_dbg)(void *, int, const char *, int, const char *),
1539 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001541 conf->f_dbg = f_dbg;
1542 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001543}
1544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1546 void *p_bio,
1547 mbedtls_ssl_send_t *f_send,
1548 mbedtls_ssl_recv_t *f_recv,
1549 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001550{
1551 ssl->p_bio = p_bio;
1552 ssl->f_send = f_send;
1553 ssl->f_recv = f_recv;
1554 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001555}
1556
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001558void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001559{
1560 ssl->mtu = mtu;
1561}
1562#endif
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001565{
1566 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001567}
1568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1570 void *p_timer,
1571 mbedtls_ssl_set_timer_t *f_set_timer,
1572 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001573{
1574 ssl->p_timer = p_timer;
1575 ssl->f_set_timer = f_set_timer;
1576 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001577
1578 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001580}
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1584 void *p_cache,
1585 mbedtls_ssl_cache_get_t *f_get_cache,
1586 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001587{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001588 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001589 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001590 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001595int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001596{
Janos Follath865b3eb2019-12-16 11:46:15 +00001597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001600 session == NULL ||
1601 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001604 }
1605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (ssl->handshake->resume == 1) {
1607 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1608 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001609
Jerry Yu21092062022-10-10 21:21:31 +08001610#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron233fcaa2024-04-04 14:05:21 +02001612#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu21092062022-10-10 21:21:31 +08001613 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001617 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1619 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1620 session->ciphersuite));
1621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001622 }
Ronald Cron233fcaa2024-04-04 14:05:21 +02001623#else
1624 /*
1625 * If session tickets are not enabled, it is not possible to resume a
1626 * TLS 1.3 session, thus do not make any change to the SSL context in
1627 * the first place.
1628 */
1629 return 0;
1630#endif
Jerry Yu40afab62022-10-08 10:42:13 +08001631 }
Jerry Yu21092062022-10-10 21:21:31 +08001632#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1635 session)) != 0) {
1636 return ret;
1637 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001638
Paul Bakker0a597072012-09-25 21:55:46 +00001639 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001642}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1646 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001647{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001648 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001649}
1650
Ronald Cron6f135e12021-12-08 16:57:54 +01001651#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001652void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1653 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001654{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001655 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001656}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001657
1658#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08001659void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
1660 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001661{
1662 conf->early_data_enabled = early_data_enabled;
1663}
Jerry Yucc4e0072022-11-22 17:22:22 +08001664
1665#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08001666void mbedtls_ssl_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001668{
Jerry Yu39da9852022-12-06 16:58:36 +08001669 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001670}
1671#endif /* MBEDTLS_SSL_SRV_C */
1672
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001673#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001677void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1678 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001679{
1680 conf->cert_profile = profile;
1681}
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001684{
1685 mbedtls_ssl_key_cert *cur = key_cert, *next;
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001688 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001690 cur = next;
1691 }
1692}
1693
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001694/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001695MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001696static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1697 mbedtls_x509_crt *cert,
1698 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001699{
niisato8ee24222018-06-25 19:05:48 +09001700 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001701
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001703 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001705 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001707 }
1708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1710 if (new_cert == NULL) {
1711 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1712 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001713
niisato8ee24222018-06-25 19:05:48 +09001714 new_cert->cert = cert;
1715 new_cert->key = key;
1716 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001717
Glenn Strauss36872db2022-01-22 05:06:31 -05001718 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001720 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001722 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001724 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 }
niisato8ee24222018-06-25 19:05:48 +09001726 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001727 }
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001730}
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001733 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001735{
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001737}
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001740 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001742{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001743 conf->ca_chain = ca_chain;
1744 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001745
1746#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1747 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1748 * cannot be used together. */
1749 conf->f_ca_cb = NULL;
1750 conf->p_ca_cb = NULL;
1751#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001752}
Hanno Becker5adaad92019-03-27 16:54:37 +00001753
1754#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001755void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1756 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1757 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001758{
1759 conf->f_ca_cb = f_ca_cb;
1760 conf->p_ca_cb = p_ca_cb;
1761
1762 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1763 * cannot be used together. */
1764 conf->ca_chain = NULL;
1765 conf->ca_crl = NULL;
1766}
1767#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001769
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001770#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001771const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1772 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001773{
1774 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001776}
1777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1779 mbedtls_x509_crt *own_cert,
1780 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001781{
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1783 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001784}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1787 mbedtls_x509_crt *ca_chain,
1788 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001789{
1790 ssl->handshake->sni_ca_chain = ca_chain;
1791 ssl->handshake->sni_ca_crl = ca_crl;
1792}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001793
Glenn Strauss999ef702022-03-11 01:37:23 -05001794#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001795void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1796 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001797{
1798 ssl->handshake->dn_hints = crt;
1799}
1800#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1803 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001804{
1805 ssl->handshake->sni_authmode = authmode;
1806}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001807#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1808
Hanno Becker8927c832019-04-03 12:52:50 +01001809#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001810void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1811 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1812 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001813{
1814 ssl->f_vrfy = f_vrfy;
1815 ssl->p_vrfy = p_vrfy;
1816}
1817#endif
1818
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001819#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001820
Przemek Stekielfde11282023-03-13 16:06:09 +01001821static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1822static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1823
Valerio Setti016f6822022-12-09 14:17:50 +01001824static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 mbedtls_ssl_context *ssl,
1826 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001827{
1828 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001829 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001830 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001831 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001832 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001833 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1835 psa_pake_cs_set_primitive(&cipher_suite,
1836 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1837 PSA_ECC_FAMILY_SECP_R1,
1838 256));
1839 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1842 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001843 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001847 user = jpake_server_id;
1848 user_len = sizeof(jpake_server_id);
1849 peer = jpake_client_id;
1850 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001852 user = jpake_client_id;
1853 user_len = sizeof(jpake_client_id);
1854 peer = jpake_server_id;
1855 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001857
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001858 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1859 if (status != PSA_SUCCESS) {
1860 return status;
1861 }
1862
1863 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001865 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 }
Valerio Setti016f6822022-12-09 14:17:50 +01001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1869 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001870 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 }
Valerio Setti016f6822022-12-09 14:17:50 +01001872
1873 ssl->handshake->psa_pake_ctx_is_ok = 1;
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001876}
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1879 const unsigned char *pw,
1880 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001881{
1882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1883 psa_status_t status;
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (ssl->handshake == NULL || ssl->conf == NULL) {
1886 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001887 }
1888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 /* Empty password is not valid */
1890 if ((pw == NULL) || (pw_len == 0)) {
1891 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1892 }
1893
1894 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1895 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1896 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1897
1898 status = psa_import_key(&attributes, pw, pw_len,
1899 &ssl->handshake->psa_pake_password);
1900 if (status != PSA_SUCCESS) {
1901 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1902 }
1903
1904 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1905 ssl->handshake->psa_pake_password);
1906 if (status != PSA_SUCCESS) {
1907 psa_destroy_key(ssl->handshake->psa_pake_password);
1908 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1909 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1910 }
1911
1912 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001913}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1916 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001917{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001918 psa_status_t status;
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 if (ssl->handshake == NULL || ssl->conf == NULL) {
1921 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 if (mbedtls_svc_key_id_is_null(pwd)) {
1925 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1926 }
1927
1928 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1929 if (status != PSA_SUCCESS) {
1930 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1931 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1932 }
1933
1934 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001935}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001936#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001937
Ronald Cron73fe8df2022-10-05 14:31:43 +02001938#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001939int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001940{
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (conf->psk_identity == NULL ||
1942 conf->psk_identity_len == 0) {
1943 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02001944 }
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
1947 return 1;
1948 }
Ronald Crond29e13e2022-10-19 10:33:48 +02001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (conf->psk != NULL && conf->psk_len != 0) {
1951 return 1;
1952 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001955}
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001958{
1959 /* Remove reference to existing PSK, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001961 /* The maintenance of the PSK key slot is the
1962 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001963 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001964 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001966 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001967 conf->psk = NULL;
1968 conf->psk_len = 0;
1969 }
1970
1971 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (conf->psk_identity != NULL) {
1973 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001974 conf->psk_identity = NULL;
1975 conf->psk_identity_len = 0;
1976 }
1977}
1978
Hanno Becker7390c712018-11-15 13:33:04 +00001979/* This function assumes that PSK identity in the SSL config is unset.
1980 * It checks that the provided identity is well-formed and attempts
1981 * to make a copy of it in the SSL config.
1982 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001983MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001984static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
1985 unsigned char const *psk_identity,
1986 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001987{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001988 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02001990 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 (psk_identity_len >> 16) != 0 ||
1992 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
1993 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02001994 }
1995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
1997 if (conf->psk_identity == NULL) {
1998 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1999 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002000
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002001 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002005}
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2008 const unsigned char *psk, size_t psk_len,
2009 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002010{
Janos Follath865b3eb2019-12-16 11:46:15 +00002011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002012
2013 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2015 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2016 }
Hanno Becker7390c712018-11-15 13:33:04 +00002017
2018 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (psk == NULL) {
2020 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2021 }
2022 if (psk_len == 0) {
2023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2024 }
2025 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2026 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2027 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2030 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2031 }
Hanno Becker7390c712018-11-15 13:33:04 +00002032 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002034
2035 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2037 if (ret != 0) {
2038 ssl_conf_remove_psk(conf);
2039 }
Hanno Becker7390c712018-11-15 13:33:04 +00002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002042}
2043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002045{
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002047 /* The maintenance of the external PSK key slot is the
2048 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ssl->handshake->psk_opaque_is_internal) {
2050 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002051 ssl->handshake->psk_opaque_is_internal = 0;
2052 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002053 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002054 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002055}
2056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2058 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002059{
Neil Armstrong501c9322022-05-03 09:35:09 +02002060 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002062 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (psk == NULL || ssl->handshake == NULL) {
2066 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2067 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2070 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2071 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002074
Jerry Yuccc68a42022-07-26 16:39:20 +08002075#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2077 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2078 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2079 } else {
2080 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2081 }
2082 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002083 }
2084#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002085
Jerry Yu568ec252022-07-22 21:27:34 +08002086#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2088 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2089 psa_set_key_usage_flags(&key_attributes,
2090 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002091 }
2092#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 psa_set_key_algorithm(&key_attributes, alg);
2095 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2098 if (status != PSA_SUCCESS) {
2099 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2100 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002101
2102 /* Allow calling psa_destroy_key() on psk remove */
2103 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002105}
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2108 mbedtls_svc_key_id_t psk,
2109 const unsigned char *psk_identity,
2110 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111{
Janos Follath865b3eb2019-12-16 11:46:15 +00002112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002113
2114 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2116 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2117 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002118
Hanno Becker7390c712018-11-15 13:33:04 +00002119 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (mbedtls_svc_key_id_is_null(psk)) {
2121 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2122 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002123 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002124
2125 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2127 psk_identity_len);
2128 if (ret != 0) {
2129 ssl_conf_remove_psk(conf);
2130 }
Hanno Becker7390c712018-11-15 13:33:04 +00002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002133}
2134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2136 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002137{
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if ((mbedtls_svc_key_id_is_null(psk)) ||
2139 (ssl->handshake == NULL)) {
2140 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2141 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002144 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002146}
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002147
Jerry Yu8897c072022-08-12 13:56:53 +08002148#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002149void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2150 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2151 size_t),
2152 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002153{
2154 conf->f_psk = f_psk;
2155 conf->p_psk = p_psk;
2156}
Jerry Yu8897c072022-08-12 13:56:53 +08002157#endif /* MBEDTLS_SSL_SRV_C */
2158
Ronald Cron73fe8df2022-10-05 14:31:43 +02002159#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002160
Gilles Peskine301711e2022-04-26 16:57:05 +02002161static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002163{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002164#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (alg == PSA_ALG_CBC_NO_PADDING) {
2166 return MBEDTLS_SSL_MODE_CBC;
2167 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002168#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (PSA_ALG_IS_AEAD(alg)) {
2170 return MBEDTLS_SSL_MODE_AEAD;
2171 }
2172 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002173}
2174
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002175
Gilles Peskinee108d982022-04-26 16:50:40 +02002176static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2177 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002179{
2180#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2182 base_mode == MBEDTLS_SSL_MODE_CBC) {
2183 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002184 }
2185#else
2186 (void) encrypt_then_mac;
2187#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002189}
2190
Neil Armstrongab555e02022-04-04 11:07:59 +02002191mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002193{
Gilles Peskinee108d982022-04-26 16:50:40 +02002194 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 transform->psa_alg
Gilles Peskinee108d982022-04-26 16:50:40 +02002196 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002197
Gilles Peskinee108d982022-04-26 16:50:40 +02002198 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002199#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002200 encrypt_then_mac = transform->encrypt_then_mac;
2201#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002203}
2204
Neil Armstrongab555e02022-04-04 11:07:59 +02002205mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002206#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002208#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002210{
Gilles Peskinee108d982022-04-26 16:50:40 +02002211 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2212
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002213 psa_status_t status;
2214 psa_algorithm_t alg;
2215 psa_key_type_t type;
2216 size_t size;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002217 status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
2218 0, &alg, &type, &size);
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (status == PSA_SUCCESS) {
2220 base_mode = mbedtls_ssl_get_base_mode(alg);
2221 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002222
Gilles Peskinee108d982022-04-26 16:50:40 +02002223#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2224 int encrypt_then_mac = 0;
2225#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002227}
2228
Jerry Yu251a12e2022-07-13 15:15:48 +08002229
Ronald Cron8b592d22024-11-12 18:08:25 +01002230const mbedtls_error_pair_t psa_to_ssl_errors[] =
2231{
2232 { PSA_SUCCESS, 0 },
2233 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
2234 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
2235 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
2236 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
2237 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
2238 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
2239};
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2242 size_t taglen,
2243 psa_algorithm_t *alg,
2244 psa_key_type_t *key_type,
2245 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002246{
Elena Uziunaitec2561722024-07-05 11:37:33 +01002247#if !defined(PSA_WANT_ALG_CCM)
Yanray Wang19e4dc82023-11-16 18:02:05 +08002248 (void) taglen;
2249#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 switch (mbedtls_cipher_type) {
Elena Uziunaite74342c72024-07-05 11:31:29 +01002251#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002252 case MBEDTLS_CIPHER_AES_128_CBC:
2253 *alg = PSA_ALG_CBC_NO_PADDING;
2254 *key_type = PSA_KEY_TYPE_AES;
2255 *key_size = 128;
2256 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002257#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002258#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002259 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002261 *key_type = PSA_KEY_TYPE_AES;
2262 *key_size = 128;
2263 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002264#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002265#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002266 case MBEDTLS_CIPHER_AES_128_GCM:
2267 *alg = PSA_ALG_GCM;
2268 *key_type = PSA_KEY_TYPE_AES;
2269 *key_size = 128;
2270 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002271#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002272#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002273 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002275 *key_type = PSA_KEY_TYPE_AES;
2276 *key_size = 192;
2277 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002278#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002279#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002280 case MBEDTLS_CIPHER_AES_192_GCM:
2281 *alg = PSA_ALG_GCM;
2282 *key_type = PSA_KEY_TYPE_AES;
2283 *key_size = 192;
2284 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002285#endif
Elena Uziunaite74342c72024-07-05 11:31:29 +01002286#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002287 case MBEDTLS_CIPHER_AES_256_CBC:
2288 *alg = PSA_ALG_CBC_NO_PADDING;
2289 *key_type = PSA_KEY_TYPE_AES;
2290 *key_size = 256;
2291 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002292#endif
Elena Uziunaitec2561722024-07-05 11:37:33 +01002293#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002294 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002296 *key_type = PSA_KEY_TYPE_AES;
2297 *key_size = 256;
2298 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002299#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +01002300#if defined(PSA_WANT_KEY_TYPE_AES) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002301 case MBEDTLS_CIPHER_AES_256_GCM:
2302 *alg = PSA_ALG_GCM;
2303 *key_type = PSA_KEY_TYPE_AES;
2304 *key_size = 256;
2305 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002306#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002307#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002308 case MBEDTLS_CIPHER_ARIA_128_CBC:
2309 *alg = PSA_ALG_CBC_NO_PADDING;
2310 *key_type = PSA_KEY_TYPE_ARIA;
2311 *key_size = 128;
2312 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002313#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002314#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002315 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002317 *key_type = PSA_KEY_TYPE_ARIA;
2318 *key_size = 128;
2319 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002320#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002321#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002322 case MBEDTLS_CIPHER_ARIA_128_GCM:
2323 *alg = PSA_ALG_GCM;
2324 *key_type = PSA_KEY_TYPE_ARIA;
2325 *key_size = 128;
2326 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002327#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002328#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002329 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002331 *key_type = PSA_KEY_TYPE_ARIA;
2332 *key_size = 192;
2333 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002334#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002335#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002336 case MBEDTLS_CIPHER_ARIA_192_GCM:
2337 *alg = PSA_ALG_GCM;
2338 *key_type = PSA_KEY_TYPE_ARIA;
2339 *key_size = 192;
2340 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002341#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002342#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002343 case MBEDTLS_CIPHER_ARIA_256_CBC:
2344 *alg = PSA_ALG_CBC_NO_PADDING;
2345 *key_type = PSA_KEY_TYPE_ARIA;
2346 *key_size = 256;
2347 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002348#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002349#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002350 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002352 *key_type = PSA_KEY_TYPE_ARIA;
2353 *key_size = 256;
2354 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002355#endif
Elena Uziunaite51c85a02024-07-05 11:20:17 +01002356#if defined(PSA_WANT_KEY_TYPE_ARIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002357 case MBEDTLS_CIPHER_ARIA_256_GCM:
2358 *alg = PSA_ALG_GCM;
2359 *key_type = PSA_KEY_TYPE_ARIA;
2360 *key_size = 256;
2361 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002362#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002363#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002364 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2365 *alg = PSA_ALG_CBC_NO_PADDING;
2366 *key_type = PSA_KEY_TYPE_CAMELLIA;
2367 *key_size = 128;
2368 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002369#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002370#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002371 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002373 *key_type = PSA_KEY_TYPE_CAMELLIA;
2374 *key_size = 128;
2375 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002376#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002377#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002378 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2379 *alg = PSA_ALG_GCM;
2380 *key_type = PSA_KEY_TYPE_CAMELLIA;
2381 *key_size = 128;
2382 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002383#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002384#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002385 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002387 *key_type = PSA_KEY_TYPE_CAMELLIA;
2388 *key_size = 192;
2389 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002390#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002391#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002392 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2393 *alg = PSA_ALG_GCM;
2394 *key_type = PSA_KEY_TYPE_CAMELLIA;
2395 *key_size = 192;
2396 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002397#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002398#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CBC_NO_PADDING)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002399 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2400 *alg = PSA_ALG_CBC_NO_PADDING;
2401 *key_type = PSA_KEY_TYPE_CAMELLIA;
2402 *key_size = 256;
2403 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002404#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002405#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_CCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002406 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002408 *key_type = PSA_KEY_TYPE_CAMELLIA;
2409 *key_size = 256;
2410 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002411#endif
Elena Uziunaiteda41b602024-07-05 11:27:21 +01002412#if defined(PSA_WANT_KEY_TYPE_CAMELLIA) && defined(PSA_WANT_ALG_GCM)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002413 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2414 *alg = PSA_ALG_GCM;
2415 *key_type = PSA_KEY_TYPE_CAMELLIA;
2416 *key_size = 256;
2417 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002418#endif
Elena Uziunaite5c70c302024-07-05 11:44:44 +01002419#if defined(PSA_WANT_ALG_CHACHA20_POLY1305)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002420 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2421 *alg = PSA_ALG_CHACHA20_POLY1305;
2422 *key_type = PSA_KEY_TYPE_CHACHA20;
2423 *key_size = 256;
2424 break;
Yanray Wang1a369d62023-11-16 15:17:31 +08002425#endif
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002426 case MBEDTLS_CIPHER_NULL:
2427 *alg = MBEDTLS_SSL_NULL_CIPHER;
2428 *key_type = 0;
2429 *key_size = 0;
2430 break;
2431 default:
2432 return PSA_ERROR_NOT_SUPPORTED;
2433 }
2434
2435 return PSA_SUCCESS;
2436}
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002437
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002438#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002439int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2440 const unsigned char *dhm_P, size_t P_len,
2441 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002442{
Janos Follath865b3eb2019-12-16 11:46:15 +00002443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 mbedtls_mpi_free(&conf->dhm_P);
2446 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2449 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2450 mbedtls_mpi_free(&conf->dhm_P);
2451 mbedtls_mpi_free(&conf->dhm_G);
2452 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002453 }
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002456}
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002459{
Janos Follath865b3eb2019-12-16 11:46:15 +00002460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 mbedtls_mpi_free(&conf->dhm_P);
2463 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2466 &conf->dhm_P)) != 0 ||
2467 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2468 &conf->dhm_G)) != 0) {
2469 mbedtls_mpi_free(&conf->dhm_P);
2470 mbedtls_mpi_free(&conf->dhm_G);
2471 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002472 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002475}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002476#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002477
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002478#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2479/*
2480 * Set the minimum length for Diffie-Hellman parameters
2481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2483 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002484{
2485 conf->dhm_min_bitlen = bitlen;
2486}
2487#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2488
Ronald Crone68ab4f2022-10-05 12:46:29 +02002489#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002490#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002491/*
2492 * Set allowed/preferred hashes for handshake signatures
2493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2495 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002496{
2497 conf->sig_hashes = hashes;
2498}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002499#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002500
Jerry Yuf017ee42022-01-12 15:49:48 +08002501/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2503 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002504{
Jerry Yuf017ee42022-01-12 15:49:48 +08002505#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2506 conf->sig_hashes = NULL;
2507#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2508 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002509}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002510#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002511
Brett Warrene0edc842021-08-17 09:53:13 +01002512/*
2513 * Set the allowed groups
2514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002515void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2516 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002517{
Brett Warrene0edc842021-08-17 09:53:13 +01002518 conf->group_list = group_list;
2519}
2520
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002522int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002523{
Hanno Becker947194e2017-04-07 13:25:49 +01002524 /* Initialize to suppress unnecessary compiler warning */
2525 size_t hostname_len = 0;
2526
2527 /* Check if new hostname is valid before
2528 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (hostname != NULL) {
2530 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2533 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2534 }
Hanno Becker947194e2017-04-07 13:25:49 +01002535 }
2536
2537 /* Now it's clear that we will overwrite the old hostname,
2538 * so we can free it safely */
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01002541 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Hanno Becker947194e2017-04-07 13:25:49 +01002542 }
2543
2544 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002547 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 } else {
2549 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2550 if (ssl->hostname == NULL) {
2551 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2552 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002555
Hanno Becker947194e2017-04-07 13:25:49 +01002556 ssl->hostname[hostname_len] = '\0';
2557 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002560}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002561#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002563#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002564void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2565 int (*f_sni)(void *, mbedtls_ssl_context *,
2566 const unsigned char *, size_t),
2567 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002568{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002569 conf->f_sni = f_sni;
2570 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002575int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002576{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002577 size_t cur_len, tot_len;
2578 const char **p;
2579
2580 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002581 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2582 * MUST NOT be truncated."
2583 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002584 */
2585 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 for (p = protos; *p != NULL; p++) {
2587 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002588 tot_len += cur_len;
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if ((cur_len == 0) ||
2591 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2592 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2594 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002595 }
2596
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002597 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002600}
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002603{
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002607
Johan Pascalb62bb512015-12-03 21:56:45 +01002608#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002609void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2610 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002611{
2612 conf->dtls_srtp_mki_support = support_mki_value;
2613}
2614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2616 unsigned char *mki_value,
2617 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002618{
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002621 }
Ron Eldor591f1622018-01-22 12:30:04 +02002622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2624 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002625 }
Ron Eldor591f1622018-01-22 12:30:04 +02002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002628 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002630}
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2633 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002634{
Johan Pascal253d0262020-09-22 13:04:45 +02002635 const mbedtls_ssl_srtp_profile *p;
2636 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002637
Johan Pascal253d0262020-09-22 13:04:45 +02002638 /* check the profiles list: all entry must be valid,
2639 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2641 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2642 p++) {
2643 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002644 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002646 /* unsupported value, stop parsing and set the size to an error value */
2647 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002648 }
2649 }
2650
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2652 conf->dtls_srtp_profile_list = NULL;
2653 conf->dtls_srtp_profile_list_len = 0;
2654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002655 }
2656
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002657 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002658 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002661}
2662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2664 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002665{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002666 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2667 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002669 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002671 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2673 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002674 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002675}
Johan Pascalb62bb512015-12-03 21:56:45 +01002676#endif /* MBEDTLS_SSL_DTLS_SRTP */
2677
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302678#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002679void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002680{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002681 conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker490ecc82011-10-06 13:04:09 +00002682}
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002685{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01002686 conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
Paul Bakker1d29fb52012-09-28 13:28:45 +00002687}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302688#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00002689
Janos Follath088ce432017-04-10 12:42:31 +01002690#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002691void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
2692 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01002693{
2694 conf->cert_req_ca_list = cert_req_ca_list;
2695}
2696#endif
2697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002699void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002700{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002701 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002702}
2703#endif
2704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01002706void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002707{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002708 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002709}
2710#endif
2711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002713int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002714{
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
2716 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
2717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002718 }
2719
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01002720 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002724#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00002727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002728 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00002729}
2730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002732void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002734 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002735}
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002739 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02002740}
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
2743 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002744{
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01002746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002747#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002750#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002751void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002752{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01002753 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002754}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002755#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02002756
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002757#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002758
Jerry Yud0766ec2022-09-22 10:46:57 +08002759#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002760void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
2761 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002762{
Jerry Yud0766ec2022-09-22 10:46:57 +08002763 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08002764}
2765#endif
2766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
2768 mbedtls_ssl_ticket_write_t *f_ticket_write,
2769 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
2770 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02002771{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02002772 conf->f_ticket_write = f_ticket_write;
2773 conf->f_ticket_parse = f_ticket_parse;
2774 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02002775}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02002776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
2780 mbedtls_ssl_export_keys_t *f_export_keys,
2781 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002782{
Hanno Becker7e6c1782021-06-08 09:24:55 +01002783 ssl->f_export_keys = f_export_keys;
2784 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03002785}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01002786
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002787#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002788void mbedtls_ssl_conf_async_private_cb(
2789 mbedtls_ssl_config *conf,
2790 mbedtls_ssl_async_sign_t *f_async_sign,
2791 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
2792 mbedtls_ssl_async_resume_t *f_async_resume,
2793 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002795{
2796 conf->f_async_sign_start = f_async_sign;
2797 conf->f_async_decrypt_start = f_async_decrypt;
2798 conf->f_async_resume = f_async_resume;
2799 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002800 conf->p_async_config_data = async_config_data;
2801}
2802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02002804{
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02002806}
2807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002809{
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (ssl->handshake == NULL) {
2811 return NULL;
2812 } else {
2813 return ssl->handshake->user_async_ctx;
2814 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002815}
2816
Gilles Peskine449bd832023-01-11 14:50:10 +01002817void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
2818 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002819{
Gilles Peskine449bd832023-01-11 14:50:10 +01002820 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02002821 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002823}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02002824#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01002825
Paul Bakker5121ce52009-01-03 21:22:43 +00002826/*
2827 * SSL get accessors
2828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002830{
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (ssl->session != NULL) {
2832 return ssl->session->verify_result;
2833 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (ssl->session_negotiate != NULL) {
2836 return ssl->session_negotiate->verify_result;
2837 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002840}
2841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05002843{
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if (ssl == NULL || ssl->session == NULL) {
2845 return 0;
2846 }
Glenn Strauss8f526902022-01-13 00:04:49 -05002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05002849}
2850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00002852{
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (ssl == NULL || ssl->session == NULL) {
2854 return NULL;
2855 }
Paul Bakker926c8e42013-03-06 10:23:34 +01002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00002858}
2859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00002861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2864 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002865 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002867 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01002869 }
2870 }
2871#endif
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002874 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04002876 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00002878 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00002880 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00002881}
2882
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00002883#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
2884
2885size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
2886{
2887 const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2888 size_t record_size_limit = max_len;
2889
2890 if (ssl->session != NULL &&
2891 ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
2892 ssl->session->record_size_limit < max_len) {
2893 record_size_limit = ssl->session->record_size_limit;
2894 }
2895
2896 // TODO: this is currently untested
2897 /* During a handshake, use the value being negotiated */
2898 if (ssl->session_negotiate != NULL &&
2899 ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
2900 ssl->session_negotiate->record_size_limit < max_len) {
2901 record_size_limit = ssl->session_negotiate->record_size_limit;
2902 }
2903
2904 return record_size_limit;
2905}
2906#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
2907
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002908#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002909size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002910{
David Horstmann95d516f2021-05-04 18:36:56 +01002911 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002912 size_t read_mfl;
2913
Jerry Yuddda0502022-12-01 19:43:12 +08002914#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002915 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2917 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
2918 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002919 }
Jerry Yuddda0502022-12-01 19:43:12 +08002920#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002921
2922 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 if (ssl->session_out != NULL) {
2924 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
2925 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002926 max_len = read_mfl;
2927 }
2928 }
2929
Jerry Yuddda0502022-12-01 19:43:12 +08002930 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 if (ssl->session_negotiate != NULL) {
2932 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
2933 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002934 max_len = read_mfl;
2935 }
2936 }
2937
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04002939}
2940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002942{
2943 size_t max_len;
2944
2945 /*
2946 * Assume mfl_code is correct since it was checked when set
2947 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002949
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002950 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 if (ssl->session_out != NULL &&
2952 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
2953 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002954 }
2955
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002956 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (ssl->session_negotiate != NULL &&
2958 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
2959 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02002960 }
2961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02002963}
2964#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2965
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002967size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002968{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002969 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2971 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
2972 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
2973 return 0;
2974 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
2977 return ssl->mtu;
2978 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 if (ssl->mtu == 0) {
2981 return ssl->handshake->mtu;
2982 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002983
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 return ssl->mtu < ssl->handshake->mtu ?
2985 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02002986}
2987#endif /* MBEDTLS_SSL_PROTO_DTLS */
2988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002990{
2991 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
2992
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002993#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Jan Brucknerf482dcc2023-03-15 09:09:06 +01002994 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02002995 !defined(MBEDTLS_SSL_PROTO_DTLS)
2996 (void) ssl;
2997#endif
2998
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02002999#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003003 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003005#endif
3006
Jan Brucknerf482dcc2023-03-15 09:09:06 +01003007#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3008 const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
3009
3010 if (max_len > record_size_limit) {
3011 max_len = record_size_limit;
3012 }
3013#endif
3014
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003015 if (ssl->transform_out != NULL &&
3016 ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Waleed Elmelegyf5017902024-01-09 14:18:34 +00003017 /*
3018 * In TLS 1.3 case, when records are protected, `max_len` as computed
3019 * above is the maximum length of the TLSInnerPlaintext structure that
3020 * along the plaintext payload contains the inner content type (one byte)
3021 * and some zero padding. Given the algorithm used for padding
3022 * in mbedtls_ssl_encrypt_buf(), compute the maximum length for
3023 * the plaintext payload. Round down to a multiple of
3024 * MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
3025 * subtract 1.
Waleed Elmelegy6a971fd2023-12-28 17:48:16 +00003026 */
3027 max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
3028 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
3029 }
3030
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3033 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3034 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003035 const size_t overhead = (size_t) ret;
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 if (ret < 0) {
3038 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003039 }
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (mtu <= overhead) {
3042 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3043 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3044 }
3045
3046 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003047 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003049 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003050#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003051
Hanno Becker0defedb2018-08-10 12:35:02 +01003052#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
Waleed Elmelegy049cd302023-12-20 17:28:31 +00003053 !defined(MBEDTLS_SSL_PROTO_DTLS) && \
3054 !defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Hanno Becker0defedb2018-08-10 12:35:02 +01003055 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003056#endif
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003059}
3060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003062{
3063 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3064
3065#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3066 (void) ssl;
3067#endif
3068
3069#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003071
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003073 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003075#endif
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003078}
3079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003081const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003082{
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (ssl == NULL || ssl->session == NULL) {
3084 return NULL;
3085 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003086
Hanno Beckere6824572019-02-07 13:18:46 +00003087#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003089#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003091#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003096int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3097 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003098{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003099 int ret;
3100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003102 dst == NULL ||
3103 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3105 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003106 }
3107
Hanno Beckere810bbc2021-05-14 16:01:05 +01003108 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3109 * idempotent: Each session can only be exported once.
3110 *
3111 * (This is in preparation for TLS 1.3 support where we will
3112 * need the ability to export multiple sessions (aka tickets),
3113 * which will be achieved by calling mbedtls_ssl_get_session()
3114 * multiple times until it fails.)
3115 *
3116 * Check whether we have already exported the current session,
3117 * and fail if so.
3118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (ssl->session->exported == 1) {
3120 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3121 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3124 if (ret != 0) {
3125 return ret;
3126 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003127
3128 /* Remember that we've exported the session. */
3129 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003133
David Horstmanncb01b362024-02-29 17:31:46 +00003134#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3135
3136/* Serialization of TLS 1.2 sessions
David Horstmanne59f9702024-02-29 16:08:57 +00003137 *
David Horstmanncb01b362024-02-29 17:31:46 +00003138 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003139 */
3140static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
3141 unsigned char *buf,
3142 size_t buf_len)
3143{
3144 unsigned char *p = buf;
3145 size_t used = 0;
3146
3147#if defined(MBEDTLS_HAVE_TIME)
3148 uint64_t start;
3149#endif
3150#if defined(MBEDTLS_X509_CRT_PARSE_C)
3151#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3152 size_t cert_len;
3153#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3154#endif /* MBEDTLS_X509_CRT_PARSE_C */
3155
3156 /*
3157 * Time
3158 */
3159#if defined(MBEDTLS_HAVE_TIME)
3160 used += 8;
3161
3162 if (used <= buf_len) {
3163 start = (uint64_t) session->start;
3164
3165 MBEDTLS_PUT_UINT64_BE(start, p, 0);
3166 p += 8;
3167 }
3168#endif /* MBEDTLS_HAVE_TIME */
3169
3170 /*
3171 * Basic mandatory fields
3172 */
3173 used += 1 /* id_len */
3174 + sizeof(session->id)
3175 + sizeof(session->master)
3176 + 4; /* verify_result */
3177
3178 if (used <= buf_len) {
3179 *p++ = MBEDTLS_BYTE_0(session->id_len);
3180 memcpy(p, session->id, 32);
3181 p += 32;
3182
3183 memcpy(p, session->master, 48);
3184 p += 48;
3185
3186 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
3187 p += 4;
3188 }
3189
3190 /*
3191 * Peer's end-entity certificate
3192 */
3193#if defined(MBEDTLS_X509_CRT_PARSE_C)
3194#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3195 if (session->peer_cert == NULL) {
3196 cert_len = 0;
3197 } else {
3198 cert_len = session->peer_cert->raw.len;
3199 }
3200
3201 used += 3 + cert_len;
3202
3203 if (used <= buf_len) {
3204 *p++ = MBEDTLS_BYTE_2(cert_len);
3205 *p++ = MBEDTLS_BYTE_1(cert_len);
3206 *p++ = MBEDTLS_BYTE_0(cert_len);
3207
3208 if (session->peer_cert != NULL) {
3209 memcpy(p, session->peer_cert->raw.p, cert_len);
3210 p += cert_len;
3211 }
3212 }
3213#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3214 if (session->peer_cert_digest != NULL) {
3215 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
3216 if (used <= buf_len) {
3217 *p++ = (unsigned char) session->peer_cert_digest_type;
3218 *p++ = (unsigned char) session->peer_cert_digest_len;
3219 memcpy(p, session->peer_cert_digest,
3220 session->peer_cert_digest_len);
3221 p += session->peer_cert_digest_len;
3222 }
3223 } else {
3224 used += 2;
3225 if (used <= buf_len) {
3226 *p++ = (unsigned char) MBEDTLS_MD_NONE;
3227 *p++ = 0;
3228 }
3229 }
3230#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3231#endif /* MBEDTLS_X509_CRT_PARSE_C */
3232
3233 /*
3234 * Session ticket if any, plus associated data
3235 */
3236#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3237#if defined(MBEDTLS_SSL_CLI_C)
3238 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3239 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
3240
3241 if (used <= buf_len) {
3242 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
3243 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
3244 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
3245
3246 if (session->ticket != NULL) {
3247 memcpy(p, session->ticket, session->ticket_len);
3248 p += session->ticket_len;
3249 }
3250
3251 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3252 p += 4;
3253 }
3254 }
3255#endif /* MBEDTLS_SSL_CLI_C */
3256#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3257 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3258 used += 8;
3259
3260 if (used <= buf_len) {
3261 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3262 p += 8;
3263 }
3264 }
3265#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3266#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3267
3268 /*
3269 * Misc extension-related info
3270 */
3271#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3272 used += 1;
3273
3274 if (used <= buf_len) {
3275 *p++ = session->mfl_code;
3276 }
3277#endif
3278
3279#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3280 used += 1;
3281
3282 if (used <= buf_len) {
3283 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
3284 }
3285#endif
3286
3287 return used;
3288}
3289
3290MBEDTLS_CHECK_RETURN_CRITICAL
3291static int ssl_tls12_session_load(mbedtls_ssl_session *session,
3292 const unsigned char *buf,
3293 size_t len)
3294{
3295#if defined(MBEDTLS_HAVE_TIME)
3296 uint64_t start;
3297#endif
3298#if defined(MBEDTLS_X509_CRT_PARSE_C)
3299#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3300 size_t cert_len;
3301#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3302#endif /* MBEDTLS_X509_CRT_PARSE_C */
3303
3304 const unsigned char *p = buf;
3305 const unsigned char * const end = buf + len;
3306
3307 /*
3308 * Time
3309 */
3310#if defined(MBEDTLS_HAVE_TIME)
3311 if (8 > (size_t) (end - p)) {
3312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3313 }
3314
3315 start = MBEDTLS_GET_UINT64_BE(p, 0);
3316 p += 8;
3317
3318 session->start = (time_t) start;
3319#endif /* MBEDTLS_HAVE_TIME */
3320
3321 /*
3322 * Basic mandatory fields
3323 */
3324 if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
3325 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3326 }
3327
3328 session->id_len = *p++;
3329 memcpy(session->id, p, 32);
3330 p += 32;
3331
3332 memcpy(session->master, p, 48);
3333 p += 48;
3334
3335 session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
3336 p += 4;
3337
3338 /* Immediately clear invalid pointer values that have been read, in case
3339 * we exit early before we replaced them with valid ones. */
3340#if defined(MBEDTLS_X509_CRT_PARSE_C)
3341#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3342 session->peer_cert = NULL;
3343#else
3344 session->peer_cert_digest = NULL;
3345#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3346#endif /* MBEDTLS_X509_CRT_PARSE_C */
3347#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3348 session->ticket = NULL;
3349#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
3350
3351 /*
3352 * Peer certificate
3353 */
3354#if defined(MBEDTLS_X509_CRT_PARSE_C)
3355#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
3356 /* Deserialize CRT from the end of the ticket. */
3357 if (3 > (size_t) (end - p)) {
3358 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3359 }
3360
3361 cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
3362 p += 3;
3363
3364 if (cert_len != 0) {
3365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3366
3367 if (cert_len > (size_t) (end - p)) {
3368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3369 }
3370
3371 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
3372
3373 if (session->peer_cert == NULL) {
3374 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3375 }
3376
3377 mbedtls_x509_crt_init(session->peer_cert);
3378
3379 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
3380 p, cert_len)) != 0) {
3381 mbedtls_x509_crt_free(session->peer_cert);
3382 mbedtls_free(session->peer_cert);
3383 session->peer_cert = NULL;
3384 return ret;
3385 }
3386
3387 p += cert_len;
3388 }
3389#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3390 /* Deserialize CRT digest from the end of the ticket. */
3391 if (2 > (size_t) (end - p)) {
3392 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3393 }
3394
3395 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
3396 session->peer_cert_digest_len = (size_t) *p++;
3397
3398 if (session->peer_cert_digest_len != 0) {
3399 const mbedtls_md_info_t *md_info =
3400 mbedtls_md_info_from_type(session->peer_cert_digest_type);
3401 if (md_info == NULL) {
3402 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3403 }
3404 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
3405 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3406 }
3407
3408 if (session->peer_cert_digest_len > (size_t) (end - p)) {
3409 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3410 }
3411
3412 session->peer_cert_digest =
3413 mbedtls_calloc(1, session->peer_cert_digest_len);
3414 if (session->peer_cert_digest == NULL) {
3415 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3416 }
3417
3418 memcpy(session->peer_cert_digest, p,
3419 session->peer_cert_digest_len);
3420 p += session->peer_cert_digest_len;
3421 }
3422#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3423#endif /* MBEDTLS_X509_CRT_PARSE_C */
3424
3425 /*
3426 * Session ticket and associated data
3427 */
3428#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3429#if defined(MBEDTLS_SSL_CLI_C)
3430 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3431 if (3 > (size_t) (end - p)) {
3432 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3433 }
3434
3435 session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
3436 p += 3;
3437
3438 if (session->ticket_len != 0) {
3439 if (session->ticket_len > (size_t) (end - p)) {
3440 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3441 }
3442
3443 session->ticket = mbedtls_calloc(1, session->ticket_len);
3444 if (session->ticket == NULL) {
3445 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3446 }
3447
3448 memcpy(session->ticket, p, session->ticket_len);
3449 p += session->ticket_len;
3450 }
3451
3452 if (4 > (size_t) (end - p)) {
3453 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3454 }
3455
3456 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3457 p += 4;
3458 }
3459#endif /* MBEDTLS_SSL_CLI_C */
3460#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
3461 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3462 if (8 > (size_t) (end - p)) {
3463 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3464 }
3465 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3466 p += 8;
3467 }
3468#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
3469#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3470
3471 /*
3472 * Misc extension-related info
3473 */
3474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3475 if (1 > (size_t) (end - p)) {
3476 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3477 }
3478
3479 session->mfl_code = *p++;
3480#endif
3481
3482#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3483 if (1 > (size_t) (end - p)) {
3484 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3485 }
3486
3487 session->encrypt_then_mac = *p++;
3488#endif
3489
3490 /* Done, should have consumed entire buffer */
3491 if (p != end) {
3492 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3493 }
3494
3495 return 0;
3496}
3497
3498#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3499
3500#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3501/* Serialization of TLS 1.3 sessions:
3502 *
David Horstmanncb01b362024-02-29 17:31:46 +00003503 * For more detail, see the description of ssl_session_save().
David Horstmanne59f9702024-02-29 16:08:57 +00003504 */
3505#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3506MBEDTLS_CHECK_RETURN_CRITICAL
3507static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3508 unsigned char *buf,
3509 size_t buf_len,
3510 size_t *olen)
3511{
3512 unsigned char *p = buf;
3513#if defined(MBEDTLS_SSL_CLI_C) && \
3514 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3515 size_t hostname_len = (session->hostname == NULL) ?
3516 0 : strlen(session->hostname) + 1;
3517#endif
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003518
3519#if defined(MBEDTLS_SSL_SRV_C) && \
3520 defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003521 const size_t alpn_len = (session->ticket_alpn == NULL) ?
Waleed Elmelegyb28ab0a2024-03-13 16:48:28 +00003522 0 : strlen(session->ticket_alpn) + 1;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003523#endif
David Horstmanne59f9702024-02-29 16:08:57 +00003524 size_t needed = 4 /* ticket_age_add */
3525 + 1 /* ticket_flags */
3526 + 1; /* resumption_key length */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003527
David Horstmanne59f9702024-02-29 16:08:57 +00003528 *olen = 0;
3529
3530 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
3531 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3532 }
3533 needed += session->resumption_key_len; /* resumption_key */
3534
3535#if defined(MBEDTLS_SSL_EARLY_DATA)
3536 needed += 4; /* max_early_data_size */
3537#endif
3538#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3539 needed += 2; /* record_size_limit */
3540#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3541
3542#if defined(MBEDTLS_HAVE_TIME)
3543 needed += 8; /* ticket_creation_time or ticket_reception_time */
3544#endif
3545
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003546#if defined(MBEDTLS_SSL_SRV_C)
3547 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
3548#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003549 needed += 2 /* alpn_len */
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003550 + alpn_len; /* alpn */
3551#endif
3552 }
3553#endif /* MBEDTLS_SSL_SRV_C */
3554
David Horstmanne59f9702024-02-29 16:08:57 +00003555#if defined(MBEDTLS_SSL_CLI_C)
3556 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3557#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3558 needed += 2 /* hostname_len */
3559 + hostname_len; /* hostname */
3560#endif
3561
3562 needed += 4 /* ticket_lifetime */
3563 + 2; /* ticket_len */
3564
3565 /* Check size_t overflow */
3566 if (session->ticket_len > SIZE_MAX - needed) {
3567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3568 }
3569
3570 needed += session->ticket_len; /* ticket */
3571 }
3572#endif /* MBEDTLS_SSL_CLI_C */
3573
3574 *olen = needed;
3575 if (needed > buf_len) {
3576 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3577 }
3578
3579 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
3580 p[4] = session->ticket_flags;
3581
3582 /* save resumption_key */
3583 p[5] = session->resumption_key_len;
3584 p += 6;
3585 memcpy(p, session->resumption_key, session->resumption_key_len);
3586 p += session->resumption_key_len;
3587
3588#if defined(MBEDTLS_SSL_EARLY_DATA)
3589 MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
3590 p += 4;
3591#endif
3592#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3593 MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
3594 p += 2;
3595#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3596
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003597#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003598 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003599#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003600 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
3601 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003602#endif /* MBEDTLS_HAVE_TIME */
3603
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003604#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003605 MBEDTLS_PUT_UINT16_BE(alpn_len, p, 0);
3606 p += 2;
3607
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003608 if (alpn_len > 0) {
3609 /* save chosen alpn */
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003610 memcpy(p, session->ticket_alpn, alpn_len);
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003611 p += alpn_len;
3612 }
3613#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3614 }
3615#endif /* MBEDTLS_SSL_SRV_C */
3616
David Horstmanne59f9702024-02-29 16:08:57 +00003617#if defined(MBEDTLS_SSL_CLI_C)
3618 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3619#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3620 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
3621 p += 2;
3622 if (hostname_len > 0) {
3623 /* save host name */
3624 memcpy(p, session->hostname, hostname_len);
3625 p += hostname_len;
3626 }
3627#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3628
3629#if defined(MBEDTLS_HAVE_TIME)
3630 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
3631 p += 8;
3632#endif
3633 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
3634 p += 4;
3635
3636 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
3637 p += 2;
3638
3639 if (session->ticket != NULL && session->ticket_len > 0) {
3640 memcpy(p, session->ticket, session->ticket_len);
3641 p += session->ticket_len;
3642 }
3643 }
3644#endif /* MBEDTLS_SSL_CLI_C */
3645 return 0;
3646}
3647
3648MBEDTLS_CHECK_RETURN_CRITICAL
3649static int ssl_tls13_session_load(mbedtls_ssl_session *session,
3650 const unsigned char *buf,
3651 size_t len)
3652{
3653 const unsigned char *p = buf;
3654 const unsigned char *end = buf + len;
3655
3656 if (end - p < 6) {
3657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3658 }
3659 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
3660 session->ticket_flags = p[4];
3661
3662 /* load resumption_key */
3663 session->resumption_key_len = p[5];
3664 p += 6;
3665
3666 if (end - p < session->resumption_key_len) {
3667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3668 }
3669
3670 if (sizeof(session->resumption_key) < session->resumption_key_len) {
3671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3672 }
3673 memcpy(session->resumption_key, p, session->resumption_key_len);
3674 p += session->resumption_key_len;
3675
3676#if defined(MBEDTLS_SSL_EARLY_DATA)
3677 if (end - p < 4) {
3678 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3679 }
3680 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
3681 p += 4;
3682#endif
3683#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3684 if (end - p < 2) {
3685 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3686 }
3687 session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
3688 p += 2;
3689#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3690
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003691#if defined(MBEDTLS_SSL_SRV_C)
David Horstmanne59f9702024-02-29 16:08:57 +00003692 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003693#if defined(MBEDTLS_HAVE_TIME)
David Horstmanne59f9702024-02-29 16:08:57 +00003694 if (end - p < 8) {
3695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3696 }
3697 session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
3698 p += 8;
David Horstmanne59f9702024-02-29 16:08:57 +00003699#endif /* MBEDTLS_HAVE_TIME */
3700
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003701#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003702 size_t alpn_len;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003703
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003704 if (end - p < 2) {
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003705 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3706 }
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00003707
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003708 alpn_len = MBEDTLS_GET_UINT16_BE(p, 0);
3709 p += 2;
3710
3711 if (end - p < (long int) alpn_len) {
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003712 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3713 }
3714
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003715 if (alpn_len > 0) {
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003716 int ret = mbedtls_ssl_session_set_ticket_alpn(session, (char *) p);
3717 if (ret != 0) {
3718 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003719 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003720 p += alpn_len;
3721 }
3722#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
3723 }
3724#endif /* MBEDTLS_SSL_SRV_C */
3725
David Horstmanne59f9702024-02-29 16:08:57 +00003726#if defined(MBEDTLS_SSL_CLI_C)
3727 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3728#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3729 size_t hostname_len;
3730 /* load host name */
3731 if (end - p < 2) {
3732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3733 }
3734 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
3735 p += 2;
3736
3737 if (end - p < (long int) hostname_len) {
3738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3739 }
3740 if (hostname_len > 0) {
3741 session->hostname = mbedtls_calloc(1, hostname_len);
3742 if (session->hostname == NULL) {
3743 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3744 }
3745 memcpy(session->hostname, p, hostname_len);
3746 p += hostname_len;
3747 }
3748#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3749
3750#if defined(MBEDTLS_HAVE_TIME)
3751 if (end - p < 8) {
3752 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3753 }
3754 session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
3755 p += 8;
3756#endif
3757 if (end - p < 4) {
3758 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3759 }
3760 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
3761 p += 4;
3762
3763 if (end - p < 2) {
3764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3765 }
3766 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
3767 p += 2;
3768
3769 if (end - p < (long int) session->ticket_len) {
3770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3771 }
3772 if (session->ticket_len > 0) {
3773 session->ticket = mbedtls_calloc(1, session->ticket_len);
3774 if (session->ticket == NULL) {
3775 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
3776 }
3777 memcpy(session->ticket, p, session->ticket_len);
3778 p += session->ticket_len;
3779 }
3780 }
3781#endif /* MBEDTLS_SSL_CLI_C */
3782
3783 return 0;
3784
3785}
3786#else /* MBEDTLS_SSL_SESSION_TICKETS */
3787MBEDTLS_CHECK_RETURN_CRITICAL
3788static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
3789 unsigned char *buf,
3790 size_t buf_len,
3791 size_t *olen)
3792{
3793 ((void) session);
3794 ((void) buf);
3795 ((void) buf_len);
3796 *olen = 0;
3797 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3798}
3799
3800static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
Norbert Fabritiusd36913a2023-01-24 17:48:29 +01003801 const unsigned char *buf,
David Horstmanne59f9702024-02-29 16:08:57 +00003802 size_t buf_len)
3803{
3804 ((void) session);
3805 ((void) buf);
3806 ((void) buf_len);
3807 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3808}
3809#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
3810#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3811
Paul Bakker5121ce52009-01-03 21:22:43 +00003812/*
Hanno Beckera835da52019-05-16 12:39:07 +01003813 * Define ticket header determining Mbed TLS version
3814 * and structure of the ticket.
3815 */
3816
Hanno Becker94ef3b32019-05-16 12:50:45 +01003817/*
Hanno Becker50b59662019-05-28 14:30:45 +01003818 * Define bitflag determining compile-time settings influencing
3819 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003820 */
3821
Hanno Becker50b59662019-05-28 14:30:45 +01003822#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003823#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003824#else
Hanno Becker3e088662019-05-29 11:10:18 +01003825#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003826#endif /* MBEDTLS_HAVE_TIME */
3827
3828#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003829#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003830#else
Hanno Becker3e088662019-05-29 11:10:18 +01003831#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003832#endif /* MBEDTLS_X509_CRT_PARSE_C */
3833
David Horstmann5c5a32f2024-02-13 17:53:35 +00003834#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmannf686f1d2024-03-01 11:20:32 +00003835#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmann5c5a32f2024-02-13 17:53:35 +00003836#else
David Horstmannf686f1d2024-03-01 11:20:32 +00003837#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmann5c5a32f2024-02-13 17:53:35 +00003838#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3839
Hanno Becker94ef3b32019-05-16 12:50:45 +01003840#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003841#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003842#else
Hanno Becker3e088662019-05-29 11:10:18 +01003843#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003844#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3845
3846#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003847#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003848#else
Hanno Becker3e088662019-05-29 11:10:18 +01003849#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003850#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3851
Hanno Becker94ef3b32019-05-16 12:50:45 +01003852#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003853#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003854#else
Hanno Becker3e088662019-05-29 11:10:18 +01003855#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003856#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3857
Hanno Becker94ef3b32019-05-16 12:50:45 +01003858#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3859#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3860#else
3861#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3862#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3863
David Horstmann92b258b2024-02-13 17:23:34 +00003864#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3865#define SSL_SERIALIZED_SESSION_CONFIG_SNI 1
3866#else
3867#define SSL_SERIALIZED_SESSION_CONFIG_SNI 0
3868#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
3869
3870#if defined(MBEDTLS_SSL_EARLY_DATA)
3871#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 1
3872#else
3873#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA 0
3874#endif /* MBEDTLS_SSL_EARLY_DATA */
3875
3876#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3877#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 1
3878#else
3879#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE 0
3880#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
3881
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003882#if defined(MBEDTLS_SSL_ALPN) && defined(MBEDTLS_SSL_SRV_C) && \
3883 defined(MBEDTLS_SSL_EARLY_DATA)
Waleed Elmelegy11025632024-03-07 15:25:52 +00003884#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 1
3885#else
3886#define SSL_SERIALIZED_SESSION_CONFIG_ALPN 0
3887#endif /* MBEDTLS_SSL_ALPN */
3888
Hanno Becker3e088662019-05-29 11:10:18 +01003889#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3890#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3891#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3892#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003893#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3894#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
David Horstmannf686f1d2024-03-01 11:20:32 +00003895#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 6
David Horstmann92b258b2024-02-13 17:23:34 +00003896#define SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT 7
3897#define SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT 8
3898#define SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT 9
Waleed Elmelegy11025632024-03-07 15:25:52 +00003899#define SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT 10
Hanno Becker3e088662019-05-29 11:10:18 +01003900
Hanno Becker50b59662019-05-28 14:30:45 +01003901#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 ((uint16_t) ( \
3903 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3904 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3905 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3906 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3907 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3908 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmann5c5a32f2024-02-13 17:53:35 +00003909 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmann71fa1a92024-03-01 12:32:18 +00003910 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
3911 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT) | \
David Horstmann92b258b2024-02-13 17:23:34 +00003912 (SSL_SERIALIZED_SESSION_CONFIG_SNI << SSL_SERIALIZED_SESSION_CONFIG_SNI_BIT) | \
3913 (SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA << \
3914 SSL_SERIALIZED_SESSION_CONFIG_EARLY_DATA_BIT) | \
3915 (SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00003916 SSL_SERIALIZED_SESSION_CONFIG_RECORD_SIZE_BIT) | \
Waleed Elmelegy75e33fa2024-03-07 16:57:58 +00003917 (SSL_SERIALIZED_SESSION_CONFIG_ALPN << \
Waleed Elmelegy11025632024-03-07 15:25:52 +00003918 SSL_SERIALIZED_SESSION_CONFIG_ALPN_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003919
Chien Wong4e9683e2023-12-28 17:07:43 +08003920static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003921 MBEDTLS_VERSION_MAJOR,
3922 MBEDTLS_VERSION_MINOR,
3923 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3925 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003926};
Hanno Beckera835da52019-05-16 12:39:07 +01003927
3928/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003929 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003930 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003931 *
David Horstmanncb01b362024-02-29 17:31:46 +00003932 * TLS 1.2 session:
3933 *
3934 * struct {
3935 * #if defined(MBEDTLS_SSL_SESSION_TICKETS)
3936 * opaque ticket<0..2^24-1>; // length 0 means no ticket
3937 * uint32 ticket_lifetime;
3938 * #endif
3939 * } ClientOnlyData;
3940 *
3941 * struct {
3942 * #if defined(MBEDTLS_HAVE_TIME)
3943 * uint64 start_time;
3944 * #endif
3945 * uint8 session_id_len; // at most 32
3946 * opaque session_id[32];
3947 * opaque master[48]; // fixed length in the standard
3948 * uint32 verify_result;
3949 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
3950 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
3951 * #else
David Horstmann76ba26a2024-03-01 12:03:35 +00003952 * uint8 peer_cert_digest_type;
David Horstmanncb01b362024-02-29 17:31:46 +00003953 * opaque peer_cert_digest<0..2^8-1>
3954 * #endif
3955 * select (endpoint) {
3956 * case client: ClientOnlyData;
3957 * case server: uint64 ticket_creation_time;
3958 * };
3959 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3960 * uint8 mfl_code; // up to 255 according to standard
3961 * #endif
3962 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3963 * uint8 encrypt_then_mac; // 0 or 1
3964 * #endif
3965 * } serialized_session_tls12;
3966 *
3967 *
3968 * TLS 1.3 Session:
3969 *
3970 * struct {
3971 * #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3972 * opaque hostname<0..2^16-1>;
3973 * #endif
3974 * #if defined(MBEDTLS_HAVE_TIME)
3975 * uint64 ticket_reception_time;
3976 * #endif
3977 * uint32 ticket_lifetime;
3978 * opaque ticket<1..2^16-1>;
3979 * } ClientOnlyData;
3980 *
3981 * struct {
3982 * uint32 ticket_age_add;
3983 * uint8 ticket_flags;
3984 * opaque resumption_key<0..255>;
3985 * #if defined(MBEDTLS_SSL_EARLY_DATA)
3986 * uint32 max_early_data_size;
3987 * #endif
3988 * #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
3989 * uint16 record_size_limit;
3990 * #endif
3991 * select ( endpoint ) {
3992 * case client: ClientOnlyData;
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003993 * case server:
David Horstmanncb01b362024-02-29 17:31:46 +00003994 * #if defined(MBEDTLS_HAVE_TIME)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00003995 * uint64 ticket_creation_time;
David Horstmanncb01b362024-02-29 17:31:46 +00003996 * #endif
Waleed Elmelegyfe9ae082024-03-07 15:47:31 +00003997 * #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegydaa4da72024-03-13 16:25:34 +00003998 * opaque ticket_alpn<0..256>;
David Horstmanncb01b362024-02-29 17:31:46 +00003999 * #endif
4000 * };
4001 * } serialized_session_tls13;
4002 *
4003 *
4004 * SSL session:
4005 *
4006 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004007 *
Hanno Beckerdce50972021-08-01 05:39:23 +01004008 * opaque mbedtls_version[3]; // library version: major, minor, patch
4009 * opaque session_format[2]; // library-version specific 16-bit field
4010 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004011 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01004012 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004013 * Note: When updating the format, remember to keep
4014 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004015 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004016 * // In this version, `session_format` determines
4017 * // the setting of those compile-time
4018 * // configuration options which influence
4019 * // the structure of mbedtls_ssl_session.
4020 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004021 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08004022 * // - TLS 1.2 (0x0303)
4023 * // - TLS 1.3 (0x0304)
David Horstmann531aca22024-02-29 18:14:28 +00004024 * uint8_t endpoint;
4025 * uint16_t ciphersuite;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004026 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004027 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004028 *
Glenn Straussda7851c2022-03-14 13:29:48 -04004029 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004030 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08004031 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08004032 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004033 *
4034 * };
4035 *
4036 * } serialized_session;
4037 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004038 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004039
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004041static int ssl_session_save(const mbedtls_ssl_session *session,
4042 unsigned char omit_header,
4043 unsigned char *buf,
4044 size_t buf_len,
4045 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004046{
4047 unsigned char *p = buf;
4048 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08004049 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08004050#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4051 size_t out_len;
4052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4053#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (session == NULL) {
4055 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4056 }
Jerry Yu438ddd82022-07-07 06:55:50 +00004057
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004059 /*
4060 * Add Mbed TLS version identifier
4061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004063
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 if (used <= buf_len) {
4065 memcpy(p, ssl_serialized_session_header,
4066 sizeof(ssl_serialized_session_header));
4067 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004068 }
4069 }
4070
4071 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004072 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004073 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004074 used += 1 /* TLS version */
4075 + 1 /* endpoint */
4076 + 2; /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (used <= buf_len) {
4078 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004079 *p++ = session->endpoint;
4080 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
4081 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004082 }
4083
4084 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08004085 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004087#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 case MBEDTLS_SSL_VERSION_TLS1_2:
4089 used += ssl_tls12_session_save(session, p, remaining_len);
4090 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4092
Jerry Yu251a12e2022-07-13 15:15:48 +08004093#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 case MBEDTLS_SSL_VERSION_TLS1_3:
4095 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
4096 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4097 return ret;
4098 }
4099 used += out_len;
4100 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08004101#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4102
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 default:
4104 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004105 }
4106
4107 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (used > buf_len) {
4109 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4110 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004111
Gilles Peskine449bd832023-01-11 14:50:10 +01004112 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004113}
4114
4115/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004116 * Public wrapper for ssl_session_save()
4117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004118int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
4119 unsigned char *buf,
4120 size_t buf_len,
4121 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004122{
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004124}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004125
Jerry Yuf1b23ca2022-02-18 11:48:47 +08004126/*
4127 * Deserialize session, see mbedtls_ssl_session_save() for format.
4128 *
4129 * This internal version is wrapped by a public function that cleans up in
4130 * case of error, and has an extra option omit_header.
4131 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004132MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004133static int ssl_session_load(mbedtls_ssl_session *session,
4134 unsigned char omit_header,
4135 const unsigned char *buf,
4136 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004137{
4138 const unsigned char *p = buf;
4139 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00004140 size_t remaining_len;
4141
4142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 if (session == NULL) {
4144 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
4145 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004146
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004148 /*
4149 * Check Mbed TLS version identifier
4150 */
4151
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
4153 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004155
4156 if (memcmp(p, ssl_serialized_session_header,
4157 sizeof(ssl_serialized_session_header)) != 0) {
4158 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4159 }
4160 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004161 }
4162
4163 /*
Ronald Cron40a4ab02024-01-15 10:21:30 +01004164 * TLS version identifier, endpoint, ciphersuite
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004165 */
Ronald Cron40a4ab02024-01-15 10:21:30 +01004166 if (4 > (size_t) (end - p)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4168 }
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004169 session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
Ronald Cron40a4ab02024-01-15 10:21:30 +01004170 session->endpoint = *p++;
4171 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
4172 p += 2;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004173
4174 /* Dispatch according to TLS version. */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00004175 remaining_len = (size_t) (end - p);
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 case MBEDTLS_SSL_VERSION_TLS1_2:
4179 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004180#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4181
Jerry Yu438ddd82022-07-07 06:55:50 +00004182#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 case MBEDTLS_SSL_VERSION_TLS1_3:
4184 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00004185#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
4186
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 default:
4188 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004189 }
4190}
4191
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02004192/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004193 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004195int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
4196 const unsigned char *buf,
4197 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004198{
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004200
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 if (ret != 0) {
4202 mbedtls_ssl_session_free(session);
4203 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004204
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004206}
4207
4208/*
Paul Bakker1961b702013-01-25 14:49:24 +01004209 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004210 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004211MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004212static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01004213{
4214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
4215
Ronald Cron66dbf912022-02-02 15:33:46 +01004216 /*
4217 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01004218 * that were written into the output buffer by the previous handshake step,
4219 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01004220 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
4221 * We proceed to the next handshake step only when all data from the
4222 * previous one have been sent to the peer, thus we make sure that this is
4223 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
4224 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
4225 * we have to wait before to go ahead.
4226 * In the case of TLS 1.3, handshake step handlers do not send data to the
4227 * peer. Data are only sent here and through
4228 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04004229 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01004230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
4232 return ret;
4233 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004234
4235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4237 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
4238 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
4239 return ret;
4240 }
Hanno Becker41934dd2021-08-07 19:13:43 +01004241 }
4242#endif /* MBEDTLS_SSL_PROTO_DTLS */
4243
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01004245}
4246
Gilles Peskine449bd832023-01-11 14:50:10 +01004247int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004248{
Hanno Becker41934dd2021-08-07 19:13:43 +01004249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004250
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01004252 ssl->conf == NULL ||
4253 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01004254 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4255 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01004256 }
4257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 ret = ssl_prepare_handshake_step(ssl);
4259 if (ret != 0) {
4260 return ret;
4261 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004262
Gilles Peskine449bd832023-01-11 14:50:10 +01004263 ret = mbedtls_ssl_handle_pending_alert(ssl);
4264 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08004265 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 }
Jerry Yue7047812021-09-13 19:26:39 +08004267
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01004268 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
4269 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
4270 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
4274 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01004275 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08004276
Gilles Peskine449bd832023-01-11 14:50:10 +01004277 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01004278 case MBEDTLS_SSL_HELLO_REQUEST:
4279 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01004280 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01004281 break;
Jerry Yub9930e72021-08-06 17:11:51 +08004282
Ronald Cron9f0fba32022-02-10 16:45:15 +01004283 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004285 break;
4286
4287 default:
4288#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4290 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
4291 } else {
4292 ret = mbedtls_ssl_handshake_client_step(ssl);
4293 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01004294#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004296#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01004298#endif
4299 }
Jerry Yub9930e72021-08-06 17:11:51 +08004300 }
Ronald Cron6291b232023-03-08 15:51:25 +01004301#endif /* MBEDTLS_SSL_CLI_C */
4302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004303#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01004305#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
4306 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01004307 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01004308 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 ret = mbedtls_ssl_handshake_server_step(ssl);
4310 }
Ronald Cron6291b232023-03-08 15:51:25 +01004311#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
4312 ret = mbedtls_ssl_handshake_server_step(ssl);
4313#else
4314 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004315#endif
Ronald Cron6291b232023-03-08 15:51:25 +01004316 }
4317#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Gilles Peskine449bd832023-01-11 14:50:10 +01004319 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08004320 /* handshake_step return error. And it is same
4321 * with alert_reason.
4322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 if (ssl->send_alert) {
4324 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08004325 goto cleanup;
4326 }
4327 }
4328
4329cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01004331}
4332
4333/*
4334 * Perform the SSL handshake
4335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004336int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01004337{
4338 int ret = 0;
4339
Hanno Beckera817ea42020-10-20 15:20:23 +01004340 /* Sanity checks */
4341
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (ssl == NULL || ssl->conf == NULL) {
4343 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4344 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004345
Hanno Beckera817ea42020-10-20 15:20:23 +01004346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4348 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
4349 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
4350 "mbedtls_ssl_set_timer_cb() for DTLS"));
4351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01004352 }
4353#endif /* MBEDTLS_SSL_PROTO_DTLS */
4354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01004356
Hanno Beckera817ea42020-10-20 15:20:23 +01004357 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
4359 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01004362 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 }
Paul Bakker1961b702013-01-25 14:49:24 +01004364 }
4365
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004369}
4370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004371#if defined(MBEDTLS_SSL_RENEGOTIATION)
4372#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004373/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004374 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004375 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004376MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004377static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004378{
Janos Follath865b3eb2019-12-16 11:46:15 +00004379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004380
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004382
4383 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004384 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4385 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004386
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4388 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4389 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004390 }
4391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004393
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004397
4398/*
4399 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400 * - any side: calling mbedtls_ssl_renegotiate(),
4401 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
4402 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004403 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004404 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004407int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004408{
Janos Follath865b3eb2019-12-16 11:46:15 +00004409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00004410
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 if ((ret = ssl_handshake_init(ssl)) != 0) {
4414 return ret;
4415 }
Paul Bakker48916f92012-09-16 19:57:18 +00004416
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004417 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4418 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4421 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4422 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004423 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004424 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004425 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004427 }
4428#endif
4429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4431 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4435 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004436 }
4437
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004441}
4442
4443/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004444 * Renegotiate current connection on client,
4445 * or request renegotiation on server
4446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004447int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004448{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (ssl == NULL || ssl->conf == NULL) {
4452 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4453 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004456 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4458 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4459 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4460 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004463
4464 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 if (ssl->out_left != 0) {
4466 return mbedtls_ssl_flush_output(ssl);
4467 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004468
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004471#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004474 /*
4475 * On client, either start the renegotiation process or,
4476 * if already in progress, continue the handshake
4477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4479 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4480 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004481 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004482
4483 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4484 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4485 return ret;
4486 }
4487 } else {
4488 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4489 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4490 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004491 }
4492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004494
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004500{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004501 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004504 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004506
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004507#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Brett Warrene0edc842021-08-17 09:53:13 +01004508#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 if (ssl->handshake->group_list_heap_allocated) {
4510 mbedtls_free((void *) handshake->group_list);
4511 }
Brett Warrene0edc842021-08-17 09:53:13 +01004512 handshake->group_list = NULL;
4513#endif /* MBEDTLS_DEPRECATED_REMOVED */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01004514#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Brett Warrene0edc842021-08-17 09:53:13 +01004515
Ronald Crone68ab4f2022-10-05 12:46:29 +02004516#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004517#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if (ssl->handshake->sig_algs_heap_allocated) {
4519 mbedtls_free((void *) handshake->sig_algs);
4520 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004521 handshake->sig_algs = NULL;
4522#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004523#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 if (ssl->handshake->certificate_request_context) {
4525 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004526 }
4527#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004528#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004529
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004530#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4532 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004533 handshake->async_in_progress = 0;
4534 }
4535#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4536
Elena Uziunaite0916cd72024-05-23 17:01:07 +01004537#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004539#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01004540#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004542#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004544#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004546#endif
Valerio Setti7aeec542023-07-05 18:57:21 +02004547#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
Valerio Setti6eb00542023-07-07 17:04:24 +02004548 defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004550#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004551
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004552#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004554 /*
4555 * Opaque keys are not stored in the handshake's data and it's the user
4556 * responsibility to destroy them. Clear ones, instead, are created by
4557 * the TLS library and should be destroyed at the same level
4558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4560 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004561 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004562 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004563#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004565 handshake->ecjpake_cache = NULL;
4566 handshake->ecjpake_cache_len = 0;
4567#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004568#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004569
Valerio Setti7aeec542023-07-05 18:57:21 +02004570#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
Valerio Setti45d56f32023-07-13 17:23:20 +02004571 defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
Janos Follath4ae5c292016-02-10 11:27:43 +00004572 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004573 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004575#endif
4576
Ronald Cron73fe8df2022-10-05 14:31:43 +02004577#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004579 /* The maintenance of the external PSK key slot is the
4580 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (ssl->handshake->psk_opaque_is_internal) {
4582 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004583 ssl->handshake->psk_opaque_is_internal = 0;
4584 }
4585 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4586 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02004587#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004589#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4590 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004591 /*
4592 * Free only the linked list wrapper, not the keys themselves
4593 * since the belong to the SNI callback
4594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004597
Gilles Peskineeccd8882020-03-10 12:19:08 +01004598#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4600 if (handshake->ecrs_peer_cert != NULL) {
4601 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4602 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004603 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004604#endif
4605
Hanno Becker75173122019-02-06 16:18:31 +00004606#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4607 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004609#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4610
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004611#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4613 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004614#endif /* MBEDTLS_SSL_CLI_C &&
4615 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004616
4617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 mbedtls_ssl_flight_free(handshake->flight);
4619 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004620#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004621
Valerio Settiea59c432023-07-25 11:14:03 +02004622#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02004623 if (handshake->xxdh_psa_privkey_is_external == 0) {
4624 psa_destroy_key(handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 }
Valerio Settiea59c432023-07-25 11:14:03 +02004626#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004627
Ronald Cron6f135e12021-12-08 16:57:54 +01004628#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004629 mbedtls_ssl_transform_free(handshake->transform_handshake);
4630 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004631#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4633 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004634#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004635#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004636
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004637
4638#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4639 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4640 * processes datagrams and the fact that a datagram is allowed to have
4641 * several records in it, it is possible that the I/O buffers are not
4642 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4644 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004645#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004646
Jerry Yuba9c7272021-10-30 11:54:10 +08004647 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004648 mbedtls_platform_zeroize(handshake,
4649 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004650}
4651
Gilles Peskine449bd832023-01-11 14:50:10 +01004652void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004653{
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004655 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004660#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004661
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004662#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004663#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4664 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004666#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004668#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004669
Waleed Elmelegy2824a202024-02-23 17:51:47 +00004670#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) && \
4671 defined(MBEDTLS_SSL_SRV_C)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00004672 mbedtls_free(session->ticket_alpn);
Paul Bakker5121ce52009-01-03 21:22:43 +00004673#endif
4674
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker5121ce52009-01-03 21:22:43 +00004676}
4677
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004678#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004679
4680#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4681#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4682#else
4683#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4684#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4685
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004686#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004687
4688#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4689#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4690#else
4691#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4692#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4693
4694#if defined(MBEDTLS_SSL_ALPN)
4695#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4696#else
4697#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4698#endif /* MBEDTLS_SSL_ALPN */
4699
4700#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4701#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4702#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4703#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4704
4705#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 ((uint32_t) ( \
4707 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4708 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4709 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4710 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4711 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4712 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4713 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4714 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004715
Chien Wong4e9683e2023-12-28 17:07:43 +08004716static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004717 MBEDTLS_VERSION_MAJOR,
4718 MBEDTLS_VERSION_MINOR,
4719 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4721 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4722 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4723 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4724 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004725};
4726
Paul Bakker5121ce52009-01-03 21:22:43 +00004727/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004728 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004729 *
4730 * The format of the serialized data is:
4731 * (in the presentation language of TLS, RFC 8446 section 3)
4732 *
4733 * // header
4734 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004735 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004736 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004737 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004738 * Note: When updating the format, remember to keep these
4739 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004740 *
4741 * // session sub-structure
4742 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4743 * // transform sub-structure
4744 * uint8 random[64]; // ServerHello.random+ClientHello.random
4745 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4746 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4747 * // fields from ssl_context
4748 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4749 * uint64 in_window_top; // DTLS: last validated record seq_num
4750 * uint64 in_window; // DTLS: bitmask for replay protection
4751 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4752 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4753 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4754 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4755 *
4756 * Note that many fields of the ssl_context or sub-structures are not
4757 * serialized, as they fall in one of the following categories:
4758 *
4759 * 1. forced value (eg in_left must be 0)
4760 * 2. pointer to dynamically-allocated memory (eg session, transform)
4761 * 3. value can be re-derived from other data (eg session keys from MS)
4762 * 4. value was temporary (eg content of input buffer)
4763 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004764 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4766 unsigned char *buf,
4767 size_t buf_len,
4768 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004769{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004770 unsigned char *p = buf;
4771 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004772 size_t session_len;
4773 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004774
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004775 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004776 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4777 * this function's documentation.
4778 *
4779 * These are due to assumptions/limitations in the implementation. Some of
4780 * them are likely to stay (no handshake in progress) some might go away
4781 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004782 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004783 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4785 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4786 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004787 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (ssl->handshake != NULL) {
4789 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4790 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004791 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004792 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 if (ssl->transform == NULL || ssl->session == NULL) {
4794 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004796 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004797 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (mbedtls_ssl_check_pending(ssl) != 0) {
4799 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004801 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 if (ssl->out_left != 0) {
4803 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4804 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004805 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004806 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4808 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4809 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004810 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004811 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4813 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4814 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004815 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004816 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4818 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4819 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004820 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004821 /* Renegotiation must not be enabled */
4822#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4824 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4825 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004826 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004827#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004828
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004829 /*
4830 * Version and format identifier
4831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (used <= buf_len) {
4835 memcpy(p, ssl_serialized_context_header,
4836 sizeof(ssl_serialized_context_header));
4837 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004838 }
4839
4840 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004841 * Session (length + data)
4842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4844 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4845 return ret;
4846 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004847
4848 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 if (used <= buf_len) {
4850 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004851 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 ret = ssl_session_save(ssl->session, 1,
4854 p, session_len, &session_len);
4855 if (ret != 0) {
4856 return ret;
4857 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004858
4859 p += session_len;
4860 }
4861
4862 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004863 * Transform
4864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 used += sizeof(ssl->transform->randbytes);
4866 if (used <= buf_len) {
4867 memcpy(p, ssl->transform->randbytes,
4868 sizeof(ssl->transform->randbytes));
4869 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004870 }
4871
4872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Dave Rodgmanc37ad442023-11-03 23:36:06 +00004873 used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004875 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004877 p += ssl->transform->in_cid_len;
4878
4879 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004881 p += ssl->transform->out_cid_len;
4882 }
4883#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4884
4885 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004886 * Saved fields from top-level ssl_context structure
4887 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004888 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (used <= buf_len) {
4890 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004891 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004892 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004893
4894#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4895 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 if (used <= buf_len) {
4897 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004898 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004901 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004902 }
4903#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4904
4905#if defined(MBEDTLS_SSL_PROTO_DTLS)
4906 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004908 *p++ = ssl->disable_datagram_packing;
4909 }
4910#endif /* MBEDTLS_SSL_PROTO_DTLS */
4911
Jerry Yuae0b2e22021-10-08 15:21:19 +08004912 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if (used <= buf_len) {
4914 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004915 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004916 }
4917
4918#if defined(MBEDTLS_SSL_PROTO_DTLS)
4919 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 if (used <= buf_len) {
4921 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004922 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004923 }
4924#endif /* MBEDTLS_SSL_PROTO_DTLS */
4925
4926#if defined(MBEDTLS_SSL_ALPN)
4927 {
4928 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004929 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004930 : 0;
4931
4932 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004934 *p++ = alpn_len;
4935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 if (ssl->alpn_chosen != NULL) {
4937 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004938 p += alpn_len;
4939 }
4940 }
4941 }
4942#endif /* MBEDTLS_SSL_ALPN */
4943
4944 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004945 * Done
4946 */
4947 *olen = used;
4948
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 if (used > buf_len) {
4950 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4951 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004952
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004956}
4957
4958/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004959 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004960 *
4961 * This internal version is wrapped by a public function that cleans up in
4962 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004963 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004964MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004965static int ssl_context_load(mbedtls_ssl_context *ssl,
4966 const unsigned char *buf,
4967 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004968{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004969 const unsigned char *p = buf;
4970 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004971 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004972 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004973#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004974 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004975#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004976
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004977 /*
4978 * The context should have been freshly setup or reset.
4979 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004980 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004981 * renegotiating, or if the user mistakenly loaded a session first.)
4982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4984 ssl->session != NULL) {
4985 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004986 }
4987
4988 /*
4989 * We can't check that the config matches the initial one, but we can at
4990 * least check it matches the requirements for serializing.
4991 */
Dave Rodgmane99b24d2023-09-14 15:45:03 +01004992 if (
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004993#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004994 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004995#endif
Dave Rodgmane99b24d2023-09-14 15:45:03 +01004996 ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
4997 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4998 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
4999 ) {
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02005001 }
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005004
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005005 /*
5006 * Check version identifier
5007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005008 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
5009 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005010 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005011
5012 if (memcmp(p, ssl_serialized_context_header,
5013 sizeof(ssl_serialized_context_header)) != 0) {
5014 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5015 }
5016 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005017
5018 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005019 * Session
5020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005021 if ((size_t) (end - p) < 4) {
5022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5023 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005024
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005025 session_len = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005026 p += 4;
5027
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005028 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005029 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005030 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005031 ssl->session_in = ssl->session;
5032 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005033 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005034
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 if ((size_t) (end - p) < session_len) {
5036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5037 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005038
Gilles Peskine449bd832023-01-11 14:50:10 +01005039 ret = ssl_session_load(ssl->session, 1, p, session_len);
5040 if (ret != 0) {
5041 mbedtls_ssl_session_free(ssl->session);
5042 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005043 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005044
5045 p += session_len;
5046
5047 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005048 * Transform
5049 */
5050
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005051 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00005052 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08005053#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005054 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005055 ssl->transform_in = ssl->transform;
5056 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02005057 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08005058#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005059
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
5062 if (prf_func == NULL) {
5063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5064 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04005065
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005066 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005067 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
5068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5069 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04005070
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 ret = ssl_tls12_populate_transform(ssl->transform,
5072 ssl->session->ciphersuite,
5073 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005074#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02005076#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 prf_func,
5078 p, /* currently pointing to randbytes */
5079 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
5080 ssl->conf->endpoint,
5081 ssl);
5082 if (ret != 0) {
5083 return ret;
5084 }
Jerry Yu840fbb22022-02-17 14:59:29 +08005085#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005087
5088#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5089 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if ((size_t) (end - p) < 1) {
5091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5092 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005093
5094 ssl->transform->in_cid_len = *p++;
5095
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
5097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5098 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005099
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005101 p += ssl->transform->in_cid_len;
5102
5103 ssl->transform->out_cid_len = *p++;
5104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
5106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5107 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005108
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02005110 p += ssl->transform->out_cid_len;
5111#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5112
5113 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005114 * Saved fields from top-level ssl_context structure
5115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 if ((size_t) (end - p) < 4) {
5117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5118 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005119
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005120 ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005121 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005122
5123#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if ((size_t) (end - p) < 16) {
5125 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5126 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005127
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005128 ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005129 p += 8;
5130
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +01005131 ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005132 p += 8;
5133#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
5134
5135#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 if ((size_t) (end - p) < 1) {
5137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5138 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005139
5140 ssl->disable_datagram_packing = *p++;
5141#endif /* MBEDTLS_SSL_PROTO_DTLS */
5142
Gilles Peskine449bd832023-01-11 14:50:10 +01005143 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
5144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5145 }
5146 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
5147 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005148
5149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 if ((size_t) (end - p) < 2) {
5151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5152 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005153
Dave Rodgmana3d0f612023-11-03 23:34:02 +00005154 ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005155 p += 2;
5156#endif /* MBEDTLS_SSL_PROTO_DTLS */
5157
5158#if defined(MBEDTLS_SSL_ALPN)
5159 {
5160 uint8_t alpn_len;
5161 const char **cur;
5162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if ((size_t) (end - p) < 1) {
5164 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5165 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005166
5167 alpn_len = *p++;
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005170 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
5172 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy131b2ff2024-03-14 01:39:39 +00005173 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005174 ssl->alpn_chosen = *cur;
5175 break;
5176 }
5177 }
5178 }
5179
5180 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
5182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5183 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02005184
5185 p += alpn_len;
5186 }
5187#endif /* MBEDTLS_SSL_ALPN */
5188
5189 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005190 * Forced fields from top-level ssl_context structure
5191 *
5192 * Most of them already set to the correct value by mbedtls_ssl_init() and
5193 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
5194 */
5195 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04005196 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005197
Hanno Becker361b10d2019-08-30 10:42:49 +01005198 /* Adjust pointers for header fields of outgoing records to
5199 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01005201
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005202#if defined(MBEDTLS_SSL_PROTO_DTLS)
5203 ssl->in_epoch = 1;
5204#endif
5205
5206 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
5207 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005208 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
5209 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 if (ssl->handshake != NULL) {
5211 mbedtls_ssl_handshake_free(ssl);
5212 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02005213 ssl->handshake = NULL;
5214 }
5215
5216 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02005217 * Done - should have consumed entire buffer
5218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005219 if (p != end) {
5220 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5221 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005222
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02005224}
5225
5226/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005227 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005229int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
5230 const unsigned char *buf,
5231 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005232{
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 if (ret != 0) {
5236 mbedtls_ssl_free(context);
5237 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005238
Gilles Peskine449bd832023-01-11 14:50:10 +01005239 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005240}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02005241#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02005242
5243/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 * Free an SSL context
5245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005247{
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005249 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005251
Gilles Peskine449bd832023-01-11 14:50:10 +01005252 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005255#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5256 size_t out_buf_len = ssl->out_buf_len;
5257#else
5258 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
5259#endif
5260
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005261 mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005262 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005263 }
5264
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02005266#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
5267 size_t in_buf_len = ssl->in_buf_len;
5268#else
5269 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
5270#endif
5271
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005272 mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005273 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005274 }
5275
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 if (ssl->transform) {
5277 mbedtls_ssl_transform_free(ssl->transform);
5278 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00005279 }
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (ssl->handshake) {
5282 mbedtls_ssl_handshake_free(ssl);
5283 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08005284
5285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 mbedtls_ssl_transform_free(ssl->transform_negotiate);
5287 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08005288#endif
5289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 mbedtls_ssl_session_free(ssl->session_negotiate);
5291 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00005292 }
5293
Ronald Cron6f135e12021-12-08 16:57:54 +01005294#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 mbedtls_ssl_transform_free(ssl->transform_application);
5296 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01005297#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (ssl->session) {
5300 mbedtls_ssl_session_free(ssl->session);
5301 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01005302 }
5303
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02005304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 if (ssl->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005306 mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005309
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005310#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005312#endif
5313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00005315
Paul Bakker86f04f42013-02-14 11:20:09 +01005316 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01005317 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00005318}
5319
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005321 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005323void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005324{
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005326}
5327
Gilles Peskineae270bf2021-06-02 00:05:29 +02005328/* The selection should be the same as mbedtls_x509_crt_profile_default in
5329 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02005330 * curves with a lower resource usage come first.
Manuel Pégourié-Gonnard93d45912025-01-14 11:45:44 +01005331 * See the documentation of mbedtls_ssl_conf_groups() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02005332 * about this list.
5333 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005334static const uint16_t ssl_preset_default_groups[] = {
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005335#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Brett Warrene0edc842021-08-17 09:53:13 +01005336 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005337#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005338#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005339 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005340#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005341#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005342 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005343#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005344#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Brett Warrene0edc842021-08-17 09:53:13 +01005345 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005346#endif
Elena Uziunaite24e24f22024-07-04 17:53:40 +01005347#if defined(PSA_WANT_ECC_SECP_R1_521)
Brett Warrene0edc842021-08-17 09:53:13 +01005348 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005349#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005350#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005351 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02005352#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005353#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005354 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005355#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005356#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Brett Warrene0edc842021-08-17 09:53:13 +01005357 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02005358#endif
Przemek Stekiel316c19e2023-05-31 15:25:11 +02005359#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel383f4712022-12-12 14:48:57 +01005360 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
5361 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
5362 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
5363 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
5364 MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
5365#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005366 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02005367};
Gilles Peskineae270bf2021-06-02 00:05:29 +02005368
Alexey Tsvetkov2ca34372022-06-07 10:21:05 +03005369static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005370 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
5371 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
5372 0
5373};
5374
Ronald Crone68ab4f2022-10-05 12:46:29 +02005375#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005376
Jerry Yu909df7b2022-01-22 11:56:27 +08005377/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08005378 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08005379 * rules SHOULD be upheld.
5380 * - No duplicate entries.
5381 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08005382 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08005383 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08005384 */
Chien Wong4e9683e2023-12-28 17:07:43 +08005385static const uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08005386
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005387#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005388 defined(PSA_WANT_ALG_SHA_256) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005389 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yued5e9f42022-01-26 11:21:34 +08005390 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005391 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5392#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005393
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005394#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005395 defined(PSA_WANT_ALG_SHA_384) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005396 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu909df7b2022-01-22 11:56:27 +08005397 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005398 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5399#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005400
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005401#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Gabor Mezeic15ef932024-06-13 12:53:54 +02005402 defined(PSA_WANT_ALG_SHA_512) && \
Valerio Setticf29c5d2023-09-01 09:03:41 +02005403 defined(PSA_WANT_ECC_SECP_R1_521)
Jerry Yued5e9f42022-01-26 11:21:34 +08005404 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005405 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
5406#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005407
Gabor Mezeic15ef932024-06-13 12:53:54 +02005408#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005409 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005410#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005411
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005412#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005413 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005414#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005415
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005416#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005417 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005418#endif
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005419
Gabor Mezeic15ef932024-06-13 12:53:54 +02005420#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005421 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Gabor Mezeic15ef932024-06-13 12:53:54 +02005422#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005423
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005424#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005425 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005426#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005427
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005428#if defined(MBEDTLS_RSA_C) && defined(PSA_WANT_ALG_SHA_256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005429 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005430#endif /* MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005431
Gabor Mezei15b95a62022-05-09 16:37:58 +02005432 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005433};
5434
5435/* NOTICE: see above */
5436#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5437static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005438
Gabor Mezeic15ef932024-06-13 12:53:54 +02005439#if defined(PSA_WANT_ALG_SHA_512)
Valerio Settie9646ec2023-08-02 20:02:28 +02005440#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005442#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005443#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5444 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Yanray Wang1136fad2023-11-22 16:54:31 +08005445#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005446#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005448#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005449#endif /* PSA_WANT_ALG_SHA_512 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005450
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005451#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005452#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005454#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005455#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5456 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Yanray Wang1136fad2023-11-22 16:54:31 +08005457#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005458#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005460#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005461#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005462
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005463#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005464#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005466#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005467#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5468 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Yanray Wang1136fad2023-11-22 16:54:31 +08005469#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005470#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005472#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005473#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005474
Gabor Mezei15b95a62022-05-09 16:37:58 +02005475 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005476};
5477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005478
Jerry Yu909df7b2022-01-22 11:56:27 +08005479/* NOTICE: see above */
Chien Wong4e9683e2023-12-28 17:07:43 +08005480static const uint16_t ssl_preset_suiteb_sig_algs[] = {
Jerry Yu909df7b2022-01-22 11:56:27 +08005481
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005482#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005483 defined(PSA_WANT_ALG_SHA_256) && \
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005484 defined(PSA_WANT_ECC_SECP_R1_256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005485 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005486 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
5487#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005488
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005489#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005490 defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005491 defined(PSA_WANT_ECC_SECP_R1_384)
Jerry Yu53037892022-01-25 11:02:06 +08005492 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnard275afe12023-09-18 11:19:20 +02005493 // == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
5494#endif
Jerry Yu909df7b2022-01-22 11:56:27 +08005495
Gabor Mezei15b95a62022-05-09 16:37:58 +02005496 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005497};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005498
Jerry Yu909df7b2022-01-22 11:56:27 +08005499/* NOTICE: see above */
5500#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5501static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Yanray Wang1136fad2023-11-22 16:54:31 +08005502
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005503#if defined(PSA_WANT_ALG_SHA_256)
Valerio Settie9646ec2023-08-02 20:02:28 +02005504#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005506#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005507#endif /* PSA_WANT_ALG_SHA_256 */
Yanray Wang69ceb392023-11-22 16:32:39 +08005508
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005509#if defined(PSA_WANT_ALG_SHA_384)
Valerio Settie9646ec2023-08-02 20:02:28 +02005510#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005512#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005513#endif /* PSA_WANT_ALG_SHA_384 */
Yanray Wang1136fad2023-11-22 16:54:31 +08005514
Gabor Mezei15b95a62022-05-09 16:37:58 +02005515 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005516};
Jerry Yu909df7b2022-01-22 11:56:27 +08005517#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5518
Ronald Crone68ab4f2022-10-05 12:46:29 +02005519#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005520
Chien Wong4e9683e2023-12-28 17:07:43 +08005521static const uint16_t ssl_preset_suiteb_groups[] = {
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005522#if defined(PSA_WANT_ECC_SECP_R1_256)
Brett Warrene0edc842021-08-17 09:53:13 +01005523 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005524#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005525#if defined(PSA_WANT_ECC_SECP_R1_384)
Brett Warrene0edc842021-08-17 09:53:13 +01005526 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005527#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005528 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005529};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005530
Ronald Crone68ab4f2022-10-05 12:46:29 +02005531#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005532/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005533 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005534MBEDTLS_CHECK_RETURN_CRITICAL
Chien Wong4e9683e2023-12-28 17:07:43 +08005535static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005536{
5537 size_t i, j;
5538 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5541 for (j = 0; j < i; j++) {
5542 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005543 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 }
5545 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5546 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5547 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005548 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005549 }
5550 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005552}
5553
Ronald Crone68ab4f2022-10-05 12:46:29 +02005554#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005555
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005556/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005557 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005559int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5560 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005561{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005562#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005564#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005565
Ronald Crone68ab4f2022-10-05 12:46:29 +02005566#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005567 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5568 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5569 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005570 }
5571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5573 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5574 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005575 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005576
5577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5579 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5580 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005581 }
5582
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5584 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5585 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005586 }
5587#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005588#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005589
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005590 /* Use the functions here so that they are covered in tests,
5591 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 mbedtls_ssl_conf_endpoint(conf, endpoint);
5593 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005594
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005595 /*
5596 * Things that are common to all presets
5597 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005598#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005600 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5601#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5602 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5603#endif
5604 }
5605#endif
5606
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005607#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5608 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5609#endif
5610
5611#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5612 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5613#endif
5614
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005615#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005616 conf->f_cookie_write = ssl_cookie_write_dummy;
5617 conf->f_cookie_check = ssl_cookie_check_dummy;
5618#endif
5619
5620#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5621 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5622#endif
5623
Janos Follath088ce432017-04-10 12:42:31 +01005624#if defined(MBEDTLS_SSL_SRV_C)
5625 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005626 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005627#endif
5628
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005629#if defined(MBEDTLS_SSL_PROTO_DTLS)
5630 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5631 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5632#endif
5633
5634#if defined(MBEDTLS_SSL_RENEGOTIATION)
5635 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 memset(conf->renego_period, 0x00, 2);
5637 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005638#endif
5639
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005640#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005642 const unsigned char dhm_p[] =
5643 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5644 const unsigned char dhm_g[] =
5645 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5648 dhm_p, sizeof(dhm_p),
5649 dhm_g, sizeof(dhm_g))) != 0) {
5650 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005651 }
5652 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005653#endif
5654
Ronald Cron6f135e12021-12-08 16:57:54 +01005655#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005656
5657#if defined(MBEDTLS_SSL_EARLY_DATA)
Yanray Wangd5ed36f2023-11-07 11:40:43 +08005658 mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005659#if defined(MBEDTLS_SSL_SRV_C)
Yanray Wang07517612023-11-07 11:47:36 +08005660 mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005661#endif
5662#endif /* MBEDTLS_SSL_EARLY_DATA */
5663
Jerry Yud0766ec2022-09-22 10:46:57 +08005664#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005665 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005667#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005668 /*
5669 * Allow all TLS 1.3 key exchange modes by default.
5670 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005671 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005673
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005676 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005677 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005678#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005680#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005682#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron097ba142023-03-08 16:18:00 +01005683 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5684 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005685#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5686 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5687 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5688#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5689 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5690 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5691#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005693#endif
5694 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005695
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005696 /*
5697 * Preset-specific defaults
5698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005700 /*
5701 * NSA Suite B
5702 */
5703 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005704
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005705 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005706
5707#if defined(MBEDTLS_X509_CRT_PARSE_C)
5708 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005709#endif
5710
Ronald Crone68ab4f2022-10-05 12:46:29 +02005711#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005712#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005713 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005714 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005718#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005719
Brett Warrene0edc842021-08-17 09:53:13 +01005720 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005721 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005722
5723 /*
5724 * Default
5725 */
5726 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005727
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005728 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005729
5730#if defined(MBEDTLS_X509_CRT_PARSE_C)
5731 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5732#endif
5733
Ronald Crone68ab4f2022-10-05 12:46:29 +02005734#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005735#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005737 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005739#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005741#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005742
Brett Warrene0edc842021-08-17 09:53:13 +01005743 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005744
5745#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5746 conf->dhm_min_bitlen = 1024;
5747#endif
5748 }
5749
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005751}
5752
5753/*
5754 * Free mbedtls_ssl_config
5755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005756void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005757{
Troy-Butlerda73abc2024-04-02 13:37:31 -04005758 if (conf == NULL) {
5759 return;
5760 }
5761
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005762#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 mbedtls_mpi_free(&conf->dhm_P);
5764 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005765#endif
5766
Ronald Cron73fe8df2022-10-05 14:31:43 +02005767#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005769 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5770 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (conf->psk != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005772 mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005773 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005774 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005775 }
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 if (conf->psk_identity != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005778 mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
Azim Khan27e8a122018-03-21 14:24:11 +00005779 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005780 conf->psk_identity_len = 0;
5781 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005782#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005783
5784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005786#endif
5787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005789}
5790
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005791#if defined(MBEDTLS_PK_C) && \
Valerio Settie9646ec2023-08-02 20:02:28 +02005792 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005793/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005794 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005796unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5800 return MBEDTLS_SSL_SIG_RSA;
5801 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005802#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02005803#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005804 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5805 return MBEDTLS_SSL_SIG_ECDSA;
5806 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005807#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005809}
5810
Gilles Peskine449bd832023-01-11 14:50:10 +01005811unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005812{
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005814 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005816 case MBEDTLS_PK_ECDSA:
5817 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005819 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005821 }
5822}
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005825{
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827#if defined(MBEDTLS_RSA_C)
5828 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005830#endif
Valerio Settie9646ec2023-08-02 20:02:28 +02005831#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005834#endif
5835 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005837 }
5838}
Valerio Settie9646ec2023-08-02 20:02:28 +02005839#endif /* MBEDTLS_PK_C &&
5840 ( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005841
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005842/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005843 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005845mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005846{
Gilles Peskine449bd832023-01-11 14:50:10 +01005847 switch (hash) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01005848#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005851#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01005852#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005853 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005855#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01005856#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005859#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005860#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005863#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005864#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005867#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005868#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005871#endif
5872 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005874 }
5875}
5876
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005877/*
5878 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5879 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005880unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005881{
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 switch (md) {
Elena Uziunaiteb66a9912024-05-10 14:25:58 +01005883#if defined(PSA_WANT_ALG_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005884 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005885 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005886#endif
Elena Uziunaite9fc5be02024-09-04 18:12:59 +01005887#if defined(PSA_WANT_ALG_SHA_1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005888 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005890#endif
Elena Uziunaitefcc9afa2024-05-23 14:43:22 +01005891#if defined(PSA_WANT_ALG_SHA_224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005892 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005894#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01005895#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005896 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005898#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01005899#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005900 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005902#endif
Gabor Mezeic15ef932024-06-13 12:53:54 +02005903#if defined(PSA_WANT_ALG_SHA_512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005904 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005906#endif
5907 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005909 }
5910}
5911
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005912/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005913 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005914 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005916int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005917{
Manuel Pégourié-Gonnard6402c352025-01-14 12:23:56 +01005918 const uint16_t *group_list = ssl->conf->group_list;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005919
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 if (group_list == NULL) {
5921 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005922 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 for (; *group_list != 0; group_list++) {
5925 if (*group_list == tls_id) {
5926 return 0;
5927 }
5928 }
5929
5930 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005931}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005932
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01005933#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005934/*
5935 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005937int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005938{
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005942 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005946}
Elena Uziunaite8dde3b32024-07-05 12:10:21 +01005947#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Valerio Setti67419f02023-01-04 16:12:42 +01005948
Valerio Setti18c9fed2022-12-30 17:44:24 +01005949static const struct {
5950 uint16_t tls_id;
5951 mbedtls_ecp_group_id ecp_group_id;
5952 psa_ecc_family_t psa_family;
5953 uint16_t bits;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005954} tls_id_match_table[] =
5955{
Elena Uziunaite24e24f22024-07-04 17:53:40 +01005956#if defined(PSA_WANT_ECC_SECP_R1_521)
Valerio Settiacd32c02023-06-29 18:06:29 +02005957 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005958#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005959#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Valerio Settiacd32c02023-06-29 18:06:29 +02005960 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005961#endif
Elena Uziunaite6b4cd482024-07-04 17:50:11 +01005962#if defined(PSA_WANT_ECC_SECP_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02005963 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005964#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005965#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Valerio Settiacd32c02023-06-29 18:06:29 +02005966 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005967#endif
Elena Uziunaite417d05f2024-07-04 17:46:30 +01005968#if defined(PSA_WANT_ECC_SECP_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02005969 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005970#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01005971#if defined(PSA_WANT_ECC_SECP_K1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02005972 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005973#endif
Elena Uziunaiteb8d10872024-07-05 10:51:40 +01005974#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Valerio Settiacd32c02023-06-29 18:06:29 +02005975 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005976#endif
Elena Uziunaiteeaa0cf02024-07-04 17:41:25 +01005977#if defined(PSA_WANT_ECC_SECP_R1_224)
Valerio Settiacd32c02023-06-29 18:06:29 +02005978 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005979#endif
Elena Uziunaitea3632862024-07-04 13:52:43 +01005980#if defined(PSA_WANT_ECC_SECP_R1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02005981 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005982#endif
Elena Uziunaite9e85c9f2024-07-04 18:37:34 +01005983#if defined(PSA_WANT_ECC_SECP_K1_192)
Valerio Settiacd32c02023-06-29 18:06:29 +02005984 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005985#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005986#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Valerio Settiacd32c02023-06-29 18:06:29 +02005987 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005988#endif
Elena Uziunaite0b5d48e2024-07-05 11:48:23 +01005989#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Valerio Settiacd32c02023-06-29 18:06:29 +02005990 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005991#endif
Valerio Settiacd32c02023-06-29 18:06:29 +02005992 { 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005993};
5994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
Przemek Stekielda4fba62023-06-02 14:52:28 +02005996 psa_key_type_t *type,
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005998{
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6000 if (tls_id_match_table[i].tls_id == tls_id) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02006001 if (type != NULL) {
6002 *type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 }
6004 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006005 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006007 return PSA_SUCCESS;
6008 }
6009 }
6010
6011 return PSA_ERROR_NOT_SUPPORTED;
6012}
6013
Gilles Peskine449bd832023-01-11 14:50:10 +01006014mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006015{
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
6017 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006018 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006020 }
6021
6022 return MBEDTLS_ECP_DP_NONE;
6023}
6024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006026{
Gilles Peskine449bd832023-01-11 14:50:10 +01006027 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
6028 i++) {
6029 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01006030 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01006031 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006032 }
6033
6034 return 0;
6035}
6036
Valerio Setti67419f02023-01-04 16:12:42 +01006037#if defined(MBEDTLS_DEBUG_C)
Valerio Settiacd32c02023-06-29 18:06:29 +02006038static const struct {
6039 uint16_t tls_id;
6040 const char *name;
6041} tls_id_curve_name_table[] =
6042{
Valerio Setti54e23792023-07-07 10:49:27 +02006043 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
6044 { MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
6045 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
6046 { MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
6047 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
6048 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
6049 { MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
6050 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
6051 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
6052 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
6053 { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
6054 { MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
6055 { MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
Valerio Settiacd32c02023-06-29 18:06:29 +02006056 { 0, NULL },
6057};
6058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01006060{
Valerio Settiacd32c02023-06-29 18:06:29 +02006061 for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
6062 if (tls_id_curve_name_table[i].tls_id == tls_id) {
6063 return tls_id_curve_name_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01006065 }
6066
6067 return NULL;
6068}
Valerio Setti67419f02023-01-04 16:12:42 +01006069#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
6072 const mbedtls_md_type_t md,
6073 unsigned char *dst,
6074 size_t dst_len,
6075 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08006076{
Ronald Cronf6893e12022-01-07 22:09:01 +01006077 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
6078 psa_hash_operation_t *hash_operation_to_clone;
6079 psa_hash_operation_t hash_operation = psa_hash_operation_init();
6080
Jerry Yu148165c2021-09-24 23:20:59 +08006081 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006084#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 case MBEDTLS_MD_SHA384:
6086 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
6087 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006088#endif
6089
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006090#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 case MBEDTLS_MD_SHA256:
6092 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
6093 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01006094#endif
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 default:
6097 goto exit;
6098 }
6099
6100 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
6101 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006102 goto exit;
6103 }
6104
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
6106 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01006107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 }
Ronald Cronf6893e12022-01-07 22:09:01 +01006109
6110exit:
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006111#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006112 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006113 (void) ssl;
6114#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05006115 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08006116}
Jerry Yu24c0ec32021-09-09 14:21:07 +08006117
Ronald Crone68ab4f2022-10-05 12:46:29 +02006118#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02006119/* mbedtls_ssl_parse_sig_alg_ext()
6120 *
6121 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
6122 * value (TLS 1.3 RFC8446):
6123 * enum {
6124 * ....
6125 * ecdsa_secp256r1_sha256( 0x0403 ),
6126 * ecdsa_secp384r1_sha384( 0x0503 ),
6127 * ecdsa_secp521r1_sha512( 0x0603 ),
6128 * ....
6129 * } SignatureScheme;
6130 *
6131 * struct {
6132 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
6133 * } SignatureSchemeList;
6134 *
6135 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
6136 * value (TLS 1.2 RFC5246):
6137 * enum {
6138 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
6139 * sha512(6), (255)
6140 * } HashAlgorithm;
6141 *
6142 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
6143 * SignatureAlgorithm;
6144 *
6145 * struct {
6146 * HashAlgorithm hash;
6147 * SignatureAlgorithm signature;
6148 * } SignatureAndHashAlgorithm;
6149 *
6150 * SignatureAndHashAlgorithm
6151 * supported_signature_algorithms<2..2^16-2>;
6152 *
6153 * The TLS 1.3 signature algorithm extension was defined to be a compatible
6154 * generalization of the TLS 1.2 signature algorithm extension.
6155 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
6156 * `SignatureScheme` field of TLS 1.3
6157 *
6158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006159int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
6160 const unsigned char *buf,
6161 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02006162{
6163 const unsigned char *p = buf;
6164 size_t supported_sig_algs_len = 0;
6165 const unsigned char *supported_sig_algs_end;
6166 uint16_t sig_alg;
6167 uint32_t common_idx = 0;
6168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
6170 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006171 p += 2;
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 memset(ssl->handshake->received_sig_algs, 0,
6174 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02006177 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 while (p < supported_sig_algs_end) {
6179 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
6180 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02006181 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
6183 sig_alg,
6184 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08006185#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
6187 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
6188 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006189 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08006190 }
6191#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
6194 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02006197 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6198 common_idx += 1;
6199 }
6200 }
6201 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (p != end) {
6203 MBEDTLS_SSL_DEBUG_MSG(1,
6204 ("Signature algorithms extension length misaligned"));
6205 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6206 MBEDTLS_ERR_SSL_DECODE_ERROR);
6207 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006208 }
6209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 if (common_idx == 0) {
6211 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6212 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6213 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6214 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006215 }
6216
Gabor Mezei15b95a62022-05-09 16:37:58 +02006217 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006219}
6220
Ronald Crone68ab4f2022-10-05 12:46:29 +02006221#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006222
Jerry Yudc7bd172022-02-17 13:44:15 +08006223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6224
Jerry Yudc7bd172022-02-17 13:44:15 +08006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6227 mbedtls_svc_key_id_t key,
6228 psa_algorithm_t alg,
6229 const unsigned char *raw_psk, size_t raw_psk_length,
6230 const unsigned char *seed, size_t seed_length,
6231 const unsigned char *label, size_t label_length,
6232 const unsigned char *other_secret,
6233 size_t other_secret_length,
6234 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006235{
6236 psa_status_t status;
6237
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 status = psa_key_derivation_setup(derivation, alg);
6239 if (status != PSA_SUCCESS) {
6240 return status;
6241 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006242
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6244 status = psa_key_derivation_input_bytes(derivation,
6245 PSA_KEY_DERIVATION_INPUT_SEED,
6246 seed, seed_length);
6247 if (status != PSA_SUCCESS) {
6248 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006249 }
6250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 if (other_secret != NULL) {
6252 status = psa_key_derivation_input_bytes(derivation,
6253 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6254 other_secret, other_secret_length);
6255 if (status != PSA_SUCCESS) {
6256 return status;
6257 }
6258 }
6259
6260 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006261 status = psa_key_derivation_input_bytes(
6262 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006263 raw_psk, raw_psk_length);
6264 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006265 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006267 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (status != PSA_SUCCESS) {
6269 return status;
6270 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 status = psa_key_derivation_input_bytes(derivation,
6273 PSA_KEY_DERIVATION_INPUT_LABEL,
6274 label, label_length);
6275 if (status != PSA_SUCCESS) {
6276 return status;
6277 }
6278 } else {
6279 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006280 }
6281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 status = psa_key_derivation_set_capacity(derivation, capacity);
6283 if (status != PSA_SUCCESS) {
6284 return status;
6285 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006288}
6289
Andrzej Kurek57d10632022-10-24 10:32:01 -04006290#if defined(PSA_WANT_ALG_SHA_384) || \
6291 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006293static int tls_prf_generic(mbedtls_md_type_t md_type,
6294 const unsigned char *secret, size_t slen,
6295 const char *label,
6296 const unsigned char *random, size_t rlen,
6297 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006298{
6299 psa_status_t status;
6300 psa_algorithm_t alg;
6301 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6302 psa_key_derivation_operation_t derivation =
6303 PSA_KEY_DERIVATION_OPERATION_INIT;
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006306 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006308 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006310
6311 /* Normally a "secret" should be long enough to be impossible to
6312 * find by brute force, and in particular should not be empty. But
6313 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6314 * and for this use case it makes sense to have a 0-length "secret".
6315 * Since the key API doesn't allow importing a key of length 0,
6316 * keep master_key=0, which setup_psa_key_derivation() understands
6317 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006319 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6321 psa_set_key_algorithm(&key_attributes, alg);
6322 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006323
Gilles Peskine449bd832023-01-11 14:50:10 +01006324 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6325 if (status != PSA_SUCCESS) {
6326 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6327 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006328 }
6329
Gilles Peskine449bd832023-01-11 14:50:10 +01006330 status = setup_psa_key_derivation(&derivation,
6331 master_key, alg,
6332 NULL, 0,
6333 random, rlen,
6334 (unsigned char const *) label,
6335 (size_t) strlen(label),
6336 NULL, 0,
6337 dlen);
6338 if (status != PSA_SUCCESS) {
6339 psa_key_derivation_abort(&derivation);
6340 psa_destroy_key(master_key);
6341 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006342 }
6343
Gilles Peskine449bd832023-01-11 14:50:10 +01006344 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6345 if (status != PSA_SUCCESS) {
6346 psa_key_derivation_abort(&derivation);
6347 psa_destroy_key(master_key);
6348 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006349 }
6350
Gilles Peskine449bd832023-01-11 14:50:10 +01006351 status = psa_key_derivation_abort(&derivation);
6352 if (status != PSA_SUCCESS) {
6353 psa_destroy_key(master_key);
6354 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006355 }
6356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 if (!mbedtls_svc_key_id_is_null(master_key)) {
6358 status = psa_destroy_key(master_key);
6359 }
6360 if (status != PSA_SUCCESS) {
6361 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6362 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006365}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006366#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006367
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006368#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006369MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006370static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6371 const char *label,
6372 const unsigned char *random, size_t rlen,
6373 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006374{
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6376 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006377}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006378#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006379
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006380#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006382static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6383 const char *label,
6384 const unsigned char *random, size_t rlen,
6385 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006386{
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6388 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006389}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006390#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006391
Jerry Yuf009d862022-02-17 14:01:37 +08006392/*
6393 * Set appropriate PRF function and other SSL / TLS1.2 functions
6394 *
6395 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006396 * - hash associated with the ciphersuite (only used by TLS 1.2)
6397 *
6398 * Outputs:
6399 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6400 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006401MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006402static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6403 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006404{
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006405#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006407 handshake->tls_prf = tls_prf_sha384;
6408 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6409 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006411#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006412#if defined(PSA_WANT_ALG_SHA_256)
Jerry Yuf009d862022-02-17 14:01:37 +08006413 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006414 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006415 handshake->tls_prf = tls_prf_sha256;
6416 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6417 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6418 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006419#else
Jerry Yuf009d862022-02-17 14:01:37 +08006420 {
Jerry Yuf009d862022-02-17 14:01:37 +08006421 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006422 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006424 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006425#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006428}
Jerry Yud6ab2352022-02-17 14:03:43 +08006429
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006430/*
6431 * Compute master secret if needed
6432 *
6433 * Parameters:
6434 * [in/out] handshake
6435 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6436 * (PSA-PSK) ciphersuite_info, psk_opaque
6437 * [out] premaster (cleared)
6438 * [out] master
6439 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6440 * debug: conf->f_dbg, conf->p_dbg
6441 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006442 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006445static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6446 unsigned char *master,
6447 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006448{
6449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6450
6451 /* cf. RFC 5246, Section 8.1:
6452 * "The master secret is always exactly 48 bytes in length." */
6453 size_t const master_secret_len = 48;
6454
6455#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6456 unsigned char session_hash[48];
6457#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6458
6459 /* The label for the KDF used for key expansion.
6460 * This is either "master secret" or "extended master secret"
6461 * depending on whether the Extended Master Secret extension
6462 * is used. */
6463 char const *lbl = "master secret";
6464
Przemek Stekielae4ed302022-04-05 17:15:55 +02006465 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006466 * - If the Extended Master Secret extension is not used,
6467 * this is ClientHello.Random + ServerHello.Random
6468 * (see Sect. 8.1 in RFC 5246).
6469 * - If the Extended Master Secret extension is used,
6470 * this is the transcript of the handshake so far.
6471 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006472 unsigned char const *seed = handshake->randbytes;
6473 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006474
6475#if !defined(MBEDTLS_DEBUG_C) && \
6476 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6477 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006479 ssl = NULL; /* make sure we don't use it except for those cases */
6480 (void) ssl;
6481#endif
6482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 if (handshake->resume != 0) {
6484 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6485 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006486 }
6487
6488#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006490 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006491 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006492 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6493 if (ret != 0) {
6494 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6495 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6498 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006499 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006500#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006501
Przemek Stekiel99114f32022-04-22 11:20:09 +02006502#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006503 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006505 /* Perform PSK-to-MS expansion in a single step. */
6506 psa_status_t status;
6507 psa_algorithm_t alg;
6508 mbedtls_svc_key_id_t psk;
6509 psa_key_derivation_operation_t derivation =
6510 PSA_KEY_DERIVATION_OPERATION_INIT;
Dave Rodgman2eab4622023-10-05 13:30:37 +01006511 mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006512
Gilles Peskine449bd832023-01-11 14:50:10 +01006513 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006516
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006518 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006519 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006520 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006522
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006523 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006527 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006528 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006529 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006530 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006531 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
6532 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6533 other_secret = handshake->premaster + 2;
6534 break;
6535 default:
6536 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006537 }
6538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 status = setup_psa_key_derivation(&derivation, psk, alg,
6540 ssl->conf->psk, ssl->conf->psk_len,
6541 seed, seed_len,
6542 (unsigned char const *) lbl,
6543 (size_t) strlen(lbl),
6544 other_secret, other_secret_len,
6545 master_secret_len);
6546 if (status != PSA_SUCCESS) {
6547 psa_key_derivation_abort(&derivation);
6548 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006549 }
6550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 status = psa_key_derivation_output_bytes(&derivation,
6552 master,
6553 master_secret_len);
6554 if (status != PSA_SUCCESS) {
6555 psa_key_derivation_abort(&derivation);
6556 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006557 }
6558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 status = psa_key_derivation_abort(&derivation);
6560 if (status != PSA_SUCCESS) {
6561 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6562 }
6563 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006564#endif
6565 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006566#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6568 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006569 psa_status_t status;
6570 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6571 psa_key_derivation_operation_t derivation =
6572 PSA_KEY_DERIVATION_OPERATION_INIT;
6573
Gilles Peskine449bd832023-01-11 14:50:10 +01006574 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006575
6576 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 status = psa_key_derivation_setup(&derivation, alg);
6579 if (status != PSA_SUCCESS) {
6580 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006581 }
6582
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 status = psa_key_derivation_set_capacity(&derivation,
6584 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6585 if (status != PSA_SUCCESS) {
6586 psa_key_derivation_abort(&derivation);
6587 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006588 }
6589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6591 &derivation);
6592 if (status != PSA_SUCCESS) {
6593 psa_key_derivation_abort(&derivation);
6594 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006595 }
6596
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 status = psa_key_derivation_output_bytes(&derivation,
6598 handshake->premaster,
6599 handshake->pmslen);
6600 if (status != PSA_SUCCESS) {
6601 psa_key_derivation_abort(&derivation);
6602 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6603 }
6604
6605 status = psa_key_derivation_abort(&derivation);
6606 if (status != PSA_SUCCESS) {
6607 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006608 }
6609 }
6610#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6612 lbl, seed, seed_len,
6613 master,
6614 master_secret_len);
6615 if (ret != 0) {
6616 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6617 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006618 }
6619
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6621 handshake->premaster,
6622 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 mbedtls_platform_zeroize(handshake->premaster,
6625 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006626 }
6627
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006629}
6630
Gilles Peskine449bd832023-01-11 14:50:10 +01006631int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006632{
6633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6634 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6635 ssl->handshake->ciphersuite_info;
6636
Gilles Peskine449bd832023-01-11 14:50:10 +01006637 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006638
6639 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 ret = ssl_set_handshake_prfs(ssl->handshake,
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006641 (mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 if (ret != 0) {
6643 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6644 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006645 }
6646
6647 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 ret = ssl_compute_master(ssl->handshake,
6649 ssl->session_negotiate->master,
6650 ssl);
6651 if (ret != 0) {
6652 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6653 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006654 }
6655
6656 /* Swap the client and server random values:
6657 * - MS derivation wanted client+server (RFC 5246 8.1)
6658 * - key derivation wants server+client (RFC 5246 6.3) */
6659 {
6660 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 memcpy(tmp, ssl->handshake->randbytes, 64);
6662 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6663 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6664 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006665 }
6666
6667 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6669 ssl->session_negotiate->ciphersuite,
6670 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006671#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006673#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 ssl->handshake->tls_prf,
6675 ssl->handshake->randbytes,
6676 ssl->tls_version,
6677 ssl->conf->endpoint,
6678 ssl);
6679 if (ret != 0) {
6680 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6681 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006682 }
6683
6684 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6686 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006687
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006691}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006692
Gilles Peskine449bd832023-01-11 14:50:10 +01006693int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006694{
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 switch (md) {
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006696#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006697 case MBEDTLS_SSL_HASH_SHA384:
6698 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6699 break;
6700#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006701#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006702 case MBEDTLS_SSL_HASH_SHA256:
6703 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6704 break;
6705#endif
6706 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006708 }
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006709#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006710 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006711 (void) ssl;
6712#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006714}
6715
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006716static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
6717 const psa_hash_operation_t *hs_op,
6718 size_t buffer_size,
6719 unsigned char *hash,
6720 size_t *hlen)
6721{
6722 psa_status_t status;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006723 psa_hash_operation_t cloned_op = psa_hash_operation_init();
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006724
6725#if !defined(MBEDTLS_DEBUG_C)
6726 (void) ssl;
6727#endif
6728 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006729 status = psa_hash_clone(hs_op, &cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006730 if (status != PSA_SUCCESS) {
6731 goto exit;
6732 }
6733
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006734 status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006735 if (status != PSA_SUCCESS) {
6736 goto exit;
6737 }
6738
6739 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6740 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
6741
6742exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02006743 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006744 return mbedtls_md_error_from_psa(status);
6745}
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006746
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006747#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006748int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6749 unsigned char *hash,
6750 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006751{
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006752 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
6753 hash, hlen);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006754}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01006755#endif /* PSA_WANT_ALG_SHA_256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006756
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006757#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006758int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6759 unsigned char *hash,
6760 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006761{
Manuel Pégourié-Gonnard74970662023-06-24 09:43:26 +02006762 return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
6763 hash, hlen);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006764}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01006765#endif /* PSA_WANT_ALG_SHA_384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006766
Neil Armstrong80f6f322022-05-03 17:56:38 +02006767#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6768 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006769int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006770{
6771 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006772 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006773 const unsigned char *psk = NULL;
6774 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006776
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006778 /*
6779 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006780 * checked before calling this function.
Jerry Yuce3dca42022-02-17 14:16:37 +08006781 */
Valerio Setti48659a12025-01-15 14:22:28 +01006782 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6783 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006784 }
6785
6786 /*
6787 * PMS = struct {
6788 * opaque other_secret<0..2^16-1>;
6789 * opaque psk<0..2^16-1>;
6790 * };
6791 * with "other_secret" depending on the particular key exchange
6792 */
6793#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6795 if (end - p < 2) {
6796 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6797 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006800 p += 2;
6801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (end < p || (size_t) (end - p) < psk_len) {
6803 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6804 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006807 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006809#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006810#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6813 size_t zlen;
6814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006816 p + 2, (size_t) (end - (p + 2)),
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6819 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006820 }
6821
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006823 p += 2 + zlen;
6824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6826 MBEDTLS_DEBUG_ECDH_Z);
6827 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006828#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006829 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6831 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 }
6833
6834 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 if (end - p < 2) {
6836 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6837 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006840 p += 2;
6841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 if (end < p || (size_t) (end - p) < psk_len) {
6843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6844 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006847 p += psk_len;
6848
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00006849 ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006852}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006853#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006854
6855#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006856MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006857static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006858
6859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006860int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006861{
6862 /* If renegotiation is not enforced, retransmit until we would reach max
6863 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006865 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6866 unsigned char doublings = 1;
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006869 ++doublings;
6870 ratio >>= 1;
6871 }
6872
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (++ssl->renego_records_seen > doublings) {
6874 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6875 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006876 }
6877 }
6878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006880}
6881#endif
6882#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006883
Jerry Yud9526692022-02-17 14:23:47 +08006884/*
6885 * Handshake functions
6886 */
6887#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6888/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006889int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006890{
6891 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6892 ssl->handshake->ciphersuite_info;
6893
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006895
Gilles Peskine449bd832023-01-11 14:50:10 +01006896 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6897 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006898 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006900 }
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6903 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006904}
6905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006907{
6908 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6909 ssl->handshake->ciphersuite_info;
6910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6914 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006915 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006916 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006917 }
6918
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6920 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006921}
6922
6923#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6924/* Some certificate support -> implement write and parse */
6925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006927{
6928 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6929 size_t i, n;
6930 const mbedtls_x509_crt *crt;
6931 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6932 ssl->handshake->ciphersuite_info;
6933
Gilles Peskine449bd832023-01-11 14:50:10 +01006934 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6937 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006938 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006940 }
6941
6942#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6944 if (ssl->handshake->client_auth == 0) {
6945 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006946 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006948 }
6949 }
6950#endif /* MBEDTLS_SSL_CLI_C */
6951#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6953 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006954 /* Should never happen because we shouldn't have picked the
6955 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006957 }
6958 }
6959#endif
6960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006962
6963 /*
6964 * 0 . 0 handshake type
6965 * 1 . 3 handshake length
6966 * 4 . 6 length of all certs
6967 * 7 . 9 length of cert. 1
6968 * 10 . n-1 peer certificate
6969 * n . n+2 length of cert. 2
6970 * n+3 . ... upper level cert, etc.
6971 */
6972 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006976 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6978 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6979 " > %" MBEDTLS_PRINTF_SIZET,
6980 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6981 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006982 }
6983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6985 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6986 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006989 i += n; crt = crt->next;
6990 }
6991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6993 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6994 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006995
6996 ssl->out_msglen = i;
6997 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6998 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6999
7000 ssl->state++;
7001
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7003 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7004 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007005 }
7006
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007010}
7011
7012#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7013
7014#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007016static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7017 unsigned char *crt_buf,
7018 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007019{
7020 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7021
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 if (peer_crt == NULL) {
7023 return -1;
7024 }
Jerry Yud9526692022-02-17 14:23:47 +08007025
Gilles Peskine449bd832023-01-11 14:50:10 +01007026 if (peer_crt->raw.len != crt_buf_len) {
7027 return -1;
7028 }
Jerry Yud9526692022-02-17 14:23:47 +08007029
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007031}
7032#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007033MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007034static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7035 unsigned char *crt_buf,
7036 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007037{
7038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7039 unsigned char const * const peer_cert_digest =
7040 ssl->session->peer_cert_digest;
7041 mbedtls_md_type_t const peer_cert_digest_type =
7042 ssl->session->peer_cert_digest_type;
7043 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007044 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007045 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7046 size_t digest_len;
7047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 if (peer_cert_digest == NULL || digest_info == NULL) {
7049 return -1;
7050 }
Jerry Yud9526692022-02-17 14:23:47 +08007051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 digest_len = mbedtls_md_get_size(digest_info);
7053 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7054 return -1;
7055 }
Jerry Yud9526692022-02-17 14:23:47 +08007056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7058 if (ret != 0) {
7059 return -1;
7060 }
Jerry Yud9526692022-02-17 14:23:47 +08007061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007063}
7064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7065#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7066
7067/*
7068 * Once the certificate message is read, parse it into a cert chain and
7069 * perform basic checks, but leave actual verification to the caller
7070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007072static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7073 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007074{
7075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7076#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007078#endif
7079 size_t i, n;
7080 uint8_t alert;
7081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7083 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7084 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7085 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7086 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007087 }
7088
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7090 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7091 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7092 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007093 }
7094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7096 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7097 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7098 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7099 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007100 }
7101
Gilles Peskine449bd832023-01-11 14:50:10 +01007102 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007103
7104 /*
7105 * Same message structure as in mbedtls_ssl_write_certificate()
7106 */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007107 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007108
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 if (ssl->in_msg[i] != 0 ||
7110 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7111 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7112 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7113 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7114 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007115 }
7116
7117 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7118 i += 3;
7119
7120 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007122 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 if (i + 3 > ssl->in_hslen) {
7124 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7125 mbedtls_ssl_send_alert_message(ssl,
7126 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7127 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7128 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007129 }
7130 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7131 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (ssl->in_msg[i] != 0) {
7133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7134 mbedtls_ssl_send_alert_message(ssl,
7135 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7136 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7137 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139
7140 /* Read length of the next CRT in the chain. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00007141 n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
Jerry Yud9526692022-02-17 14:23:47 +08007142 i += 3;
7143
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 if (n < 128 || i + n > ssl->in_hslen) {
7145 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7146 mbedtls_ssl_send_alert_message(ssl,
7147 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7148 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7149 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007150 }
7151
7152 /* Check if we're handling the first CRT in the chain. */
7153#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007155 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007156 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007157 /* During client-side renegotiation, check that the server's
7158 * end-CRTs hasn't changed compared to the initial handshake,
7159 * mitigating the triple handshake attack. On success, reuse
7160 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7162 if (ssl_check_peer_crt_unchanged(ssl,
7163 &ssl->in_msg[i],
7164 n) != 0) {
7165 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7166 mbedtls_ssl_send_alert_message(ssl,
7167 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7168 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7169 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007170 }
7171
7172 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007173 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007174 }
7175#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7176
7177 /* Parse the next certificate in the chain. */
7178#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007180#else
7181 /* If we don't need to store the CRT chain permanently, parse
7182 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007184#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007186 case 0: /*ok*/
7187 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7188 /* Ignore certificate with an unknown algorithm: maybe a
7189 prior certificate was already trusted. */
7190 break;
7191
7192 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7193 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7194 goto crt_parse_der_failed;
7195
7196 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7197 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7198 goto crt_parse_der_failed;
7199
7200 default:
7201 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007202crt_parse_der_failed:
7203 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7204 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7205 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007206 }
7207
7208 i += n;
7209 }
7210
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7212 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007213}
7214
7215#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007216MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007217static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007218{
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7220 return -1;
7221 }
Jerry Yud9526692022-02-17 14:23:47 +08007222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007224 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7225 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7227 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7228 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007231}
7232#endif /* MBEDTLS_SSL_SRV_C */
7233
7234/* Check if a certificate message is expected.
7235 * Return either
7236 * - SSL_CERTIFICATE_EXPECTED, or
7237 * - SSL_CERTIFICATE_SKIP
7238 * indicating whether a Certificate message is expected or not.
7239 */
7240#define SSL_CERTIFICATE_EXPECTED 0
7241#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007243static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7244 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007245{
7246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7247 ssl->handshake->ciphersuite_info;
7248
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7250 return SSL_CERTIFICATE_SKIP;
7251 }
Jerry Yud9526692022-02-17 14:23:47 +08007252
7253#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Gilles Peskine449bd832023-01-11 14:50:10 +01007255 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007256 ssl->session_negotiate->verify_result =
7257 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007259 }
7260 }
7261#else
7262 ((void) authmode);
7263#endif /* MBEDTLS_SSL_SRV_C */
7264
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007266}
7267
Jerry Yud9526692022-02-17 14:23:47 +08007268#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007269MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007270static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7271 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007272{
7273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7274 /* Remember digest of the peer's end-CRT. */
7275 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007276 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7277 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7278 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7279 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7280 mbedtls_ssl_send_alert_message(ssl,
7281 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7282 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007283
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007285 }
7286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 ret = mbedtls_md(mbedtls_md_info_from_type(
7288 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7289 start, len,
7290 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007291
7292 ssl->session_negotiate->peer_cert_digest_type =
7293 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7294 ssl->session_negotiate->peer_cert_digest_len =
7295 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007298}
7299
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007300MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007301static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7302 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007303{
7304 unsigned char *end = start + len;
7305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7306
7307 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7309 ret = mbedtls_pk_parse_subpubkey(&start, end,
7310 &ssl->handshake->peer_pubkey);
7311 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007312 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007314 }
7315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007317}
7318#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007321{
7322 int ret = 0;
7323 int crt_expected;
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +02007324 /* Authmode: precedence order is SNI if used else configuration */
Jerry Yud9526692022-02-17 14:23:47 +08007325#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7326 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7327 ? ssl->handshake->sni_authmode
7328 : ssl->conf->authmode;
7329#else
7330 const int authmode = ssl->conf->authmode;
7331#endif
7332 void *rs_ctx = NULL;
7333 mbedtls_x509_crt *chain = NULL;
7334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007336
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7338 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7339 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007340 goto exit;
7341 }
7342
7343#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (ssl->handshake->ecrs_enabled &&
7345 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007346 chain = ssl->handshake->ecrs_peer_cert;
7347 ssl->handshake->ecrs_peer_cert = NULL;
7348 goto crt_verify;
7349 }
7350#endif
7351
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007353 /* mbedtls_ssl_read_record may have sent an alert already. We
7354 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007356 goto exit;
7357 }
7358
7359#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007361 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7362
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007364 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 }
Jerry Yud9526692022-02-17 14:23:47 +08007366
7367 goto exit;
7368 }
7369#endif /* MBEDTLS_SSL_SRV_C */
7370
7371 /* Clear existing peer CRT structure in case we tried to
7372 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7376 if (chain == NULL) {
7377 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7378 sizeof(mbedtls_x509_crt)));
7379 mbedtls_ssl_send_alert_message(ssl,
7380 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7381 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007382
7383 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7384 goto exit;
7385 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007386 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007387
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 ret = ssl_parse_certificate_chain(ssl, chain);
7389 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007390 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 }
Jerry Yud9526692022-02-17 14:23:47 +08007392
7393#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007395 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 }
Jerry Yud9526692022-02-17 14:23:47 +08007397
7398crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007400 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 }
Jerry Yud9526692022-02-17 14:23:47 +08007402#endif
7403
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +02007404 ret = mbedtls_ssl_verify_certificate(ssl, authmode, chain,
7405 ssl->handshake->ciphersuite_info,
7406 rs_ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007408 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 }
Jerry Yud9526692022-02-17 14:23:47 +08007410
7411#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7412 {
7413 unsigned char *crt_start, *pk_start;
7414 size_t crt_len, pk_len;
7415
7416 /* We parse the CRT chain without copying, so
7417 * these pointers point into the input buffer,
7418 * and are hence still valid after freeing the
7419 * CRT chain. */
7420
7421 crt_start = chain->raw.p;
7422 crt_len = chain->raw.len;
7423
7424 pk_start = chain->pk_raw.p;
7425 pk_len = chain->pk_raw.len;
7426
7427 /* Free the CRT structures before computing
7428 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 mbedtls_x509_crt_free(chain);
7430 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007431 chain = NULL;
7432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7434 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 }
Jerry Yud9526692022-02-17 14:23:47 +08007437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7439 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 }
Jerry Yud9526692022-02-17 14:23:47 +08007442 }
7443#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7444 /* Pass ownership to session structure. */
7445 ssl->session_negotiate->peer_cert = chain;
7446 chain = NULL;
7447#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007450
7451exit:
7452
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007454 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 }
Jerry Yud9526692022-02-17 14:23:47 +08007456
7457#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007459 ssl->handshake->ecrs_peer_cert = chain;
7460 chain = NULL;
7461 }
7462#endif
7463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 if (chain != NULL) {
7465 mbedtls_x509_crt_free(chain);
7466 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007467 }
7468
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007470}
7471#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7472
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007473static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
7474 unsigned char *padbuf, size_t hlen,
7475 unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007476{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00007477 unsigned int len = 12;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007478 const char *sender;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007479 psa_status_t status;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007480 psa_hash_operation_t *hs_op = ctx;
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007481 psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007482 size_t hash_size;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007483
7484 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007486 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007487 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007490 ? "client finished"
7491 : "server finished";
7492
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007493 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007494
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007495 status = psa_hash_clone(hs_op, &cloned_op);
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007497 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007498 }
7499
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007500 status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007502 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007503 }
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007504 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007505
7506 MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
7507
Jerry Yu615bd6f2022-02-17 14:25:15 +08007508 /*
7509 * TLSv1.2:
7510 * hash = PRF( master, finished_label,
7511 * Hash( handshake ) )[0.11]
7512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 ssl->handshake->tls_prf(session->master, 48, sender,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007514 padbuf, hlen, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007515
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007517
Paul Elliottecb95be2023-08-11 16:41:04 +01007518 mbedtls_platform_zeroize(padbuf, hlen);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007519
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007520 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007521
7522exit:
Manuel Pégourié-Gonnardaaad2b62023-07-04 11:35:16 +02007523 psa_hash_abort(&cloned_op);
Manuel Pégourié-Gonnard3761e9e2023-03-28 12:54:56 +02007524 return mbedtls_md_error_from_psa(status);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007525}
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007526
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007527#if defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007528static int ssl_calc_finished_tls_sha256(
7529 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
7530{
7531 unsigned char padbuf[32];
7532 return ssl_calc_finished_tls_generic(ssl,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007533 &ssl->handshake->fin_sha256_psa,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007534 padbuf, sizeof(padbuf),
7535 buf, from);
7536}
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007537#endif /* PSA_WANT_ALG_SHA_256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007538
Jerry Yub7ba49e2022-02-17 14:25:53 +08007539
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007540#if defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007541static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007543{
Jerry Yub7ba49e2022-02-17 14:25:53 +08007544 unsigned char padbuf[48];
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007545 return ssl_calc_finished_tls_generic(ssl,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007546 &ssl->handshake->fin_sha384_psa,
Manuel Pégourié-Gonnardde332782023-06-24 10:13:41 +02007547 padbuf, sizeof(padbuf),
7548 buf, from);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007549}
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007550#endif /* PSA_WANT_ALG_SHA_384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007551
Gilles Peskine449bd832023-01-11 14:50:10 +01007552void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007553{
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007555
7556 /*
7557 * Free our handshake params
7558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 mbedtls_ssl_handshake_free(ssl);
7560 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007561 ssl->handshake = NULL;
7562
7563 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007564 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (ssl->transform) {
7567 mbedtls_ssl_transform_free(ssl->transform);
7568 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007569 }
7570 ssl->transform = ssl->transform_negotiate;
7571 ssl->transform_negotiate = NULL;
7572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007574}
7575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007577{
7578 int resume = ssl->handshake->resume;
7579
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007581
7582#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007583 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007584 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7585 ssl->renego_records_seen = 0;
7586 }
7587#endif
7588
7589 /*
7590 * Free the previous session and switch in the current one
7591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007593#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7594 /* RFC 7366 3.1: keep the EtM state */
7595 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007597#endif
7598
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 mbedtls_ssl_session_free(ssl->session);
7600 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007601 }
7602 ssl->session = ssl->session_negotiate;
7603 ssl->session_negotiate = NULL;
7604
7605 /*
7606 * Add cache entry
7607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007609 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 resume == 0) {
7611 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7612 ssl->session->id,
7613 ssl->session->id_len,
7614 ssl->session) != 0) {
7615 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7616 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007617 }
7618
7619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7621 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007622 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007624
7625 /* Keep last flight around in case we need to resend it:
7626 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007627 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7628 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007629#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007630 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007631
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007632 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007633
Gilles Peskine449bd832023-01-11 14:50:10 +01007634 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007635}
7636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007638{
Dave Rodgmanc37ad442023-11-03 23:36:06 +00007639 int ret;
7640 unsigned int hash_len;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007643
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007645
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007646 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7647 if (ret != 0) {
7648 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7649 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007650
7651 /*
7652 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7653 * may define some other value. Currently (early 2016), no defined
7654 * ciphersuite does this (and this is unlikely to change as activity has
7655 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7656 */
7657 hash_len = 12;
7658
7659#if defined(MBEDTLS_SSL_RENEGOTIATION)
7660 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007662#endif
7663
7664 ssl->out_msglen = 4 + hash_len;
7665 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7666 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7667
7668 /*
7669 * In case of session resuming, invert the client and server
7670 * ChangeCipherSpec messages order.
7671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007673#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007675 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007677#endif
7678#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007680 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007682#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007684 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007686
7687 /*
7688 * Switch to our negotiated transform and session parameters for outbound
7689 * data.
7690 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007692
7693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007695 unsigned char i;
7696
7697 /* Remember current epoch settings for resending */
7698 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7700 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007701
7702 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007704
7705
7706 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 for (i = 2; i > 0; i--) {
7708 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007709 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 }
7711 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007712
7713 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 if (i == 0) {
7715 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7716 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007717 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007719#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007721
7722 ssl->transform_out = ssl->transform_negotiate;
7723 ssl->session_out = ssl->session_negotiate;
7724
7725#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7727 mbedtls_ssl_send_flight_completed(ssl);
7728 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007729#endif
7730
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7732 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7733 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007734 }
7735
7736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7738 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7739 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7740 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007741 }
7742#endif
7743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007745
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007747}
7748
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007749#define SSL_MAX_HASH_LEN 12
7750
Gilles Peskine449bd832023-01-11 14:50:10 +01007751int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007752{
7753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7754 unsigned int hash_len = 12;
7755 unsigned char buf[SSL_MAX_HASH_LEN];
7756
Gilles Peskine449bd832023-01-11 14:50:10 +01007757 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007758
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007759 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7760 if (ret != 0) {
7761 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7762 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007763
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7765 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007766 goto exit;
7767 }
7768
Gilles Peskine449bd832023-01-11 14:50:10 +01007769 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7770 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7771 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7772 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007773 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7774 goto exit;
7775 }
7776
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7778 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7779 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007780 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7781 goto exit;
7782 }
7783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7785 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7786 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7787 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007788 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7789 goto exit;
7790 }
7791
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7793 buf, hash_len) != 0) {
7794 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7795 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7796 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007797 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7798 goto exit;
7799 }
7800
7801#if defined(MBEDTLS_SSL_RENEGOTIATION)
7802 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007804#endif
7805
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007807#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007809 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007810 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007811#endif
7812#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007813 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007814 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007816#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007818 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007820
7821#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7823 mbedtls_ssl_recv_flight_completed(ssl);
7824 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007825#endif
7826
Gilles Peskine449bd832023-01-11 14:50:10 +01007827 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007828
7829exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 mbedtls_platform_zeroize(buf, hash_len);
7831 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007832}
7833
Jerry Yu392112c2022-02-17 14:34:10 +08007834#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7835/*
7836 * Helper to get TLS 1.2 PRF from ciphersuite
7837 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007839static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007840{
Jerry Yu392112c2022-02-17 14:34:10 +08007841 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007843#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7845 return tls_prf_sha384;
7846 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007847#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007848#if defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04007849 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7851 return tls_prf_sha256;
7852 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007853 }
7854#endif
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007855#if !defined(PSA_WANT_ALG_SHA_384) && \
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007856 !defined(PSA_WANT_ALG_SHA_256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04007857 (void) ciphersuite_info;
7858#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007859
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08007861}
7862#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007863
Gilles Peskine449bd832023-01-11 14:50:10 +01007864static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007865{
7866 ((void) tls_prf);
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +01007867#if defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 if (tls_prf == tls_prf_sha384) {
7869 return MBEDTLS_SSL_TLS_PRF_SHA384;
7870 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08007871#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +01007872#if defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 if (tls_prf == tls_prf_sha256) {
7874 return MBEDTLS_SSL_TLS_PRF_SHA256;
7875 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08007876#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08007878}
7879
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007880/*
7881 * Populate a transform structure with session keys and all the other
7882 * necessary information.
7883 *
7884 * Parameters:
7885 * - [in/out]: transform: structure to populate
7886 * [in] must be just initialised with mbedtls_ssl_transform_init()
7887 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7888 * - [in] ciphersuite
7889 * - [in] master
7890 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007891 * - [in] tls_prf: pointer to PRF to use for key derivation
7892 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04007893 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007894 * - [in] endpoint: client or server
7895 * - [in] ssl: used for:
7896 * - ssl->conf->{f,p}_export_keys
7897 * [in] optionally used for:
7898 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
7899 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007900MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007901static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
7902 int ciphersuite,
7903 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007904#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007906#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 ssl_tls_prf_t tls_prf,
7908 const unsigned char randbytes[64],
7909 mbedtls_ssl_protocol_version tls_version,
7910 unsigned endpoint,
7911 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007912{
7913 int ret = 0;
7914 unsigned char keyblk[256];
7915 unsigned char *key1;
7916 unsigned char *key2;
7917 unsigned char *mac_enc;
7918 unsigned char *mac_dec;
7919 size_t mac_key_len = 0;
7920 size_t iv_copy_len;
7921 size_t keylen;
7922 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007923 mbedtls_ssl_mode_t ssl_mode;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007924
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007925 psa_key_type_t key_type;
7926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7927 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01007928 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007929 size_t key_bits;
7930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007931
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007932 /*
7933 * Some data just needs copying into the structure
7934 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007935#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007936 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007937#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04007938 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007939
7940#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007942#endif
7943
7944#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007946 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
7947 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007949 }
7950#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
7951
7952 /*
7953 * Get various info structures
7954 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
7956 if (ciphersuite_info == NULL) {
7957 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
7958 ciphersuite));
7959 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007960 }
7961
Neil Armstrongab555e02022-04-04 11:07:59 +02007962 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007963#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02007965#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02007967
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007969 transform->taglen =
7970 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007972
Dave Rodgman2eab4622023-10-05 13:30:37 +01007973 if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 transform->taglen,
7975 &alg,
7976 &key_type,
7977 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05007978 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02007980 goto end;
7981 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007982
Dave Rodgman2eab4622023-10-05 13:30:37 +01007983 mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01007984 if (mac_alg == 0) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02007985 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 (unsigned) ciphersuite_info->mac));
7987 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01007988 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007989
7990#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
7991 /* Copy own and peer's CID if the use of the CID
7992 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
7994 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007995
7996 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
7998 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
7999 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008000
8001 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8003 ssl->handshake->peer_cid_len);
8004 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8005 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008006 }
8007#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8008
8009 /*
8010 * Compute key block using the PRF
8011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8013 if (ret != 0) {
8014 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8015 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008016 }
8017
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8019 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8020 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8021 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8022 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008023
8024 /*
8025 * Determine the appropriate key, IV and MAC length.
8026 */
8027
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008028 keylen = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008029
Valerio Settie5707042023-10-11 11:54:42 +02008030#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008032 size_t explicit_ivlen;
8033
8034 transform->maclen = 0;
8035 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008036
8037 /* All modes haves 96-bit IVs, but the length of the static parts vary
8038 * with mode and version:
8039 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8040 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8041 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8042 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8043 * sequence number).
8044 */
8045 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008046
8047 int is_chachapoly = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
David Horstmann3b2276a2022-10-06 14:49:08 +01008049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008051 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008053 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008055
8056 /* Minimum length of encrypted record */
8057 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8058 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 } else
Valerio Settie5707042023-10-11 11:54:42 +02008060#endif /* MBEDTLS_SSL_HAVE_AEAD */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008061#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008063 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008066
Neil Armstronge4512952022-03-08 09:08:22 +01008067 /* Get MAC length */
8068 mac_key_len = PSA_HASH_LENGTH(mac_alg);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008069 transform->maclen = mac_key_len;
8070
8071 /* IV length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008072 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008073
8074 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008075 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008076 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008078 /*
8079 * GenericBlockCipher:
8080 * 1. if EtM is in use: one block plus MAC
8081 * otherwise: * first multiple of blocklen greater than maclen
8082 * 2. IV
8083 */
8084#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008086 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008087 + block_size;
8088 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008089#endif
8090 {
8091 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 + block_size
8093 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008094 }
8095
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 } else {
8099 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8101 goto end;
8102 }
8103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008104 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008105#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8106 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8108 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008109 }
8110
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8112 (unsigned) keylen,
8113 (unsigned) transform->minlen,
8114 (unsigned) transform->ivlen,
8115 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008116
8117 /*
8118 * Finally setup the cipher contexts, IVs and MAC secrets.
8119 */
8120#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008122 key1 = keyblk + mac_key_len * 2;
8123 key2 = keyblk + mac_key_len * 2 + keylen;
8124
8125 mac_enc = keyblk;
8126 mac_dec = keyblk + mac_key_len;
8127
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 iv_copy_len = (transform->fixed_ivlen) ?
8129 transform->fixed_ivlen : transform->ivlen;
8130 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8131 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8132 iv_copy_len);
8133 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008134#endif /* MBEDTLS_SSL_CLI_C */
8135#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008137 key1 = keyblk + mac_key_len * 2 + keylen;
8138 key2 = keyblk + mac_key_len * 2;
8139
8140 mac_enc = keyblk + mac_key_len;
8141 mac_dec = keyblk;
8142
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 iv_copy_len = (transform->fixed_ivlen) ?
8144 transform->fixed_ivlen : transform->ivlen;
8145 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8146 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8147 iv_copy_len);
8148 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008149#endif /* MBEDTLS_SSL_SRV_C */
8150 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008152 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8153 goto end;
8154 }
8155
Paul Elliott4fb19552023-10-18 12:15:30 +01008156 if (ssl->f_export_keys != NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 ssl->f_export_keys(ssl->p_export_keys,
8158 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8159 master, 48,
8160 randbytes + 32,
8161 randbytes,
8162 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008163 }
8164
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008165 transform->psa_alg = alg;
8166
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8168 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8169 psa_set_key_algorithm(&attributes, alg);
8170 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008171
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 if ((status = psa_import_key(&attributes,
8173 key1,
8174 PSA_BITS_TO_BYTES(key_bits),
8175 &transform->psa_key_enc)) != PSA_SUCCESS) {
8176 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008177 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008179 goto end;
8180 }
8181
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008183
Gilles Peskine449bd832023-01-11 14:50:10 +01008184 if ((status = psa_import_key(&attributes,
8185 key2,
8186 PSA_BITS_TO_BYTES(key_bits),
8187 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008188 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008189 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008190 goto end;
8191 }
8192 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008193
Neil Armstrong29c0c042022-03-17 17:47:28 +01008194#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8195 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8196 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 if (mac_key_len != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008199
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8201 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8202 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008203
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 if ((status = psa_import_key(&attributes,
8205 mac_enc, mac_key_len,
8206 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008207 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008209 goto end;
8210 }
8211
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8213 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008214#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008216#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008218 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008219 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8220 PSA_KEY_USAGE_VERIFY_HASH);
8221 } else {
8222 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8223 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008224
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 if ((status = psa_import_key(&attributes,
8226 mac_dec, mac_key_len,
8227 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008228 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008230 goto end;
8231 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008232 }
8233#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8234
8235 ((void) mac_dec);
8236 ((void) mac_enc);
8237
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8240 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008241}
8242
Valerio Settia08b1a42022-11-17 15:10:02 +01008243#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8244 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008245int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 psa_pake_operation_t *pake_ctx,
8247 const unsigned char *buf,
8248 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008249{
8250 psa_status_t status;
8251 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008252 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008253 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8254 * At round two perform a single cycle
8255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008257
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 for (; remaining_steps > 0; remaining_steps--) {
8259 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008260 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008262 /* Length is stored at the first byte */
8263 size_t length = buf[input_offset];
8264 input_offset += 1;
8265
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008267 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8268 }
8269
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 status = psa_pake_input(pake_ctx, step,
8271 buf + input_offset, length);
8272 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008273 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008274 }
8275
8276 input_offset += length;
8277 }
8278 }
8279
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008281 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008283
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008285}
8286
Valerio Setti6b3dab02022-11-17 17:14:54 +01008287int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 psa_pake_operation_t *pake_ctx,
8289 unsigned char *buf,
8290 size_t len, size_t *olen,
8291 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008292{
8293 psa_status_t status;
8294 size_t output_offset = 0;
8295 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008296 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008297 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8298 * At round two perform a single cycle
8299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 for (; remaining_steps > 0; remaining_steps--) {
8303 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8304 step <= PSA_PAKE_STEP_ZK_PROOF;
8305 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008306 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008307 * For each step, prepend 1 byte with the length of the data as
8308 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 status = psa_pake_output(pake_ctx, step,
8311 buf + output_offset + 1,
8312 len - output_offset - 1,
8313 &output_len);
8314 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008315 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008316 }
8317
Valerio Setti99d88c12022-11-22 16:03:43 +01008318 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008319
8320 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008321 }
8322 }
8323
8324 *olen = output_offset;
8325
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008327}
Valerio Settia08b1a42022-11-17 15:10:02 +01008328#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8329
Gilles Peskine449bd832023-01-11 14:50:10 +01008330int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8331 unsigned char *hash, size_t *hashlen,
8332 unsigned char *data, size_t data_len,
8333 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008334{
8335 psa_status_t status;
8336 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02008337 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008338
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008340
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 if ((status = psa_hash_setup(&hash_operation,
8342 hash_alg)) != PSA_SUCCESS) {
8343 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008344 goto exit;
8345 }
8346
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8348 64)) != PSA_SUCCESS) {
8349 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008350 goto exit;
8351 }
8352
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 if ((status = psa_hash_update(&hash_operation,
8354 data, data_len)) != PSA_SUCCESS) {
8355 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008356 goto exit;
8357 }
8358
Gilles Peskine449bd832023-01-11 14:50:10 +01008359 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8360 hashlen)) != PSA_SUCCESS) {
8361 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8362 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008363 }
8364
8365exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 if (status != PSA_SUCCESS) {
8367 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8368 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8369 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008370 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008372 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8373 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008375 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008376 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008377 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008379 }
8380 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008382}
8383
Jerry Yuee40f9d2022-02-17 14:55:16 +08008384
Jerry Yud9d91da2022-02-17 14:57:06 +08008385#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8386
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008387/* Find the preferred hash for a given signature algorithm. */
8388unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 mbedtls_ssl_context *ssl,
8390 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008391{
Gabor Mezei078e8032022-04-27 21:17:56 +02008392 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008393 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008394
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8396 return MBEDTLS_SSL_HASH_NONE;
8397 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008398
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008400 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8402 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008403 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8405 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008406
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02008407 mbedtls_md_type_t md_alg =
8408 mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
8409 if (md_alg == MBEDTLS_MD_NONE) {
8410 continue;
8411 }
8412
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 if (sig_alg == sig_alg_received) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008415 psa_algorithm_t psa_hash_alg =
Manuel Pégourié-Gonnard47bb3802023-06-05 12:40:32 +02008416 mbedtls_md_psa_alg_from_type(md_alg);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008417
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8419 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8420 PSA_ALG_ECDSA(psa_hash_alg),
8421 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008422 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008424
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8426 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8427 PSA_ALG_RSA_PKCS1V15_SIGN(
8428 psa_hash_alg),
8429 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008430 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008432 }
Neil Armstrong96eceb82022-06-30 18:05:05 +02008433
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008435 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008436 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008437
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008439}
8440
8441#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8442
Jerry Yudc7bd172022-02-17 13:44:15 +08008443#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8444
XiaokangQian75d40ef2022-04-20 11:05:24 +00008445int mbedtls_ssl_validate_ciphersuite(
8446 const mbedtls_ssl_context *ssl,
8447 const mbedtls_ssl_ciphersuite_t *suite_info,
8448 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008450{
8451 (void) ssl;
8452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 if (suite_info == NULL) {
8454 return -1;
8455 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00008456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 if ((suite_info->min_tls_version > max_tls_version) ||
8458 (suite_info->max_tls_version < min_tls_version)) {
8459 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00008460 }
8461
XiaokangQian060d8672022-04-21 09:24:56 +00008462#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00008463#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
Manuel Pégourié-Gonnard88800dd2025-01-23 11:01:35 +01008465 ssl->handshake->psa_pake_ctx_is_ok != 1) {
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00008467 }
8468#endif
8469
8470 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
8471#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
8473 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
8474 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00008475 }
8476#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
8477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
8478
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00008480}
8481
Ronald Crone68ab4f2022-10-05 12:46:29 +02008482#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00008483/*
8484 * Function for writing a signature algorithm extension.
8485 *
8486 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
8487 * value (TLS 1.3 RFC8446):
8488 * enum {
8489 * ....
8490 * ecdsa_secp256r1_sha256( 0x0403 ),
8491 * ecdsa_secp384r1_sha384( 0x0503 ),
8492 * ecdsa_secp521r1_sha512( 0x0603 ),
8493 * ....
8494 * } SignatureScheme;
8495 *
8496 * struct {
8497 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
8498 * } SignatureSchemeList;
8499 *
8500 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
8501 * value (TLS 1.2 RFC5246):
8502 * enum {
8503 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
8504 * sha512(6), (255)
8505 * } HashAlgorithm;
8506 *
8507 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
8508 * SignatureAlgorithm;
8509 *
8510 * struct {
8511 * HashAlgorithm hash;
8512 * SignatureAlgorithm signature;
8513 * } SignatureAndHashAlgorithm;
8514 *
8515 * SignatureAndHashAlgorithm
8516 * supported_signature_algorithms<2..2^16-2>;
8517 *
8518 * The TLS 1.3 signature algorithm extension was defined to be a compatible
8519 * generalization of the TLS 1.2 signature algorithm extension.
8520 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
8521 * `SignatureScheme` field of TLS 1.3
8522 *
8523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008524int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
8525 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00008526{
8527 unsigned char *p = buf;
8528 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
8529 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
8530
8531 *out_len = 0;
8532
Gilles Peskine449bd832023-01-11 14:50:10 +01008533 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00008534
8535 /* Check if we have space for header and length field:
8536 * - extension_type (2 bytes)
8537 * - extension_data_length (2 bytes)
8538 * - supported_signature_algorithms_length (2 bytes)
8539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00008541 p += 6;
8542
8543 /*
8544 * Write supported_signature_algorithms
8545 */
8546 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
8548 if (sig_alg == NULL) {
8549 return MBEDTLS_ERR_SSL_BAD_CONFIG;
8550 }
XiaokangQianeaf36512022-04-24 09:07:44 +00008551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
8553 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
8554 *sig_alg,
8555 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
8556 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00008557 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 }
8559 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
8560 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00008561 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
8563 *sig_alg,
8564 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00008565 }
8566
8567 /* Length of supported_signature_algorithms */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00008568 supported_sig_alg_len = (size_t) (p - supported_sig_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 if (supported_sig_alg_len == 0) {
8570 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
8571 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00008572 }
8573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
8575 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
8576 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00008577
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00008578 *out_len = (size_t) (p - buf);
XiaokangQianeaf36512022-04-24 09:07:44 +00008579
8580#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00008582#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08008583
Gilles Peskine449bd832023-01-11 14:50:10 +01008584 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00008585}
Ronald Crone68ab4f2022-10-05 12:46:29 +02008586#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00008587
XiaokangQian40a35232022-05-07 09:02:40 +00008588#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00008589/*
8590 * mbedtls_ssl_parse_server_name_ext
8591 *
8592 * Structure of server_name extension:
8593 *
8594 * enum {
8595 * host_name(0), (255)
8596 * } NameType;
8597 * opaque HostName<1..2^16-1>;
8598 *
8599 * struct {
8600 * NameType name_type;
8601 * select (name_type) {
8602 * case host_name: HostName;
8603 * } name;
8604 * } ServerName;
8605 * struct {
8606 * ServerName server_name_list<1..2^16-1>
8607 * } ServerNameList;
8608 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02008609MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008610int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
8611 const unsigned char *buf,
8612 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00008613{
8614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8615 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00008616 size_t server_name_list_len, hostname_len;
8617 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
8622 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00008623 p += 2;
8624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00008626 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 while (p < server_name_list_end) {
8628 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
8629 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
8630 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
8631 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00008634 /* sni_name is intended to be used only during the parsing of the
8635 * ClientHello message (it is reset to NULL before the end of
8636 * the message parsing). Thus it is ok to just point to the
8637 * reception buffer and not make a copy of it.
8638 */
XiaokangQianf2a94202022-05-20 06:44:24 +00008639 ssl->handshake->sni_name = p + 3;
8640 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 if (ssl->conf->f_sni == NULL) {
8642 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00008643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 ret = ssl->conf->f_sni(ssl->conf->p_sni,
8645 ssl, p + 3, hostname_len);
8646 if (ret != 0) {
8647 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
8648 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
8649 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
8650 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
8651 }
8652 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00008653 }
8654
8655 p += hostname_len + 3;
8656 }
8657
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00008659}
8660#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8661
XiaokangQianacb39922022-06-17 10:18:48 +00008662#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02008663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008664int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
8665 const unsigned char *buf,
8666 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00008667{
8668 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00008669 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00008670 const unsigned char *protocol_name_list;
8671 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00008672 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008673
8674 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 if (ssl->conf->alpn_list == NULL) {
8676 return 0;
8677 }
XiaokangQianacb39922022-06-17 10:18:48 +00008678
8679 /*
XiaokangQianc7403452022-06-23 03:24:12 +00008680 * RFC7301, section 3.1
8681 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00008682 *
XiaokangQianc7403452022-06-23 03:24:12 +00008683 * struct {
8684 * ProtocolName protocol_name_list<2..2^16-1>
8685 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00008686 */
8687
XiaokangQianc7403452022-06-23 03:24:12 +00008688 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00008689 * protocol_name_list_len 2 bytes
8690 * protocol_name_len 1 bytes
8691 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00008692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00008696 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00008698 protocol_name_list = p;
8699 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008700
8701 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00008703 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
8705 protocol_name_len);
8706 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00008707 MBEDTLS_SSL_PEND_FATAL_ALERT(
8708 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
8710 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00008711 }
8712
8713 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008714 }
8715
8716 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01008717 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
8718 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00008719 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00008721 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 if (protocol_name_len == alpn_len &&
8723 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00008724 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008726 }
XiaokangQian95d5f542022-06-24 02:29:26 +00008727
8728 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008729 }
8730 }
8731
XiaokangQian95d5f542022-06-24 02:29:26 +00008732 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00008733 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
8735 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
8736 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00008737}
8738
Gilles Peskine449bd832023-01-11 14:50:10 +01008739int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
8740 unsigned char *buf,
8741 unsigned char *end,
8742 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00008743{
8744 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00008745 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00008746 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 if (ssl->alpn_chosen == NULL) {
8749 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008750 }
8751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 protocol_name_len = strlen(ssl->alpn_chosen);
8753 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00008756 /*
8757 * 0 . 1 ext identifier
8758 * 2 . 3 ext length
8759 * 4 . 5 protocol list length
8760 * 6 . 6 protocol name length
8761 * 7 . 7+n protocol name
8762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00008764
XiaokangQian95d5f542022-06-24 02:29:26 +00008765 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00008766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
8768 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00008769 /* Note: the length of the chosen protocol has been checked to be less
8770 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
8771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00008773
Gilles Peskine449bd832023-01-11 14:50:10 +01008774 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08008775
8776#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08008778#endif
8779
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00008781}
8782#endif /* MBEDTLS_SSL_ALPN */
8783
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008784#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00008785 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008786 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008787 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008788int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
8789 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008790{
8791 /* Initialize to suppress unnecessary compiler warning */
8792 size_t hostname_len = 0;
8793
8794 /* Check if new hostname is valid before
8795 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 if (hostname != NULL) {
8797 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
8800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8801 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008802 }
8803
8804 /* Now it's clear that we will overwrite the old hostname,
8805 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if (session->hostname != NULL) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01008807 mbedtls_zeroize_and_free(session->hostname,
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 strlen(session->hostname));
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008809 }
8810
8811 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008813 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 } else {
8815 session->hostname = mbedtls_calloc(1, hostname_len + 1);
8816 if (session->hostname == NULL) {
8817 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
8818 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008819
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008821 }
8822
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008824}
Xiaokang Qian03409292022-10-12 02:49:52 +00008825#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
8826 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00008827 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00008828 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00008829
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00008830#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) && \
8831 defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00008832int mbedtls_ssl_session_set_ticket_alpn(mbedtls_ssl_session *session,
8833 const char *alpn)
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00008834{
8835 size_t alpn_len = 0;
8836
8837 if (alpn != NULL) {
8838 alpn_len = strlen(alpn);
8839
8840 if (alpn_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) {
8841 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8842 }
8843 }
8844
8845 if (session->ticket_alpn != NULL) {
8846 mbedtls_zeroize_and_free(session->ticket_alpn,
8847 strlen(session->ticket_alpn));
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00008848 session->ticket_alpn = NULL;
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00008849 }
8850
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00008851 if (alpn != NULL) {
8852 session->ticket_alpn = mbedtls_calloc(alpn_len + 1, 1);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00008853 if (session->ticket_alpn == NULL) {
8854 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
8855 }
Waleed Elmelegy7dfba342024-03-12 16:22:39 +00008856 memcpy(session->ticket_alpn, alpn, alpn_len);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +00008857 }
8858
8859 return 0;
8860}
8861#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02008862
8863/*
8864 * The following functions are used by 1.2 and 1.3, client and server.
8865 */
8866#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
8867int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
8868 const mbedtls_ssl_ciphersuite_t *ciphersuite,
8869 int recv_endpoint,
8870 mbedtls_ssl_protocol_version tls_version,
8871 uint32_t *flags)
8872{
8873 int ret = 0;
8874 unsigned int usage = 0;
8875 const char *ext_oid;
8876 size_t ext_len;
8877
8878 /*
8879 * keyUsage
8880 */
8881
8882 /* Note: don't guard this with MBEDTLS_SSL_CLI_C because the server wants
8883 * to check what a compliant client will think while choosing which cert
8884 * to send to the client. */
8885#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8886 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
8887 recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
8888 /* TLS 1.2 server part of the key exchange */
8889 switch (ciphersuite->key_exchange) {
8890 case MBEDTLS_KEY_EXCHANGE_RSA:
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02008891 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
8892 break;
8893
8894 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
8895 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
8896 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
8897 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
8898 break;
8899
8900 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
8901 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
8902 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
8903 break;
8904
8905 /* Don't use default: we want warnings when adding new values */
8906 case MBEDTLS_KEY_EXCHANGE_NONE:
8907 case MBEDTLS_KEY_EXCHANGE_PSK:
Manuel Pégourié-Gonnard5398e582024-08-20 12:14:43 +02008908 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
8909 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
8910 usage = 0;
8911 }
8912 } else
8913#endif
8914 {
8915 /* This is either TLS 1.3 authentication, which always uses signatures,
8916 * or 1.2 client auth: rsa_sign and mbedtls_ecdsa_sign are the only
8917 * options we implement, both using signatures. */
8918 (void) tls_version;
8919 (void) ciphersuite;
8920 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
8921 }
8922
8923 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
8924 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
8925 ret = -1;
8926 }
8927
8928 /*
8929 * extKeyUsage
8930 */
8931
8932 if (recv_endpoint == MBEDTLS_SSL_IS_CLIENT) {
8933 ext_oid = MBEDTLS_OID_SERVER_AUTH;
8934 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
8935 } else {
8936 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
8937 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
8938 }
8939
8940 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
8941 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
8942 ret = -1;
8943 }
8944
8945 return ret;
8946}
8947
8948int mbedtls_ssl_verify_certificate(mbedtls_ssl_context *ssl,
8949 int authmode,
8950 mbedtls_x509_crt *chain,
8951 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
8952 void *rs_ctx)
8953{
8954 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
8955 return 0;
8956 }
8957
8958 /*
8959 * Primary check: use the appropriate X.509 verification function
8960 */
8961 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
8962 void *p_vrfy;
8963 if (ssl->f_vrfy != NULL) {
8964 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
8965 f_vrfy = ssl->f_vrfy;
8966 p_vrfy = ssl->p_vrfy;
8967 } else {
8968 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
8969 f_vrfy = ssl->conf->f_vrfy;
8970 p_vrfy = ssl->conf->p_vrfy;
8971 }
8972
8973 int ret = 0;
8974 int have_ca_chain_or_callback = 0;
8975#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8976 if (ssl->conf->f_ca_cb != NULL) {
8977 ((void) rs_ctx);
8978 have_ca_chain_or_callback = 1;
8979
8980 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
8981 ret = mbedtls_x509_crt_verify_with_ca_cb(
8982 chain,
8983 ssl->conf->f_ca_cb,
8984 ssl->conf->p_ca_cb,
8985 ssl->conf->cert_profile,
8986 ssl->hostname,
8987 &ssl->session_negotiate->verify_result,
8988 f_vrfy, p_vrfy);
8989 } else
8990#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
8991 {
8992 mbedtls_x509_crt *ca_chain;
8993 mbedtls_x509_crl *ca_crl;
8994#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8995 if (ssl->handshake->sni_ca_chain != NULL) {
8996 ca_chain = ssl->handshake->sni_ca_chain;
8997 ca_crl = ssl->handshake->sni_ca_crl;
8998 } else
8999#endif
9000 {
9001 ca_chain = ssl->conf->ca_chain;
9002 ca_crl = ssl->conf->ca_crl;
9003 }
9004
9005 if (ca_chain != NULL) {
9006 have_ca_chain_or_callback = 1;
9007 }
9008
9009 ret = mbedtls_x509_crt_verify_restartable(
9010 chain,
9011 ca_chain, ca_crl,
9012 ssl->conf->cert_profile,
9013 ssl->hostname,
9014 &ssl->session_negotiate->verify_result,
9015 f_vrfy, p_vrfy, rs_ctx);
9016 }
9017
9018 if (ret != 0) {
9019 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
9020 }
9021
9022#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
9023 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
9024 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
9025 }
9026#endif
9027
9028 /*
9029 * Secondary checks: always done, but change 'ret' only if it was 0
9030 */
9031
9032 /* With TLS 1.2 and ECC certs, check that the curve used by the
9033 * certificate is on our list of acceptable curves.
9034 *
9035 * With TLS 1.3 this is not needed because the curve is part of the
9036 * signature algorithm (eg ecdsa_secp256r1_sha256) which is checked when
9037 * we validate the signature made with the key associated to this cert.
9038 */
9039#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
9040 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
9041 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
9042 mbedtls_pk_can_do(&chain->pk, MBEDTLS_PK_ECKEY)) {
9043 if (mbedtls_ssl_check_curve(ssl, mbedtls_pk_get_ec_group_id(&chain->pk)) != 0) {
9044 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
9045 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
9046 if (ret == 0) {
9047 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9048 }
9049 }
9050 }
9051#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
9052
9053 /* Check X.509 usage extensions (keyUsage, extKeyUsage) */
9054 if (mbedtls_ssl_check_cert_usage(chain,
9055 ciphersuite_info,
9056 ssl->conf->endpoint,
9057 ssl->tls_version,
9058 &ssl->session_negotiate->verify_result) != 0) {
9059 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
9060 if (ret == 0) {
9061 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
9062 }
9063 }
9064
9065 /* With authmode optional, we want to keep going if the certificate was
9066 * unacceptable, but still fail on other errors (out of memory etc),
9067 * including fatal errors from the f_vrfy callback.
9068 *
9069 * The only acceptable errors are:
9070 * - MBEDTLS_ERR_X509_CERT_VERIFY_FAILED: cert rejected by primary check;
9071 * - MBEDTLS_ERR_SSL_BAD_CERTIFICATE: cert rejected by secondary checks.
9072 * Anything else is a fatal error. */
9073 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
9074 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
9075 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
9076 ret = 0;
9077 }
9078
9079 /* Return a specific error as this is a user error: inconsistent
9080 * configuration - can't verify without trust anchors. */
9081 if (have_ca_chain_or_callback == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
9082 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
9083 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
9084 }
9085
9086 if (ret != 0) {
9087 uint8_t alert;
9088
9089 /* The certificate may have been rejected for several reasons.
9090 Pick one and send the corresponding alert. Which alert to send
9091 may be a subject of debate in some cases. */
9092 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
9093 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
9094 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
9095 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
9096 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
9097 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9098 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
9099 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9100 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
9101 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9102 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
9103 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
9104 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
9105 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
9106 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
9107 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
9108 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
9109 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
9110 } else {
9111 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
9112 }
9113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9114 alert);
9115 }
9116
9117#if defined(MBEDTLS_DEBUG_C)
9118 if (ssl->session_negotiate->verify_result != 0) {
9119 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
9120 (unsigned int) ssl->session_negotiate->verify_result));
9121 } else {
9122 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
9123 }
9124#endif /* MBEDTLS_DEBUG_C */
9125
9126 return ret;
9127}
9128#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
9129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009130#endif /* MBEDTLS_SSL_TLS_C */