blob: bc6dc2740fa3c6a38415db7872c3d5ff08c86f1c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7/*
8 * The SSL 3.0 specification was drafted by Netscape in 1996,
9 * and became an IETF standard in 1999.
10 *
11 * http://wp.netscape.com/eng/ssl3/
12 * http://www.ietf.org/rfc/rfc2246.txt
13 * http://www.ietf.org/rfc/rfc4346.txt
14 */
15
Gilles Peskinedb09ef62020-06-03 01:43:33 +020016#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000019
SimonBd5800b72016-04-26 07:43:27 +010020#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020023#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000024#include "mbedtls/debug.h"
25#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050026#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010027#include "mbedtls/version.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020028#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050032#if defined(MBEDTLS_USE_PSA_CRYPTO)
33#include "mbedtls/psa_util.h"
34#include "psa/crypto.h"
35#endif
36
Janos Follath23bdca02016-10-07 14:47:14 +010037#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020039#endif
40
Gilles Peskineff257152025-02-20 13:57:51 +010041#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010042
Gilles Peskinef33c45f2025-02-12 23:53:25 +010043/* A magic value for `ssl->hostname` indicating that
44 * mbedtls_ssl_set_hostname() has been called with `NULL`.
45 * If mbedtls_ssl_set_hostname() has never been called on `ssl`, then
46 * `ssl->hostname == NULL`. */
47static const char *const ssl_hostname_skip_cn_verification = "";
48
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010049#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
50/** Whether mbedtls_ssl_set_hostname() has been called.
51 *
52 * \param[in] ssl SSL context
53 *
54 * \return \c 1 if mbedtls_ssl_set_hostname() has been called on \p ssl
55 * (including `mbedtls_ssl_set_hostname(ssl, NULL)`),
56 * otherwise \c 0.
57 */
58static int mbedtls_ssl_has_set_hostname_been_called(
59 const mbedtls_ssl_context *ssl)
60{
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010061 return ssl->hostname != NULL;
62}
63#endif
64
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010065const char *mbedtls_ssl_get_hostname_pointer(const mbedtls_ssl_context *ssl)
66{
Gilles Peskinef33c45f2025-02-12 23:53:25 +010067 if (ssl->hostname == ssl_hostname_skip_cn_verification) {
68 return NULL;
69 }
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010070 return ssl->hostname;
71}
72
73static void mbedtls_ssl_free_hostname(mbedtls_ssl_context *ssl)
74{
Gilles Peskinef33c45f2025-02-12 23:53:25 +010075 if (ssl->hostname != NULL &&
76 ssl->hostname != ssl_hostname_skip_cn_verification) {
Gilles Peskine3a2f75d2025-02-12 23:28:48 +010077 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
78 mbedtls_free(ssl->hostname);
79 }
80 ssl->hostname = NULL;
81}
82
Gilles Peskineff257152025-02-20 13:57:51 +010083int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
84{
85 /* Initialize to suppress unnecessary compiler warning */
86 size_t hostname_len = 0;
87
88 /* Check if new hostname is valid before
89 * making any change to current one */
90 if (hostname != NULL) {
91 hostname_len = strlen(hostname);
92
93 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
95 }
96 }
97
98 /* Now it's clear that we will overwrite the old hostname,
99 * so we can free it safely */
Gilles Peskine3a2f75d2025-02-12 23:28:48 +0100100 mbedtls_ssl_free_hostname(ssl);
Gilles Peskineff257152025-02-20 13:57:51 +0100101
Gilles Peskineff257152025-02-20 13:57:51 +0100102 if (hostname == NULL) {
Gilles Peskinef33c45f2025-02-12 23:53:25 +0100103 /* Passing NULL as hostname clears the old one, but leaves a
104 * special marker to indicate that mbedtls_ssl_set_hostname()
105 * has been called. */
106 /* ssl->hostname should be const, but isn't. We won't actually
107 * write to the buffer, so it's ok to cast away the const. */
108 ssl->hostname = (char *) ssl_hostname_skip_cn_verification;
Gilles Peskineff257152025-02-20 13:57:51 +0100109 } else {
110 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
111 if (ssl->hostname == NULL) {
Gilles Peskinef33c45f2025-02-12 23:53:25 +0100112 /* mbedtls_ssl_set_hostname() has been called, but unsuccessfully.
113 * Leave ssl->hostname in the same state as if the function had
114 * not been called, i.e. a null pointer. */
Gilles Peskineff257152025-02-20 13:57:51 +0100115 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
116 }
117
118 memcpy(ssl->hostname, hostname, hostname_len);
119
120 ssl->hostname[hostname_len] = '\0';
121 }
122
123 return 0;
124}
125#endif /* MBEDTLS_X509_CRT_PARSE_C */
126
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200127#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100128
Hanno Beckera0e20d02019-05-15 14:03:01 +0100129#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100130/* Top-level Connection ID API */
131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
133 size_t len,
134 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +0100135{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
138 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
141 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100143 }
144
145 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100146 conf->cid_len = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100148}
149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
151 int enable,
152 unsigned char const *own_cid,
153 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100154{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
156 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
157 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100158
Hanno Beckerca092242019-04-25 16:01:49 +0100159 ssl->negotiate_cid = enable;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (enable == MBEDTLS_SSL_CID_DISABLED) {
161 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
162 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100163 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
165 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (own_cid_len != ssl->conf->cid_len) {
168 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
169 (unsigned) own_cid_len,
170 (unsigned) ssl->conf->cid_len));
171 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100172 }
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100175 /* Truncation is not an issue here because
176 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
177 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100180}
181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
183 int *enabled,
184 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
185 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100186{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
190 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
191 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100192 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100193
Hanno Beckerc5f24222019-05-03 12:54:52 +0100194 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
195 * were used, but client and server requested the empty CID.
196 * This is indistinguishable from not using the CID extension
197 * in the first place. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 if (ssl->transform_in->in_cid_len == 0 &&
199 ssl->transform_in->out_cid_len == 0) {
200 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100201 }
202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100204 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (peer_cid != NULL) {
206 memcpy(peer_cid, ssl->transform_in->out_cid,
207 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100208 }
209 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100210
211 *enabled = MBEDTLS_SSL_CID_ENABLED;
212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100214}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100215#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200220/*
221 * Convert max_fragment_length codes to length.
222 * RFC 6066 says:
223 * enum{
224 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
225 * } MaxFragmentLength;
226 * and we add 0 -> extension unused
227 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200229{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 switch (mfl) {
231 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
232 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
233 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
234 return 512;
235 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
236 return 1024;
237 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
238 return 2048;
239 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
240 return 4096;
241 default:
242 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000243 }
244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
248 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200249{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 mbedtls_ssl_session_free(dst);
251 memcpy(dst, src, sizeof(mbedtls_ssl_session));
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
吴敬辉0f6c6bc2021-11-29 10:46:35 +0800253#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
254 dst->ticket = NULL;
255#endif
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000258
259#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
264 if (dst->peer_cert == NULL) {
265 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
266 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
271 src->peer_cert->raw.len)) != 0) {
272 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273 dst->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200275 }
276 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000277#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000279 dst->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 mbedtls_calloc(1, src->peer_cert_digest_len);
281 if (dst->peer_cert_digest == NULL) {
282 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
283 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
286 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000287 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000288 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000289 }
290#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200293
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200294#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if (src->ticket != NULL) {
296 dst->ticket = mbedtls_calloc(1, src->ticket_len);
297 if (dst->ticket == NULL) {
298 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
299 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200302 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200303#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200306}
307
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200309MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
313 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314 return -1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500316
317 /* We want to copy len_new bytes when downsizing the buffer, and
318 * len_old bytes when upsizing, so we choose the smaller of two sizes,
319 * to fit one buffer into another. Size checks, ensuring that no data is
320 * lost, are done outside of this function. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 memcpy(resized_buffer, *buffer,
322 (len_new < *len_old) ? len_new : *len_old);
323 mbedtls_platform_zeroize(*buffer, *len_old);
324 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500325
326 *buffer = resized_buffer;
327 *len_old = len_new;
328
329 return 0;
330}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
333 size_t in_buf_new_len,
334 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200335{
336 int modified = 0;
337 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
338 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200340 written_in = ssl->in_msg - ssl->in_buf;
341 iv_offset_in = ssl->in_iv - ssl->in_buf;
342 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200344 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 ssl->in_buf_len < in_buf_new_len) {
346 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
347 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
348 } else {
349 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
350 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200351 modified = 1;
352 }
353 }
354 }
355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 written_out = ssl->out_msg - ssl->out_buf;
358 iv_offset_out = ssl->out_iv - ssl->out_buf;
359 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200361 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 ssl->out_buf_len < out_buf_new_len) {
363 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
364 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
365 } else {
366 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
367 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200368 modified = 1;
369 }
370 }
371 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200373 /* Update pointers here to avoid doing it twice. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200375 /* Fields below might not be properly updated with record
376 * splitting or with CID, so they are manually updated here. */
377 ssl->out_msg = ssl->out_buf + written_out;
378 ssl->out_len = ssl->out_buf + len_offset_out;
379 ssl->out_iv = ssl->out_buf + iv_offset_out;
380
381 ssl->in_msg = ssl->in_buf + written_in;
382 ssl->in_len = ssl->in_buf + len_offset_in;
383 ssl->in_iv = ssl->in_buf + iv_offset_in;
384 }
385}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500386#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388/*
389 * Key material generation
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200392MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393static int ssl3_prf(const unsigned char *secret, size_t slen,
394 const char *label,
395 const unsigned char *random, size_t rlen,
396 unsigned char *dstbuf, size_t dlen)
Paul Bakker5f70b252012-09-13 14:23:06 +0000397{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100398 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000399 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200400 mbedtls_md5_context md5;
401 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000402 unsigned char padding[16];
403 unsigned char sha1sum[20];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 ((void) label);
Paul Bakker5f70b252012-09-13 14:23:06 +0000405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100406 mbedtls_md5_init(&md5);
407 mbedtls_sha1_init(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +0200408
Paul Bakker5f70b252012-09-13 14:23:06 +0000409 /*
410 * SSLv3:
411 * block =
412 * MD5( secret + SHA1( 'A' + secret + random ) ) +
413 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
414 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
415 * ...
416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 for (i = 0; i < dlen / 16; i++) {
418 memset(padding, (unsigned char) ('A' + i), 1 + i);
Paul Bakker5f70b252012-09-13 14:23:06 +0000419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 if ((ret = mbedtls_sha1_starts_ret(&sha1)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100421 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 }
423 if ((ret = mbedtls_sha1_update_ret(&sha1, padding, 1 + i)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100424 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 }
426 if ((ret = mbedtls_sha1_update_ret(&sha1, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100427 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 }
429 if ((ret = mbedtls_sha1_update_ret(&sha1, random, rlen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100430 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 }
432 if ((ret = mbedtls_sha1_finish_ret(&sha1, sha1sum)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100433 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000435
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 if ((ret = mbedtls_md5_starts_ret(&md5)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100437 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 }
439 if ((ret = mbedtls_md5_update_ret(&md5, secret, slen)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100440 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 }
442 if ((ret = mbedtls_md5_update_ret(&md5, sha1sum, 20)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100443 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 }
445 if ((ret = mbedtls_md5_finish_ret(&md5, dstbuf + i * 16)) != 0) {
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100446 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 }
Paul Bakker5f70b252012-09-13 14:23:06 +0000448 }
449
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100450exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 mbedtls_md5_free(&md5);
452 mbedtls_sha1_free(&sha1);
Paul Bakker5f70b252012-09-13 14:23:06 +0000453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 mbedtls_platform_zeroize(padding, sizeof(padding));
455 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5f70b252012-09-13 14:23:06 +0000456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 return ret;
Paul Bakker5f70b252012-09-13 14:23:06 +0000458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463static int tls1_prf(const unsigned char *secret, size_t slen,
464 const char *label,
465 const unsigned char *random, size_t rlen,
466 unsigned char *dstbuf, size_t dlen)
Paul Bakker5121ce52009-01-03 21:22:43 +0000467{
Paul Bakker23986e52011-04-24 08:57:21 +0000468 size_t nb, hs;
469 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200470 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300471 unsigned char *tmp;
472 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 const mbedtls_md_info_t *md_info;
475 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 mbedtls_md_init(&md_ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 tmp_len = 20 + strlen(label) + rlen;
481 tmp = mbedtls_calloc(1, tmp_len);
482 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300483 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
484 goto exit;
485 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 hs = (slen + 1) / 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 S1 = secret;
489 S2 = secret + slen - hs;
490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 nb = strlen(label);
492 memcpy(tmp + 20, label, nb);
493 memcpy(tmp + 20 + nb, random, rlen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 nb += rlen;
495
496 /*
497 * First compute P_md5(secret,label+random)[0..dlen]
498 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300500 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
501 goto exit;
502 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300505 goto exit;
506 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 ret = mbedtls_md_hmac_starts(&md_ctx, S1, hs);
509 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100510 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 }
512 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
513 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100514 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 }
516 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
517 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100518 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 for (i = 0; i < dlen; i += 16) {
522 ret = mbedtls_md_hmac_reset(&md_ctx);
523 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100524 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525 }
526 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16 + nb);
527 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100528 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 }
530 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
531 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100532 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 ret = mbedtls_md_hmac_reset(&md_ctx);
536 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100537 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 }
539 ret = mbedtls_md_hmac_update(&md_ctx, 4 + tmp, 16);
540 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100541 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100542 }
543 ret = mbedtls_md_hmac_finish(&md_ctx, 4 + tmp);
544 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100545 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548 k = (i + 16 > dlen) ? dlen % 16 : 16;
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 for (j = 0; j < k; j++) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 }
554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 /*
558 * XOR out with P_sha1(secret,label+random)[0..dlen]
559 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 if ((md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1)) == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300561 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
562 goto exit;
563 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300566 goto exit;
567 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 ret = mbedtls_md_hmac_starts(&md_ctx, S2, hs);
570 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100571 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 }
573 ret = mbedtls_md_hmac_update(&md_ctx, tmp + 20, nb);
574 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100575 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 }
577 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
578 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100579 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100580 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 for (i = 0; i < dlen; i += 20) {
583 ret = mbedtls_md_hmac_reset(&md_ctx);
584 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100585 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 }
587 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20 + nb);
588 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100589 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 }
591 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
592 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100593 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 ret = mbedtls_md_hmac_reset(&md_ctx);
597 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100598 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 }
600 ret = mbedtls_md_hmac_update(&md_ctx, tmp, 20);
601 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100602 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 }
604 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
605 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100606 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100607 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 k = (i + 20 > dlen) ? dlen % 20 : 20;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 for (j = 0; j < k; j++) {
612 dstbuf[i + j] = (unsigned char) (dstbuf[i + j] ^ h_i[j]);
613 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 }
615
Ron Eldor3b350852019-05-07 18:31:49 +0300616exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 mbedtls_platform_zeroize(tmp, tmp_len);
620 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 mbedtls_free(tmp);
623 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500628#if defined(MBEDTLS_USE_PSA_CRYPTO)
k-stachowiak81053a52019-08-17 10:30:28 +0200629
David Horstmann09072662025-03-12 12:03:13 +0000630static int mbedtls_ssl_md_error_from_psa(psa_status_t status)
631{
632 switch (status) {
633 case PSA_ERROR_NOT_SUPPORTED:
634 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
635 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
636 case PSA_ERROR_BUFFER_TOO_SMALL:
637 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
638 case PSA_ERROR_INSUFFICIENT_MEMORY:
639 return MBEDTLS_ERR_MD_ALLOC_FAILED;
640 default:
641 return MBEDTLS_ERR_MD_HW_ACCEL_FAILED;
642 }
643}
644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100645static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
646 psa_key_id_t key,
647 psa_algorithm_t alg,
648 const unsigned char *seed, size_t seed_length,
649 const unsigned char *label, size_t label_length,
650 size_t capacity)
k-stachowiak81053a52019-08-17 10:30:28 +0200651{
652 psa_status_t status;
653
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100654 status = psa_key_derivation_setup(derivation, alg);
655 if (status != PSA_SUCCESS) {
656 return status;
657 }
k-stachowiak81053a52019-08-17 10:30:28 +0200658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
660 status = psa_key_derivation_input_bytes(derivation,
661 PSA_KEY_DERIVATION_INPUT_SEED,
662 seed, seed_length);
663 if (status != PSA_SUCCESS) {
664 return status;
665 }
k-stachowiak81053a52019-08-17 10:30:28 +0200666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if (mbedtls_svc_key_id_is_null(key)) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200668 status = psa_key_derivation_input_bytes(
669 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100670 NULL, 0);
671 } else {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200672 status = psa_key_derivation_input_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Gilles Peskine311f54d2019-09-23 18:19:22 +0200674 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675 if (status != PSA_SUCCESS) {
676 return status;
677 }
k-stachowiak81053a52019-08-17 10:30:28 +0200678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 status = psa_key_derivation_input_bytes(derivation,
680 PSA_KEY_DERIVATION_INPUT_LABEL,
681 label, label_length);
682 if (status != PSA_SUCCESS) {
683 return status;
684 }
685 } else {
686 return PSA_ERROR_NOT_SUPPORTED;
k-stachowiak81053a52019-08-17 10:30:28 +0200687 }
688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 status = psa_key_derivation_set_capacity(derivation, capacity);
690 if (status != PSA_SUCCESS) {
691 return status;
692 }
k-stachowiak81053a52019-08-17 10:30:28 +0200693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100694 return PSA_SUCCESS;
k-stachowiak81053a52019-08-17 10:30:28 +0200695}
696
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100698static int tls_prf_generic(mbedtls_md_type_t md_type,
699 const unsigned char *secret, size_t slen,
700 const char *label,
701 const unsigned char *random, size_t rlen,
702 unsigned char *dstbuf, size_t dlen)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500703{
704 psa_status_t status;
705 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +0200706 psa_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
Janos Follathda6ac012019-08-16 13:47:29 +0100707 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100708 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 if (md_type == MBEDTLS_MD_SHA384) {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500711 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 } else {
Andrzej Kurekc929a822019-01-14 03:51:11 -0500713 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500715
Gilles Peskine311f54d2019-09-23 18:19:22 +0200716 /* Normally a "secret" should be long enough to be impossible to
717 * find by brute force, and in particular should not be empty. But
718 * this PRF is also used to derive an IV, in particular in EAP-TLS,
719 * and for this use case it makes sense to have a 0-length "secret".
720 * Since the key API doesn't allow importing a key of length 0,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200721 * keep master_key=0, which setup_psa_key_derivation() understands
Gilles Peskine311f54d2019-09-23 18:19:22 +0200722 * to mean a 0-length "secret" input. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (slen != 0) {
Gilles Peskine311f54d2019-09-23 18:19:22 +0200724 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100725 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
726 psa_set_key_algorithm(&key_attributes, alg);
727 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Andrzej Kurekc929a822019-01-14 03:51:11 -0500728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 status = psa_import_key(&key_attributes, secret, slen, &master_key);
730 if (status != PSA_SUCCESS) {
731 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
732 }
Gilles Peskine311f54d2019-09-23 18:19:22 +0200733 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 status = setup_psa_key_derivation(&derivation,
736 master_key, alg,
737 random, rlen,
738 (unsigned char const *) label,
739 (size_t) strlen(label),
740 dlen);
741 if (status != PSA_SUCCESS) {
742 psa_key_derivation_abort(&derivation);
743 psa_destroy_key(master_key);
744 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500745 }
746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
748 if (status != PSA_SUCCESS) {
749 psa_key_derivation_abort(&derivation);
750 psa_destroy_key(master_key);
751 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500752 }
753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100754 status = psa_key_derivation_abort(&derivation);
755 if (status != PSA_SUCCESS) {
756 psa_destroy_key(master_key);
757 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500758 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100760 if (!mbedtls_svc_key_id_is_null(master_key)) {
761 status = psa_destroy_key(master_key);
762 }
763 if (status != PSA_SUCCESS) {
764 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
765 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 return 0;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500768}
769
770#else /* MBEDTLS_USE_PSA_CRYPTO */
771
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200772MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773static int tls_prf_generic(mbedtls_md_type_t md_type,
774 const unsigned char *secret, size_t slen,
775 const char *label,
776 const unsigned char *random, size_t rlen,
777 unsigned char *dstbuf, size_t dlen)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000778{
779 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100780 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300781 unsigned char *tmp;
782 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
784 const mbedtls_md_info_t *md_info;
785 mbedtls_md_context_t md_ctx;
Janos Follath865b3eb2019-12-16 11:46:15 +0000786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100788 mbedtls_md_init(&md_ctx);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000789
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
791 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
792 }
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 md_len = mbedtls_md_get_size(md_info);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 tmp_len = md_len + strlen(label) + rlen;
797 tmp = mbedtls_calloc(1, tmp_len);
798 if (tmp == NULL) {
Ron Eldor3b350852019-05-07 18:31:49 +0300799 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
800 goto exit;
801 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 nb = strlen(label);
804 memcpy(tmp + md_len, label, nb);
805 memcpy(tmp + md_len + nb, random, rlen);
Paul Bakker1ef83d62012-04-11 12:09:53 +0000806 nb += rlen;
807
808 /*
809 * Compute P_<hash>(secret, label + random)[0..dlen]
810 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Ron Eldor3b350852019-05-07 18:31:49 +0300812 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
816 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100817 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100818 }
819 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
820 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100821 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 }
823 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
824 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100825 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100826 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100828 for (i = 0; i < dlen; i += md_len) {
829 ret = mbedtls_md_hmac_reset(&md_ctx);
830 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100831 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 }
833 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
834 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100835 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100836 }
837 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
838 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100839 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100840 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100842 ret = mbedtls_md_hmac_reset(&md_ctx);
843 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100844 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 }
846 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
847 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100848 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 }
850 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
851 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100852 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100853 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000854
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100855 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 for (j = 0; j < k; j++) {
Paul Bakker1ef83d62012-04-11 12:09:53 +0000858 dstbuf[i + j] = h_i[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100859 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000860 }
861
Ron Eldor3b350852019-05-07 18:31:49 +0300862exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 mbedtls_md_free(&md_ctx);
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 if (tmp != NULL) {
866 mbedtls_platform_zeroize(tmp, tmp_len);
867 }
Dave Rodgman369f4952022-11-01 16:08:14 +0000868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Paul Bakker1ef83d62012-04-11 12:09:53 +0000870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100871 mbedtls_free(tmp);
Ron Eldor3b350852019-05-07 18:31:49 +0300872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 return ret;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000874}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500875#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200877MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878static int tls_prf_sha256(const unsigned char *secret, size_t slen,
879 const char *label,
880 const unsigned char *random, size_t rlen,
881 unsigned char *dstbuf, size_t dlen)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100882{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100883 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
884 label, random, rlen, dstbuf, dlen);
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000887
Gilles Peskined2d59372021-05-12 22:43:27 +0200888#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200889MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890static int tls_prf_sha384(const unsigned char *secret, size_t slen,
891 const char *label,
892 const unsigned char *random, size_t rlen,
893 unsigned char *dstbuf, size_t dlen)
Paul Bakkerca4ab492012-04-18 14:23:57 +0000894{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
896 label, random, rlen, dstbuf, dlen);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000897}
Gilles Peskined2d59372021-05-12 22:43:27 +0200898#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
904 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *, const unsigned char *, size_t);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200906#endif
Paul Bakker380da532012-04-18 16:10:25 +0000907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100909static void ssl_calc_verify_ssl(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000910static int ssl_calc_finished_ssl(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200911#endif
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100914static void ssl_calc_verify_tls(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000915static int ssl_calc_finished_tls(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
919#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100920static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
921static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000922static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200923#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100924
Gilles Peskined2d59372021-05-12 22:43:27 +0200925#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100926static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
927static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
David Horstmann68014b22025-03-10 14:10:11 +0000928static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Paul Bakker769075d2012-11-24 11:26:46 +0100929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000931
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100932#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100933 defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200934MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100935static int ssl_use_opaque_psk(mbedtls_ssl_context const *ssl)
Hanno Becker7d0a5692018-10-23 15:26:22 +0100936{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 if (ssl->conf->f_psk != NULL) {
Hanno Becker7d0a5692018-10-23 15:26:22 +0100938 /* If we've used a callback to select the PSK,
939 * the static configuration is irrelevant. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
941 return 1;
942 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100944 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100945 }
946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100947 if (!mbedtls_svc_key_id_is_null(ssl->conf->psk_opaque)) {
948 return 1;
949 }
Hanno Becker7d0a5692018-10-23 15:26:22 +0100950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 return 0;
Hanno Becker7d0a5692018-10-23 15:26:22 +0100952}
953#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100954 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100955
Ron Eldorcf280092019-05-14 20:19:13 +0300956#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Ron Eldorcf280092019-05-14 20:19:13 +0300958{
959#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 if (tls_prf == ssl3_prf) {
961 return MBEDTLS_SSL_TLS_PRF_SSL3;
962 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300963#endif
964#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 if (tls_prf == tls1_prf) {
966 return MBEDTLS_SSL_TLS_PRF_TLS1;
967 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300968#endif
969#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +0200970#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 if (tls_prf == tls_prf_sha384) {
972 return MBEDTLS_SSL_TLS_PRF_SHA384;
973 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300974#endif
975#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 if (tls_prf == tls_prf_sha256) {
977 return MBEDTLS_SSL_TLS_PRF_SHA256;
978 } else
Ron Eldorcf280092019-05-14 20:19:13 +0300979#endif
980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100981 return MBEDTLS_SSL_TLS_PRF_NONE;
Ron Eldorcf280092019-05-14 20:19:13 +0300982}
983#endif /* MBEDTLS_SSL_EXPORT_KEYS */
984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
986 const unsigned char *secret, size_t slen,
987 const char *label,
988 const unsigned char *random, size_t rlen,
989 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300990{
991 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 switch (prf) {
Ron Eldor51d3ab52019-05-12 14:54:30 +0300994#if defined(MBEDTLS_SSL_PROTO_SSL3)
995 case MBEDTLS_SSL_TLS_PRF_SSL3:
996 tls_prf = ssl3_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300998#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300999#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1000 case MBEDTLS_SSL_TLS_PRF_TLS1:
1001 tls_prf = tls1_prf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001002 break;
Ron Eldord2f25f72019-05-15 14:54:22 +03001003#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
1004
1005#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001006#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Ron Eldor51d3ab52019-05-12 14:54:30 +03001007 case MBEDTLS_SSL_TLS_PRF_SHA384:
1008 tls_prf = tls_prf_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001009 break;
Gilles Peskined2d59372021-05-12 22:43:27 +02001010#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Ron Eldor51d3ab52019-05-12 14:54:30 +03001011#if defined(MBEDTLS_SHA256_C)
1012 case MBEDTLS_SSL_TLS_PRF_SHA256:
1013 tls_prf = tls_prf_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 break;
Ron Eldord2f25f72019-05-15 14:54:22 +03001015#endif /* MBEDTLS_SHA256_C */
1016#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 default:
1018 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +03001019 }
1020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001021 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +03001022}
1023
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001024/* Type for the TLS PRF */
1025typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
1026 const unsigned char *, size_t,
1027 unsigned char *, size_t);
1028
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001029/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001030 * Populate a transform structure with session keys and all the other
1031 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001032 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001033 * Parameters:
1034 * - [in/out]: transform: structure to populate
1035 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001036 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001037 * - [in] ciphersuite
1038 * - [in] master
1039 * - [in] encrypt_then_mac
1040 * - [in] trunc_hmac
1041 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001042 * - [in] tls_prf: pointer to PRF to use for key derivation
1043 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001044 * - [in] minor_ver: SSL/TLS minor version
1045 * - [in] endpoint: client or server
1046 * - [in] ssl: optionally used for:
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001047 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001048 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1049 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001050 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052static int ssl_populate_transform(mbedtls_ssl_transform *transform,
1053 int ciphersuite,
1054 const unsigned char master[48],
Jarno Lamsac84bd242019-08-16 12:06:56 +03001055#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001057 int encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001058#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001059#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 int trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001061#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1062#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001063#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 int compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001065#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 ssl_tls_prf_t tls_prf,
1067 const unsigned char randbytes[64],
1068 int minor_ver,
1069 unsigned endpoint,
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001070#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 const
Manuel Pégourié-Gonnard7ae6ed42020-03-13 11:28:19 +01001072#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001073 mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001074{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001075 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001076#if defined(MBEDTLS_USE_PSA_CRYPTO)
1077 int psa_fallthrough;
1078#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmannd4f22082022-10-26 18:25:14 +01001079 int do_mbedtls_cipher_setup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 unsigned char keyblk[256];
1081 unsigned char *key1;
1082 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001083 unsigned char *mac_enc;
1084 unsigned char *mac_dec;
sander-visser3888b032020-05-06 21:49:46 +02001085 size_t mac_key_len = 0;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001086 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001087 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001088 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 const mbedtls_cipher_info_t *cipher_info;
1090 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001091
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001092#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1093 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
Gilles Peskinea6f99a12022-04-13 13:24:56 +02001094 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001095 !defined(MBEDTLS_DEBUG_C)
Paul Elliott9c2faca2023-10-18 14:34:46 +01001096 (void) ssl; /* ssl is unused except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001097#endif
1098
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001099 /*
1100 * Some data just needs copying into the structure
1101 */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001102#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1103 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001104 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001105#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001106 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001107
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001108#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Manuel Pégourié-Gonnard96fb0ee2019-07-09 12:54:17 +02001110#endif
1111
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001112 /*
1113 * Get various info structures
1114 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
1116 if (ciphersuite_info == NULL) {
1117 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
1118 ciphersuite));
1119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001120 }
1121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
1123 if (cipher_info == NULL) {
1124 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
1125 ciphersuite_info->cipher));
1126 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001127 }
1128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001129 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
1130 if (md_info == NULL) {
1131 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
1132 (unsigned) ciphersuite_info->mac));
1133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker68884e32013-01-07 18:20:04 +01001134 }
1135
Hanno Beckera0e20d02019-05-15 14:03:01 +01001136#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001137 /* Copy own and peer's CID if the use of the CID
1138 * extension has been negotiated. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
1140 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Hanno Becker8a7f9722019-04-30 13:52:29 +01001141
Hanno Becker05154c32019-05-03 15:23:51 +01001142 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
1144 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
1145 transform->in_cid_len);
Hanno Beckerd1f20352019-05-15 10:21:55 +01001146
1147 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001148 memcpy(transform->out_cid, ssl->handshake->peer_cid,
1149 ssl->handshake->peer_cid_len);
1150 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
1151 transform->out_cid_len);
Hanno Becker4bf74652019-04-26 16:22:27 +01001152 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001154
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001156 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
1159 if (ret != 0) {
1160 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1161 return ret;
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
1165 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
1166 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
1167 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
1168 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 /*
1171 * Determine the appropriate key, IV and MAC length.
1172 */
Paul Bakker68884e32013-01-07 18:20:04 +01001173
Hanno Becker88aaf652017-12-27 08:17:40 +00001174 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001175
Hanno Becker8031d062018-01-03 15:32:31 +00001176#if defined(MBEDTLS_GCM_C) || \
1177 defined(MBEDTLS_CCM_C) || \
1178 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 if (cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001180 cipher_info->mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001182 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001183
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001184 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001185 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001186 transform->taglen =
1187 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001188
Hanno Becker447558d2020-05-28 07:36:33 +01001189 /* All modes haves 96-bit IVs, but the length of the static parts vary
1190 * with mode and version:
1191 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
1192 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001193 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
1194 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
1195 * sequence number).
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001196 */
Paul Bakker68884e32013-01-07 18:20:04 +01001197 transform->ivlen = 12;
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001198#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001199 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001200 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 } else
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001202#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1203 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 if (cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY) {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001205 transform->fixed_ivlen = 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001206 } else {
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001207 transform->fixed_ivlen = 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 }
Hanno Beckerf93c2d72020-05-28 07:39:43 +01001209 }
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001210
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211 /* Minimum length of encrypted record */
1212 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001213 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001215#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1216#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1218 cipher_info->mode == MBEDTLS_MODE_CBC) {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001219 /* Initialize HMAC contexts */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001220 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
1221 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
1222 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001223 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001224 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001225
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226 /* Get MAC length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 mac_key_len = mbedtls_md_get_size(md_info);
Hanno Becker81c7b182017-11-09 18:39:33 +00001228 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001231 /*
1232 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1233 * (rfc 6066 page 13 or rfc 2104 section 4),
1234 * so we only need to adjust the length here.
1235 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 if (trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001238
1239#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1240 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001241 * HMAC implementation which also truncates the key
1242 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001243 mac_key_len = transform->maclen;
1244#endif
1245 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001247
1248 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001249 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001251 /* Minimum length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001252 if (cipher_info->mode == MBEDTLS_MODE_STREAM) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001253 transform->minlen = transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001254 } else {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001255 /*
1256 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001257 * 1. if EtM is in use: one block plus MAC
1258 * otherwise: * first multiple of blocklen greater than maclen
1259 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001263 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001264 + cipher_info->block_size;
1265 } else
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001266#endif
1267 {
1268 transform->minlen = transform->maclen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 + cipher_info->block_size
1270 - transform->maclen % cipher_info->block_size;
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001271 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001274 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1275 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001276 ; /* No need to adjust minlen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1281 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282 transform->minlen += transform->ivlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001283 } else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001284#endif
1285 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001286 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001287 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1288 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001289 }
Paul Bakker68884e32013-01-07 18:20:04 +01001290 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 } else
Hanno Becker8031d062018-01-03 15:32:31 +00001292#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1293 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001294 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1295 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker8031d062018-01-03 15:32:31 +00001296 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001298 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1299 (unsigned) keylen,
1300 (unsigned) transform->minlen,
1301 (unsigned) transform->ivlen,
1302 (unsigned) transform->maclen));
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
1304 /*
1305 * Finally setup the cipher contexts, IVs and MAC secrets.
1306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Becker81c7b182017-11-09 18:39:33 +00001309 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001310 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Paul Bakker68884e32013-01-07 18:20:04 +01001312 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001313 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 /*
1316 * This is not used in TLS v1.1.
1317 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001318 iv_copy_len = (transform->fixed_ivlen) ?
1319 transform->fixed_ivlen : transform->ivlen;
1320 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
1321 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
1322 iv_copy_len);
1323 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_SSL_CLI_C */
1325#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Becker88aaf652017-12-27 08:17:40 +00001327 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001328 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
Hanno Becker81c7b182017-11-09 18:39:33 +00001330 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001331 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001333 /*
1334 * This is not used in TLS v1.1.
1335 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336 iv_copy_len = (transform->fixed_ivlen) ?
1337 transform->fixed_ivlen : transform->ivlen;
1338 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
1339 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
1340 iv_copy_len);
1341 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001343 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001345 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1346 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Hanno Beckerd56ed242018-01-03 15:32:51 +00001349#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1352 if (mac_key_len > sizeof(transform->mac_enc)) {
1353 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001354 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1355 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001356 }
1357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 memcpy(transform->mac_enc, mac_enc, mac_key_len);
1359 memcpy(transform->mac_dec, mac_dec, mac_key_len);
1360 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1362#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1363 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 if (minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Gilles Peskine039fd122018-03-19 19:06:08 +01001365 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1366 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 if (mac_key_len != 0) {
1368 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc,
1369 mac_enc, mac_key_len);
1370 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001371 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372 }
1373 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec,
1374 mac_dec, mac_key_len);
1375 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001376 goto end;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 }
Gilles Peskine039fd122018-03-19 19:06:08 +01001378 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001380#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001381 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Ron Eldore6992702019-05-07 18:27:13 +03001383 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1384 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001385 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001386#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001389 if (mbedtls_ssl_hw_record_init != NULL) {
sander-visser1abe8ee2020-05-06 21:27:14 +02001390 ret = 0;
Paul Bakker05ef8352012-05-08 09:17:57 +00001391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_init()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00001393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001394 if ((ret = mbedtls_ssl_hw_record_init(ssl, key1, key2, keylen,
1395 transform->iv_enc, transform->iv_dec,
1396 iv_copy_len,
1397 mac_enc, mac_dec,
1398 mac_key_len)) != 0) {
1399 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_init", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001400 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1401 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001402 }
1403 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001404#else
1405 ((void) mac_dec);
1406 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001408
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001409#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 if (ssl->conf->f_export_keys != NULL) {
1411 ssl->conf->f_export_keys(ssl->conf->p_export_keys,
1412 master, keyblk,
1413 mac_key_len, keylen,
1414 iv_copy_len);
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001415 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417 if (ssl->conf->f_export_keys_ext != NULL) {
1418 ssl->conf->f_export_keys_ext(ssl->conf->p_export_keys,
1419 master, keyblk,
1420 mac_key_len, keylen,
1421 iv_copy_len,
1422 randbytes + 32,
1423 randbytes,
1424 tls_prf_get_type(tls_prf));
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001425 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001426#endif
1427
David Horstmannd4f22082022-10-26 18:25:14 +01001428 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001429#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001430
1431 /* Only use PSA-based ciphers for TLS-1.2.
1432 * That's relevant at least for TLS-1.0, where
1433 * we assume that mbedtls_cipher_crypt() updates
1434 * the structure field for the IV, which the PSA-based
1435 * implementation currently doesn't. */
1436#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1438 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_enc,
1439 cipher_info, transform->taglen);
1440 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1441 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001442 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001443 }
1444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 if (ret == 0) {
1446 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based encryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001447 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001448 } else {
1449 MBEDTLS_SSL_DEBUG_MSG(1,
1450 (
1451 "Failed to setup PSA-based cipher context for record encryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001452 psa_fallthrough = 1;
1453 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001455 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001457#else
1458 psa_fallthrough = 1;
1459#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001462 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001464#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001465 if (do_mbedtls_cipher_setup &&
1466 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
1467 cipher_info)) != 0) {
1468 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001469 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001470 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001471
David Horstmannd4f22082022-10-26 18:25:14 +01001472 do_mbedtls_cipher_setup = 1;
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 /* Only use PSA-based ciphers for TLS-1.2.
1475 * That's relevant at least for TLS-1.0, where
1476 * we assume that mbedtls_cipher_crypt() updates
1477 * the structure field for the IV, which the PSA-based
1478 * implementation currently doesn't. */
1479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
1481 ret = mbedtls_cipher_setup_psa(&transform->cipher_ctx_dec,
1482 cipher_info, transform->taglen);
1483 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
1484 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup_psa", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 }
1487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001488 if (ret == 0) {
1489 MBEDTLS_SSL_DEBUG_MSG(3, ("Successfully setup PSA-based decryption cipher context"));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490 psa_fallthrough = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 } else {
1492 MBEDTLS_SSL_DEBUG_MSG(1,
1493 (
1494 "Failed to setup PSA-based cipher context for record decryption - fall through to default setup."));
Hanno Beckercb1cc802018-11-17 22:27:38 +00001495 psa_fallthrough = 1;
1496 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 } else {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001498 psa_fallthrough = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001499 }
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500#else
1501 psa_fallthrough = 1;
1502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 if (psa_fallthrough == 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01001505 do_mbedtls_cipher_setup = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001507#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001508 if (do_mbedtls_cipher_setup &&
1509 (ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
1510 cipher_info)) != 0) {
1511 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001512 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001513 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
1516 cipher_info->key_bitlen,
1517 MBEDTLS_ENCRYPT)) != 0) {
1518 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001519 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001520 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001521
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
1523 cipher_info->key_bitlen,
1524 MBEDTLS_DECRYPT)) != 0) {
1525 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001526 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001527 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1531 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
1532 MBEDTLS_PADDING_NONE)) != 0) {
1533 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001534 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001535 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
1538 MBEDTLS_PADDING_NONE)) != 0) {
1539 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Ron Eldore6992702019-05-07 18:27:13 +03001540 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001546 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 if (compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
1549 MBEDTLS_SSL_DEBUG_MSG(3, ("Initializing zlib states"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 memset(&transform->ctx_deflate, 0, sizeof(transform->ctx_deflate));
1552 memset(&transform->ctx_inflate, 0, sizeof(transform->ctx_inflate));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 if (deflateInit(&transform->ctx_deflate,
1555 Z_DEFAULT_COMPRESSION) != Z_OK ||
1556 inflateInit(&transform->ctx_inflate) != Z_OK) {
1557 MBEDTLS_SSL_DEBUG_MSG(1, ("Failed to initialize compression"));
Ron Eldore6992702019-05-07 18:27:13 +03001558 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1559 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001560 }
1561 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001563
Ron Eldore6992702019-05-07 18:27:13 +03001564end:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
1566 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001567}
1568
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001569/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001570 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001571 *
1572 * Inputs:
1573 * - SSL/TLS minor version
1574 * - hash associated with the ciphersuite (only used by TLS 1.2)
1575 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001576 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001577 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1578 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001579MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001580static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
1581 int minor_ver,
1582 mbedtls_md_type_t hash)
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001583{
Gilles Peskinefc9c07f2021-05-12 22:51:53 +02001584#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001585 !(defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001586 (void) hash;
1587#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001588
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001589#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001591 handshake->tls_prf = ssl3_prf;
1592 handshake->calc_verify = ssl_calc_verify_ssl;
1593 handshake->calc_finished = ssl_calc_finished_ssl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001594 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001595#endif
1596#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 if (minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001598 handshake->tls_prf = tls1_prf;
1599 handshake->calc_verify = ssl_calc_verify_tls;
1600 handshake->calc_finished = ssl_calc_finished_tls;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001601 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001602#endif
1603#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02001604#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1606 hash == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001607 handshake->tls_prf = tls_prf_sha384;
1608 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1609 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001610 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001611#endif
1612#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001613 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_3) {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001614 handshake->tls_prf = tls_prf_sha256;
1615 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1616 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 } else
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001618#endif
1619#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1620 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001622 }
1623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 return 0;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001625}
1626
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001627/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001628 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001629 *
1630 * Parameters:
1631 * [in/out] handshake
1632 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001633 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001634 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001635 * [out] master
1636 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1637 * debug: conf->f_dbg, conf->p_dbg
1638 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001639 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001640 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001641MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
1643 unsigned char *master,
1644 const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001645{
Janos Follath865b3eb2019-12-16 11:46:15 +00001646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001647
1648 /* cf. RFC 5246, Section 8.1:
1649 * "The master secret is always exactly 48 bytes in length." */
1650 size_t const master_secret_len = 48;
1651
1652#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1653 unsigned char session_hash[48];
1654#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1655
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001656 /* The label for the KDF used for key expansion.
1657 * This is either "master secret" or "extended master secret"
1658 * depending on whether the Extended Master Secret extension
1659 * is used. */
1660 char const *lbl = "master secret";
1661
1662 /* The salt for the KDF used for key expansion.
1663 * - If the Extended Master Secret extension is not used,
1664 * this is ClientHello.Random + ServerHello.Random
1665 * (see Sect. 8.1 in RFC 5246).
1666 * - If the Extended Master Secret extension is used,
1667 * this is the transcript of the handshake so far.
1668 * (see Sect. 4 in RFC 7627). */
1669 unsigned char const *salt = handshake->randbytes;
1670 size_t salt_len = 64;
1671
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001672#if !defined(MBEDTLS_DEBUG_C) && \
1673 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1674 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001676 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001677 (void) ssl;
1678#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 if (handshake->resume != 0) {
1681 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
1682 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001683 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684
1685#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001686 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001687 lbl = "extended master secret";
1688 salt = session_hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 handshake->calc_verify(ssl, session_hash, &salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
1692 session_hash, salt_len);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001693 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001694#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1695
1696#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001697 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001698 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001699 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001700 ssl_use_opaque_psk(ssl) == 1) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001701 /* Perform PSK-to-MS expansion in a single step. */
1702 psa_status_t status;
1703 psa_algorithm_t alg;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001704 psa_key_id_t psk;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001705 psa_key_derivation_operation_t derivation =
1706 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001707 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 psk = mbedtls_ssl_get_opaque_psk(ssl);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001712
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001713 if (hash_alg == MBEDTLS_MD_SHA384) {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001714 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 } else {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001716 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001717 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 status = setup_psa_key_derivation(&derivation, psk, alg,
1720 salt, salt_len,
1721 (unsigned char const *) lbl,
1722 (size_t) strlen(lbl),
1723 master_secret_len);
1724 if (status != PSA_SUCCESS) {
1725 psa_key_derivation_abort(&derivation);
1726 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001727 }
1728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 status = psa_key_derivation_output_bytes(&derivation,
1730 master,
1731 master_secret_len);
1732 if (status != PSA_SUCCESS) {
1733 psa_key_derivation_abort(&derivation);
1734 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1735 }
1736
1737 status = psa_key_derivation_abort(&derivation);
1738 if (status != PSA_SUCCESS) {
1739 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1740 }
1741 } else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001742#endif
1743 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
1745 lbl, salt, salt_len,
1746 master,
1747 master_secret_len);
1748 if (ret != 0) {
1749 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
1750 return ret;
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001751 }
1752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001753 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
1754 handshake->premaster,
1755 handshake->pmslen);
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 mbedtls_platform_zeroize(handshake->premaster,
1758 sizeof(handshake->premaster));
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001759 }
1760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 return 0;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001762}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001765{
Janos Follath865b3eb2019-12-16 11:46:15 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001767 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1768 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001771
1772 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 ret = ssl_set_handshake_prfs(ssl->handshake,
1774 ssl->minor_ver,
1775 ciphersuite_info->mac);
1776 if (ret != 0) {
1777 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
1778 return ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001779 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001780
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001781 /* Compute master secret if needed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782 ret = ssl_compute_master(ssl->handshake,
1783 ssl->session_negotiate->master,
1784 ssl);
1785 if (ret != 0) {
1786 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
1787 return ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001788 }
1789
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001790 /* Swap the client and server random values:
1791 * - MS derivation wanted client+server (RFC 5246 8.1)
1792 * - key derivation wants server+client (RFC 5246 6.3) */
1793 {
1794 unsigned char tmp[64];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 memcpy(tmp, ssl->handshake->randbytes, 64);
1796 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
1797 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
1798 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001799 }
1800
1801 /* Populate transform structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802 ret = ssl_populate_transform(ssl->transform_negotiate,
1803 ssl->session_negotiate->ciphersuite,
1804 ssl->session_negotiate->master,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001805#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001806#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 ssl->session_negotiate->encrypt_then_mac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001808#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001809#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001810 ssl->session_negotiate->trunc_hmac,
Jarno Lamsac84bd242019-08-16 12:06:56 +03001811#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
1812#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001813#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 ssl->session_negotiate->compression,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001815#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 ssl->handshake->tls_prf,
1817 ssl->handshake->randbytes,
1818 ssl->minor_ver,
1819 ssl->conf->endpoint,
1820 ssl);
1821 if (ret != 0) {
1822 MBEDTLS_SSL_DEBUG_RET(1, "ssl_populate_transform", ret);
1823 return ret;
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001824 }
1825
1826 /* We no longer need Server/ClientHello.random values */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1828 sizeof(ssl->handshake->randbytes));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001829
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001830 /* Allocate compression buffer */
1831#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 if (ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1833 ssl->compress_buf == NULL) {
1834 MBEDTLS_SSL_DEBUG_MSG(3, ("Allocating compression buffer"));
1835 ssl->compress_buf = mbedtls_calloc(1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
1836 if (ssl->compress_buf == NULL) {
1837 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
1838 MBEDTLS_SSL_COMPRESS_BUFFER_LEN));
1839 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001840 }
1841 }
1842#endif
1843
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001844 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 return 0;
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001847}
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850void ssl_calc_verify_ssl(const mbedtls_ssl_context *ssl,
1851 unsigned char *hash,
1852 size_t *hlen)
Paul Bakker5121ce52009-01-03 21:22:43 +00001853{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001854 mbedtls_md5_context md5;
1855 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 unsigned char pad_1[48];
1857 unsigned char pad_2[48];
1858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify ssl"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 mbedtls_md5_init(&md5);
1862 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001863
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001864 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1865 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00001866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001867 memset(pad_1, 0x36, 48);
1868 memset(pad_2, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1871 mbedtls_md5_update_ret(&md5, pad_1, 48);
1872 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001874 mbedtls_md5_starts_ret(&md5);
1875 mbedtls_md5_update_ret(&md5, ssl->session_negotiate->master, 48);
1876 mbedtls_md5_update_ret(&md5, pad_2, 48);
1877 mbedtls_md5_update_ret(&md5, hash, 16);
1878 mbedtls_md5_finish_ret(&md5, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1881 mbedtls_sha1_update_ret(&sha1, pad_1, 40);
1882 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 mbedtls_sha1_starts_ret(&sha1);
1885 mbedtls_sha1_update_ret(&sha1, ssl->session_negotiate->master, 48);
1886 mbedtls_sha1_update_ret(&sha1, pad_2, 40);
1887 mbedtls_sha1_update_ret(&sha1, hash + 16, 20);
1888 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001889
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001890 *hlen = 36;
1891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1893 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001895 mbedtls_md5_free(&md5);
1896 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001897
Paul Bakker380da532012-04-18 16:10:25 +00001898 return;
1899}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903void ssl_calc_verify_tls(const mbedtls_ssl_context *ssl,
1904 unsigned char *hash,
1905 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001906{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001907 mbedtls_md5_context md5;
1908 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001910 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify tls"));
Paul Bakker380da532012-04-18 16:10:25 +00001911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001912 mbedtls_md5_init(&md5);
1913 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001915 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
1916 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker380da532012-04-18 16:10:25 +00001917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 mbedtls_md5_finish_ret(&md5, hash);
1919 mbedtls_sha1_finish_ret(&sha1, hash + 16);
Paul Bakker380da532012-04-18 16:10:25 +00001920
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001921 *hlen = 36;
1922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001923 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1924 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001926 mbedtls_md5_free(&md5);
1927 mbedtls_sha1_free(&sha1);
Paul Bakker5b4af392014-06-26 12:09:34 +02001928
Paul Bakker380da532012-04-18 16:10:25 +00001929 return;
1930}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1934#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001935void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
1936 unsigned char *hash,
1937 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001938{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001939#if defined(MBEDTLS_USE_PSA_CRYPTO)
1940 size_t hash_size;
1941 psa_status_t status;
1942 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1943
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
1945 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
1946 if (status != PSA_SUCCESS) {
1947 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001948 return;
1949 }
1950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
1952 if (status != PSA_SUCCESS) {
1953 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001954 return;
1955 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001956
1957 *hlen = 32;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001958 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
1959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001960#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001961 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001963 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001964
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001965 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Paul Bakker380da532012-04-18 16:10:25 +00001966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
1968 mbedtls_sha256_finish_ret(&sha256, hash);
Paul Bakker380da532012-04-18 16:10:25 +00001969
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001970 *hlen = 32;
1971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
1973 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker380da532012-04-18 16:10:25 +00001974
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05001976#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001977 return;
1978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001980
Gilles Peskined2d59372021-05-12 22:43:27 +02001981#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
1983 unsigned char *hash,
1984 size_t *hlen)
Paul Bakker380da532012-04-18 16:10:25 +00001985{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001986#if defined(MBEDTLS_USE_PSA_CRYPTO)
1987 size_t hash_size;
1988 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001989 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001991 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
1992 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
1993 if (status != PSA_SUCCESS) {
1994 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05001995 return;
1996 }
1997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001998 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
1999 if (status != PSA_SUCCESS) {
2000 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05002001 return;
2002 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002003
2004 *hlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
2006 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05002007#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002008 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00002009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002010 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02002011
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002012 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Paul Bakker380da532012-04-18 16:10:25 +00002013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002014 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
2015 mbedtls_sha512_finish_ret(&sha512, hash);
Paul Bakker5121ce52009-01-03 21:22:43 +00002016
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02002017 *hlen = 48;
2018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
2020 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05002023#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 return;
2025}
Gilles Peskined2d59372021-05-12 22:43:27 +02002026#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002028
Gilles Peskineeccd8882020-03-10 12:19:08 +01002029#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002030int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002031{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002032 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Guilhem Bryantb5f04e42020-04-01 11:23:58 +01002034 const unsigned char *psk = NULL;
Guilhem Bryant61b0fe62020-03-27 11:12:12 +00002035 size_t psk_len = 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 if (mbedtls_ssl_get_psk(ssl, &psk, &psk_len)
2038 == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Guilhem Bryantc5285d82020-03-25 17:08:15 +00002039 /*
2040 * This should never happen because the existence of a PSK is always
2041 * checked before calling this function
2042 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002043 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2044 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002045 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002046
2047 /*
2048 * PMS = struct {
2049 * opaque other_secret<0..2^16-1>;
2050 * opaque psk<0..2^16-1>;
2051 * };
2052 * with "other_secret" depending on the particular key exchange
2053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
2056 if (end - p < 2) {
2057 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2058 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002061 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 if (end < p || (size_t) (end - p) < psk_len) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2065 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 memset(p, 0, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002068 p += psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002069 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2071#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002073 /*
2074 * other_secret already set by the ClientKeyExchange message,
2075 * and is 48 bytes long
2076 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002077 if (end - p < 2) {
2078 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2079 }
Philippe Antoine747fd532018-05-30 09:13:21 +02002080
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002081 *p++ = 0;
2082 *p++ = 48;
2083 p += 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002084 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2086#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002087 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002089 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002091 /* Write length only when we know the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
2093 p + 2, end - (p + 2), &len,
2094 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2095 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
2096 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002099 p += 2 + len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
2102 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2104#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002107 size_t zlen;
2108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
2110 p + 2, end - (p + 2),
2111 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2112 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
2113 return ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002114 }
2115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002117 p += 2 + zlen;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
2120 MBEDTLS_DEBUG_ECDH_Z);
2121 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002123 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2125 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002126 }
2127
2128 /* opaque psk<0..2^16-1>; */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 if (end - p < 2) {
2130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2131 }
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Joe Subbiania651e6f2021-08-23 11:35:25 +01002134 p += 2;
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002136 if (end < p || (size_t) (end - p) < psk_len) {
2137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2138 }
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002140 memcpy(p, psk, psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002141 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002142
2143 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002145 return 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002146}
Gilles Peskineeccd8882020-03-10 12:19:08 +01002147#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002151static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002154int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002155{
2156 /* If renegotiation is not enforced, retransmit until we would reach max
2157 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 if (ssl->conf->renego_max_records < 0) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002159 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002160 unsigned char doublings = 1;
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 while (ratio != 0) {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002163 ++doublings;
2164 ratio >>= 1;
2165 }
2166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if (++ssl->renego_records_seen > doublings) {
2168 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
2169 return 0;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002170 }
2171 }
2172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002174}
2175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002177
Hanno Beckerb9d44792019-02-08 07:19:04 +00002178#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Hanno Beckerb9d44792019-02-08 07:19:04 +00002180{
2181#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 if (session->peer_cert != NULL) {
2183 mbedtls_x509_crt_free(session->peer_cert);
2184 mbedtls_free(session->peer_cert);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002185 session->peer_cert = NULL;
2186 }
2187#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002188 if (session->peer_cert_digest != NULL) {
Hanno Beckerb9d44792019-02-08 07:19:04 +00002189 /* Zeroization is not necessary. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002190 mbedtls_free(session->peer_cert_digest);
Hanno Beckerb9d44792019-02-08 07:19:04 +00002191 session->peer_cert_digest = NULL;
2192 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
2193 session->peer_cert_digest_len = 0;
2194 }
2195#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2196}
2197#endif /* MBEDTLS_X509_CRT_PARSE_C */
2198
Paul Bakker5121ce52009-01-03 21:22:43 +00002199/*
2200 * Handshake functions
2201 */
Gilles Peskineeccd8882020-03-10 12:19:08 +01002202#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02002203/* No certificate support -> dummy functions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002205{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002206 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2207 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002211 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2212 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002213 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 return 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002215 }
2216
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002217 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2218 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002219}
2220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002222{
Hanno Beckere694c3e2017-12-27 21:34:08 +00002223 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2224 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002226 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2229 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002230 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002231 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002232 }
2233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002234 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2235 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236}
Gilles Peskinef9828522017-05-03 12:28:43 +02002237
Gilles Peskineeccd8882020-03-10 12:19:08 +01002238#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02002239/* Some certificate support -> implement write and parse */
2240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00002246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
2247 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002249 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2252 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Johan Pascal4f099262020-09-22 10:59:26 +02002253 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002254 return 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255 }
2256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2259 if (ssl->client_auth == 0) {
2260 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 }
2264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 /*
2267 * If using SSLv3 and got no cert, send an Alert message
2268 * (otherwise an empty Certificate message will be sent).
2269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 if (mbedtls_ssl_own_cert(ssl) == NULL &&
2271 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
2274 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
2275 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 MBEDTLS_SSL_DEBUG_MSG(2, ("got no certificate to send"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002278 goto write_msg;
2279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_SSL_CLI_C */
2283#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2285 if (mbedtls_ssl_own_cert(ssl) == NULL) {
2286 MBEDTLS_SSL_DEBUG_MSG(1, ("got no certificate to send"));
2287 return MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED;
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
2289 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
2294 /*
2295 * 0 . 0 handshake type
2296 * 1 . 3 handshake length
2297 * 4 . 6 length of all certs
2298 * 7 . 9 length of cert. 1
2299 * 10 . n-1 peer certificate
2300 * n . n+2 length of cert. 2
2301 * n+3 . ... upper level cert, etc.
2302 */
2303 i = 7;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 crt = mbedtls_ssl_own_cert(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 while (crt != NULL) {
Paul Bakker5121ce52009-01-03 21:22:43 +00002307 n = crt->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002308 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
2309 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
2310 " > %" MBEDTLS_PRINTF_SIZET,
2311 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2312 return MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 }
2314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
2316 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
2317 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002319 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00002320 i += n; crt = crt->next;
2321 }
2322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002323 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
2324 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
2325 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Paul Bakker5121ce52009-01-03 21:22:43 +00002326
2327 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2329 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002331#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002332write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335 ssl->state++;
2336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
2338 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
2339 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002340 }
2341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002344 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002345}
2346
Hanno Becker84879e32019-01-31 07:44:03 +00002347#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00002348
2349#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002350MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002351static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2352 unsigned char *crt_buf,
2353 size_t crt_buf_len)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002354{
2355 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
2356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002357 if (peer_crt == NULL) {
2358 return -1;
2359 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002361 if (peer_crt->raw.len != crt_buf_len) {
2362 return -1;
2363 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002365 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002366}
Hanno Becker177475a2019-02-05 17:02:46 +00002367#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002368MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002369static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
2370 unsigned char *crt_buf,
2371 size_t crt_buf_len)
Hanno Becker177475a2019-02-05 17:02:46 +00002372{
Janos Follath865b3eb2019-12-16 11:46:15 +00002373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker177475a2019-02-05 17:02:46 +00002374 unsigned char const * const peer_cert_digest =
2375 ssl->session->peer_cert_digest;
2376 mbedtls_md_type_t const peer_cert_digest_type =
2377 ssl->session->peer_cert_digest_type;
2378 mbedtls_md_info_t const * const digest_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002379 mbedtls_md_info_from_type(peer_cert_digest_type);
Hanno Becker177475a2019-02-05 17:02:46 +00002380 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
2381 size_t digest_len;
2382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002383 if (peer_cert_digest == NULL || digest_info == NULL) {
2384 return -1;
2385 }
Hanno Becker177475a2019-02-05 17:02:46 +00002386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002387 digest_len = mbedtls_md_get_size(digest_info);
2388 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
2389 return -1;
2390 }
Hanno Becker177475a2019-02-05 17:02:46 +00002391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
2393 if (ret != 0) {
2394 return -1;
2395 }
Hanno Becker177475a2019-02-05 17:02:46 +00002396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Hanno Becker177475a2019-02-05 17:02:46 +00002398}
2399#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00002400#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002401
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002402/*
2403 * Once the certificate message is read, parse it into a cert chain and
2404 * perform basic checks, but leave actual verification to the caller
2405 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002406MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
2408 mbedtls_x509_crt *chain)
Paul Bakker5121ce52009-01-03 21:22:43 +00002409{
Janos Follath865b3eb2019-12-16 11:46:15 +00002410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002411#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 int crt_cnt = 0;
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002413#endif
Paul Bakker23986e52011-04-24 08:57:21 +00002414 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02002415 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00002416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
2418 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2419 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2420 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
2421 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
2425 ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
2426 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2427 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2428 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2429 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 }
2431
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 i = mbedtls_ssl_hs_hdr_len(ssl);
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00002433
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002437 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002439 if (ssl->in_msg[i] != 0 ||
2440 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
2441 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2442 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2443 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2444 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 }
2446
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002447 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
2448 i += 3;
2449
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002450 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 while (i < ssl->in_hslen) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002452 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 if (i + 3 > ssl->in_hslen) {
2454 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2455 mbedtls_ssl_send_alert_message(ssl,
2456 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2457 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2458 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Philippe Antoine747fd532018-05-30 09:13:21 +02002459 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002460 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
2461 * anything beyond 2**16 ~ 64K. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462 if (ssl->in_msg[i] != 0) {
2463 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2464 mbedtls_ssl_send_alert_message(ssl,
2465 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2466 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2467 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
2469
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002470 /* Read length of the next CRT in the chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 | (unsigned int) ssl->in_msg[i + 2];
2473 i += 3;
2474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475 if (n < 128 || i + n > ssl->in_hslen) {
2476 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
2477 mbedtls_ssl_send_alert_message(ssl,
2478 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2479 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
2480 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 }
2482
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002483 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002484#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002485 if (crt_cnt++ == 0 &&
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002486 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002487 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Hanno Becker46f34d02019-02-08 14:00:04 +00002488 /* During client-side renegotiation, check that the server's
2489 * end-CRTs hasn't changed compared to the initial handshake,
2490 * mitigating the triple handshake attack. On success, reuse
2491 * the original end-CRT instead of parsing it again. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002492 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
2493 if (ssl_check_peer_crt_unchanged(ssl,
2494 &ssl->in_msg[i],
2495 n) != 0) {
2496 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
2497 mbedtls_ssl_send_alert_message(ssl,
2498 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2499 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
2500 return MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002501 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002502
2503 /* Now we can safely free the original chain. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002504 ssl_clear_peer_cert(ssl->session);
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002505 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002506#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
2507
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002508 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00002509#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002510 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002511#else
Hanno Becker353a6f02019-02-26 11:51:34 +00002512 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00002513 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002514 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Hanno Becker0056eab2019-02-08 14:39:16 +00002515#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002516 switch (ret) {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002517 case 0: /*ok*/
2518 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
2519 /* Ignore certificate with an unknown algorithm: maybe a
2520 prior certificate was already trusted. */
2521 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002522
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002523 case MBEDTLS_ERR_X509_ALLOC_FAILED:
2524 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
2525 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002526
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002527 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
2528 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
2529 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02002530
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00002531 default:
2532 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002533crt_parse_der_failed:
2534 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
2535 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
2536 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 }
2538
2539 i += n;
2540 }
2541
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002542 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
2543 return 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002544}
2545
Hanno Becker4a55f632019-02-05 12:49:06 +00002546#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002547MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002548static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Hanno Becker4a55f632019-02-05 12:49:06 +00002549{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002550 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
2551 return -1;
2552 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002553
2554#if defined(MBEDTLS_SSL_PROTO_SSL3)
2555 /*
2556 * Check if the client sent an empty certificate
2557 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
2559 if (ssl->in_msglen == 2 &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002560 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2561 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002562 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
2563 MBEDTLS_SSL_DEBUG_MSG(1, ("SSLv3 client has no certificate"));
2564 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002565 }
2566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002567 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002568 }
2569#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2570
2571#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2572 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002573 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Hanno Becker4a55f632019-02-05 12:49:06 +00002574 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2575 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
2577 MBEDTLS_SSL_DEBUG_MSG(1, ("TLSv1 client has no certificate"));
2578 return 0;
Hanno Becker4a55f632019-02-05 12:49:06 +00002579 }
2580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002581 return -1;
Hanno Becker4a55f632019-02-05 12:49:06 +00002582#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2583 MBEDTLS_SSL_PROTO_TLS1_2 */
2584}
2585#endif /* MBEDTLS_SSL_SRV_C */
2586
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002587/* Check if a certificate message is expected.
2588 * Return either
2589 * - SSL_CERTIFICATE_EXPECTED, or
2590 * - SSL_CERTIFICATE_SKIP
2591 * indicating whether a Certificate message is expected or not.
2592 */
2593#define SSL_CERTIFICATE_EXPECTED 0
2594#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002596static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
2597 int authmode)
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002598{
2599 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002600 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002602 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
2603 return SSL_CERTIFICATE_SKIP;
2604 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002605
2606#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002607 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
2608 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
2609 return SSL_CERTIFICATE_SKIP;
2610 }
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002613 ssl->session_negotiate->verify_result =
2614 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 return SSL_CERTIFICATE_SKIP;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002616 }
2617 }
Hanno Becker84d9d272019-03-01 08:10:46 +00002618#else
2619 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002620#endif /* MBEDTLS_SSL_SRV_C */
2621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 return SSL_CERTIFICATE_EXPECTED;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002623}
2624
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002625static int get_hostname_for_verification(mbedtls_ssl_context *ssl,
2626 const char **hostname)
2627{
2628 if (!mbedtls_ssl_has_set_hostname_been_called(ssl)) {
2629 MBEDTLS_SSL_DEBUG_MSG(1, ("Certificate verification without having set hostname"));
Gilles Peskine551756d2025-02-13 14:39:02 +01002630#if !defined(MBEDTLS_SSL_CLI_ALLOW_WEAK_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME)
2631 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
2632 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2633 return MBEDTLS_ERR_SSL_CERTIFICATE_VERIFICATION_WITHOUT_HOSTNAME;
2634 }
2635#endif
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002636 }
2637
2638 *hostname = mbedtls_ssl_get_hostname_pointer(ssl);
2639 if (*hostname == NULL) {
2640 MBEDTLS_SSL_DEBUG_MSG(2, ("Certificate verification without CN verification"));
2641 }
2642
2643 return 0;
2644}
2645
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002646MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
2648 int authmode,
2649 mbedtls_x509_crt *chain,
2650 void *rs_ctx)
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002651{
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002652 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00002653 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002654 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00002655
Hanno Becker8927c832019-04-03 12:52:50 +01002656 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
2657 void *p_vrfy;
2658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002659 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
2660 return 0;
2661 }
Hanno Becker68636192019-02-05 14:36:34 +00002662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 if (ssl->f_vrfy != NULL) {
2664 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002665 f_vrfy = ssl->f_vrfy;
2666 p_vrfy = ssl->p_vrfy;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 } else {
2668 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Hanno Becker8927c832019-04-03 12:52:50 +01002669 f_vrfy = ssl->conf->f_vrfy;
2670 p_vrfy = ssl->conf->p_vrfy;
2671 }
2672
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002673 const char *hostname = "";
2674 int ret = get_hostname_for_verification(ssl, &hostname);
2675 if (ret != 0) {
2676 MBEDTLS_SSL_DEBUG_RET(1, "get_hostname_for_verification", ret);
2677 return ret;
2678 }
2679
Hanno Becker68636192019-02-05 14:36:34 +00002680 /*
2681 * Main check: verify certificate
2682 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002683#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002684 if (ssl->conf->f_ca_cb != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002685 ((void) rs_ctx);
2686 have_ca_chain = 1;
2687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002688 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03002689 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002690 chain,
2691 ssl->conf->f_ca_cb,
2692 ssl->conf->p_ca_cb,
2693 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002694 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002695 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002696 f_vrfy, p_vrfy);
2697 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002698#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2699 {
2700 mbedtls_x509_crt *ca_chain;
2701 mbedtls_x509_crl *ca_crl;
2702
2703#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 if (ssl->handshake->sni_ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002705 ca_chain = ssl->handshake->sni_ca_chain;
2706 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 } else
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002708#endif
2709 {
2710 ca_chain = ssl->conf->ca_chain;
2711 ca_crl = ssl->conf->ca_crl;
2712 }
2713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 if (ca_chain != NULL) {
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002715 have_ca_chain = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002716 }
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002717
2718 ret = mbedtls_x509_crt_verify_restartable(
2719 chain,
2720 ca_chain, ca_crl,
2721 ssl->conf->cert_profile,
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01002722 hostname,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002723 &ssl->session_negotiate->verify_result,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 f_vrfy, p_vrfy, rs_ctx);
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00002725 }
Hanno Becker68636192019-02-05 14:36:34 +00002726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002727 if (ret != 0) {
2728 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Hanno Becker68636192019-02-05 14:36:34 +00002729 }
2730
Gilles Peskineeccd8882020-03-10 12:19:08 +01002731#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2733 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
2734 }
Hanno Becker68636192019-02-05 14:36:34 +00002735#endif
2736
2737 /*
2738 * Secondary checks: always done, but change 'ret' only if it was 0
2739 */
2740
2741#if defined(MBEDTLS_ECP_C)
2742 {
2743 const mbedtls_pk_context *pk = &chain->pk;
2744
Manuel Pégourié-Gonnard06e1fcd2022-06-16 10:48:06 +02002745 /* If certificate uses an EC key, make sure the curve is OK.
2746 * This is a public key, so it can't be opaque, so can_do() is a good
2747 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002748 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY) &&
2749 mbedtls_ssl_check_curve(ssl, mbedtls_pk_ec(*pk)->grp.id) != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002750 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
2751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002752 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
2753 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002754 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 }
Hanno Becker68636192019-02-05 14:36:34 +00002756 }
2757 }
2758#endif /* MBEDTLS_ECP_C */
2759
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 if (mbedtls_ssl_check_cert_usage(chain,
2761 ciphersuite_info,
2762 !ssl->conf->endpoint,
2763 &ssl->session_negotiate->verify_result) != 0) {
2764 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
2765 if (ret == 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002766 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 }
Hanno Becker68636192019-02-05 14:36:34 +00002768 }
2769
2770 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
2771 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
2772 * with details encoded in the verification flags. All other kinds
2773 * of error codes, including those from the user provided f_vrfy
2774 * functions, are treated as fatal and lead to a failure of
2775 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
2777 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
2778 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE)) {
Hanno Becker68636192019-02-05 14:36:34 +00002779 ret = 0;
2780 }
2781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
2783 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Hanno Becker68636192019-02-05 14:36:34 +00002784 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
2785 }
2786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 if (ret != 0) {
Hanno Becker68636192019-02-05 14:36:34 +00002788 uint8_t alert;
2789
2790 /* The certificate may have been rejected for several reasons.
2791 Pick one and send the corresponding alert. Which alert to send
2792 may be a subject of debate in some cases. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002793 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Hanno Becker68636192019-02-05 14:36:34 +00002794 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002795 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Hanno Becker68636192019-02-05 14:36:34 +00002796 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002798 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002799 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Hanno Becker68636192019-02-05 14:36:34 +00002800 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Hanno Becker68636192019-02-05 14:36:34 +00002802 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002803 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Hanno Becker68636192019-02-05 14:36:34 +00002804 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Hanno Becker68636192019-02-05 14:36:34 +00002806 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002807 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Hanno Becker68636192019-02-05 14:36:34 +00002808 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002809 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Hanno Becker68636192019-02-05 14:36:34 +00002810 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002811 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Hanno Becker68636192019-02-05 14:36:34 +00002812 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002813 } else {
Hanno Becker68636192019-02-05 14:36:34 +00002814 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815 }
2816 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2817 alert);
Hanno Becker68636192019-02-05 14:36:34 +00002818 }
2819
2820#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002821 if (ssl->session_negotiate->verify_result != 0) {
2822 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
2823 (unsigned int) ssl->session_negotiate->verify_result));
2824 } else {
2825 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Hanno Becker68636192019-02-05 14:36:34 +00002826 }
2827#endif /* MBEDTLS_DEBUG_C */
2828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002829 return ret;
Hanno Becker68636192019-02-05 14:36:34 +00002830}
2831
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002832#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002833MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002834static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
2835 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002836{
Janos Follath865b3eb2019-12-16 11:46:15 +00002837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002838 /* Remember digest of the peer's end-CRT. */
2839 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002840 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
2841 if (ssl->session_negotiate->peer_cert_digest == NULL) {
2842 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
2843 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
2844 mbedtls_ssl_send_alert_message(ssl,
2845 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2846 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002848 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002849 }
2850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 ret = mbedtls_md(mbedtls_md_info_from_type(
2852 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
2853 start, len,
2854 ssl->session_negotiate->peer_cert_digest);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002855
2856 ssl->session_negotiate->peer_cert_digest_type =
2857 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
2858 ssl->session_negotiate->peer_cert_digest_len =
2859 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
2860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002861 return ret;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002862}
2863
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002865static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
2866 unsigned char *start, size_t len)
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002867{
2868 unsigned char *end = start + len;
Janos Follath865b3eb2019-12-16 11:46:15 +00002869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002870
2871 /* Make a copy of the peer's raw public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
2873 ret = mbedtls_pk_parse_subpubkey(&start, end,
2874 &ssl->handshake->peer_pubkey);
2875 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002876 /* We should have parsed the public key before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002877 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002878 }
2879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 return 0;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002881}
2882#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
2883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Hanno Becker68636192019-02-05 14:36:34 +00002885{
2886 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00002887 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002888#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
2889 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
2890 ? ssl->handshake->sni_authmode
2891 : ssl->conf->authmode;
2892#else
Johan Pascal4f099262020-09-22 10:59:26 +02002893 const int authmode = ssl->conf->authmode;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002894#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002895 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00002896 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002900 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
2901 if (crt_expected == SSL_CERTIFICATE_SKIP) {
2902 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Hanno Becker6bdfab22019-02-05 13:11:17 +00002903 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002904 }
2905
Gilles Peskineeccd8882020-03-10 12:19:08 +01002906#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907 if (ssl->handshake->ecrs_enabled &&
2908 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002909 chain = ssl->handshake->ecrs_peer_cert;
2910 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002911 goto crt_verify;
2912 }
2913#endif
2914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002916 /* mbedtls_ssl_read_record may have sent an alert already. We
2917 let it decide whether to alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Hanno Becker3dad3112019-02-05 17:19:52 +00002919 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002920 }
2921
Hanno Becker4a55f632019-02-05 12:49:06 +00002922#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Hanno Becker4a55f632019-02-05 12:49:06 +00002924 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00002925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002926 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Hanno Becker6bdfab22019-02-05 13:11:17 +00002927 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 }
Hanno Becker4a55f632019-02-05 12:49:06 +00002929
Hanno Becker6bdfab22019-02-05 13:11:17 +00002930 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00002931 }
2932#endif /* MBEDTLS_SSL_SRV_C */
2933
Hanno Beckerc7bd7802019-02-05 15:37:23 +00002934 /* Clear existing peer CRT structure in case we tried to
2935 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 ssl_clear_peer_cert(ssl->session_negotiate);
Hanno Becker3dad3112019-02-05 17:19:52 +00002937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002938 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
2939 if (chain == NULL) {
2940 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
2941 sizeof(mbedtls_x509_crt)));
2942 mbedtls_ssl_send_alert_message(ssl,
2943 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
2944 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Hanno Becker7a955a02019-02-05 13:08:01 +00002945
Hanno Becker3dad3112019-02-05 17:19:52 +00002946 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
2947 goto exit;
2948 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002949 mbedtls_x509_crt_init(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00002950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 ret = ssl_parse_certificate_chain(ssl, chain);
2952 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002953 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002954 }
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02002955
Gilles Peskineeccd8882020-03-10 12:19:08 +01002956#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002957 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02002958 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002959 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002960
2961crt_verify:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 if (ssl->handshake->ecrs_enabled) {
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002963 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002964 }
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02002965#endif
2966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 ret = ssl_parse_certificate_verify(ssl, authmode,
2968 chain, rs_ctx);
2969 if (ret != 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00002970 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002973#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002974 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002975 unsigned char *crt_start, *pk_start;
2976 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00002977
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002978 /* We parse the CRT chain without copying, so
2979 * these pointers point into the input buffer,
2980 * and are hence still valid after freeing the
2981 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002982
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002983 crt_start = chain->raw.p;
2984 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00002985
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002986 pk_start = chain->pk_raw.p;
2987 pk_len = chain->pk_raw.len;
2988
2989 /* Free the CRT structures before computing
2990 * digest and copying the peer's public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002991 mbedtls_x509_crt_free(chain);
2992 mbedtls_free(chain);
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002993 chain = NULL;
2994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
2996 if (ret != 0) {
Hanno Beckera2747532019-02-06 16:19:04 +00002997 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002998 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00002999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003000 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
3001 if (ret != 0) {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00003002 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003 }
Hanno Beckera2747532019-02-06 16:19:04 +00003004 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00003005#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3006 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00003007 ssl->session_negotiate->peer_cert = chain;
3008 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00003009#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00003010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Hanno Becker6bdfab22019-02-05 13:11:17 +00003013exit:
3014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003015 if (ret == 0) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003016 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 }
Hanno Becker3dad3112019-02-05 17:19:52 +00003018
Gilles Peskineeccd8882020-03-10 12:19:08 +01003019#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Hanno Becker3dad3112019-02-05 17:19:52 +00003021 ssl->handshake->ecrs_peer_cert = chain;
3022 chain = NULL;
3023 }
3024#endif
3025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003026 if (chain != NULL) {
3027 mbedtls_x509_crt_free(chain);
3028 mbedtls_free(chain);
Hanno Becker3dad3112019-02-05 17:19:52 +00003029 }
3030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003032}
Gilles Peskineeccd8882020-03-10 12:19:08 +01003033#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
3036 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Paul Bakker380da532012-04-18 16:10:25 +00003037{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003038 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003040#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3041 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 if (ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker48916f92012-09-16 19:57:18 +00003043 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003044 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskined2d59372021-05-12 22:43:27 +02003047#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003048 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003049 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003050 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003053 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Paul Bakker48916f92012-09-16 19:57:18 +00003054 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003055 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003058 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003059 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003061 }
Paul Bakker380da532012-04-18 16:10:25 +00003062}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003064void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3067 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003068 mbedtls_md5_starts_ret(&ssl->handshake->fin_md5);
3069 mbedtls_sha1_starts_ret(&ssl->handshake->fin_sha1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3072#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003073#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
3075 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003076#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003077 mbedtls_sha256_starts_ret(&ssl->handshake->fin_sha256, 0);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003078#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003079#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003080#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
3083 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003084#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085 mbedtls_sha512_starts_ret(&ssl->handshake->fin_sha512, 1);
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003086#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003089}
3090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003091static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
3092 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3095 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3097 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3100#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003101#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003103#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003105#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003106#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003107#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003108#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003110#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003111 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Paul Bakker769075d2012-11-24 11:26:46 +01003112#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003115}
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3118 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003119static void ssl_update_checksum_md5sha1(mbedtls_ssl_context *ssl,
3120 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003121{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 mbedtls_md5_update_ret(&ssl->handshake->fin_md5, buf, len);
3123 mbedtls_sha1_update_ret(&ssl->handshake->fin_sha1, buf, len);
Paul Bakker380da532012-04-18 16:10:25 +00003124}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003125#endif
Paul Bakker380da532012-04-18 16:10:25 +00003126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3128#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
3130 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003131{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003132#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003133 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003134#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003135 mbedtls_sha256_update_ret(&ssl->handshake->fin_sha256, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003136#endif
Paul Bakker380da532012-04-18 16:10:25 +00003137}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003138#endif
Paul Bakker380da532012-04-18 16:10:25 +00003139
Gilles Peskined2d59372021-05-12 22:43:27 +02003140#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003141static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
3142 const unsigned char *buf, size_t len)
Paul Bakker380da532012-04-18 16:10:25 +00003143{
Andrzej Kurekeb342242019-01-29 09:14:33 -05003144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003146#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003147 mbedtls_sha512_update_ret(&ssl->handshake->fin_sha512, buf, len);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003148#endif
Paul Bakker380da532012-04-18 16:10:25 +00003149}
Paul Bakker769075d2012-11-24 11:26:46 +01003150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153#if defined(MBEDTLS_SSL_PROTO_SSL3)
David Horstmann68014b22025-03-10 14:10:11 +00003154static int ssl_calc_finished_ssl(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003155 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003156{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003157 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003158 mbedtls_md5_context md5;
3159 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 unsigned char padbuf[48];
3162 unsigned char md5sum[16];
3163 unsigned char sha1sum[20];
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003166 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003167 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003168 }
Paul Bakker48916f92012-09-16 19:57:18 +00003169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003170 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished ssl"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003172 mbedtls_md5_init(&md5);
3173 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003175 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3176 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
3178 /*
3179 * SSLv3:
3180 * hash =
3181 * MD5( master + pad2 +
3182 * MD5( handshake + sender + master + pad1 ) )
3183 * + SHA1( master + pad2 +
3184 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 */
3186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003188 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3189 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003193 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3194 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003195#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003197 sender = (from == MBEDTLS_SSL_IS_CLIENT) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02003198 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003200 memset(padbuf, 0x36, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 mbedtls_md5_update_ret(&md5, (const unsigned char *) sender, 4);
3203 mbedtls_md5_update_ret(&md5, session->master, 48);
3204 mbedtls_md5_update_ret(&md5, padbuf, 48);
3205 mbedtls_md5_finish_ret(&md5, md5sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207 mbedtls_sha1_update_ret(&sha1, (const unsigned char *) sender, 4);
3208 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3209 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3210 mbedtls_sha1_finish_ret(&sha1, sha1sum);
Paul Bakker5121ce52009-01-03 21:22:43 +00003211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 memset(padbuf, 0x5C, 48);
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003214 mbedtls_md5_starts_ret(&md5);
3215 mbedtls_md5_update_ret(&md5, session->master, 48);
3216 mbedtls_md5_update_ret(&md5, padbuf, 48);
3217 mbedtls_md5_update_ret(&md5, md5sum, 16);
3218 mbedtls_md5_finish_ret(&md5, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00003219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003220 mbedtls_sha1_starts_ret(&sha1);
3221 mbedtls_sha1_update_ret(&sha1, session->master, 48);
3222 mbedtls_sha1_update_ret(&sha1, padbuf, 40);
3223 mbedtls_sha1_update_ret(&sha1, sha1sum, 20);
3224 mbedtls_sha1_finish_ret(&sha1, buf + 16);
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003226 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, 36);
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003228 mbedtls_md5_free(&md5);
3229 mbedtls_sha1_free(&sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003231 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
3232 mbedtls_platform_zeroize(md5sum, sizeof(md5sum));
3233 mbedtls_platform_zeroize(sha1sum, sizeof(sha1sum));
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003236
3237 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
David Horstmann68014b22025-03-10 14:10:11 +00003242static int ssl_calc_finished_tls(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker5121ce52009-01-03 21:22:43 +00003244{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003245 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003246 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02003247 mbedtls_md5_context md5;
3248 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003249 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003253 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254 }
Paul Bakker48916f92012-09-16 19:57:18 +00003255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003258 mbedtls_md5_init(&md5);
3259 mbedtls_sha1_init(&sha1);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003261 mbedtls_md5_clone(&md5, &ssl->handshake->fin_md5);
3262 mbedtls_sha1_clone(&sha1, &ssl->handshake->fin_sha1);
Paul Bakker5121ce52009-01-03 21:22:43 +00003263
Paul Bakker1ef83d62012-04-11 12:09:53 +00003264 /*
3265 * TLSv1:
3266 * hash = PRF( master, finished_label,
3267 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3268 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if !defined(MBEDTLS_MD5_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 MBEDTLS_SSL_DEBUG_BUF(4, "finished md5 state", (unsigned char *)
3272 md5.state, sizeof(md5.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003273#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#if !defined(MBEDTLS_SHA1_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha1 state", (unsigned char *)
3277 sha1.state, sizeof(sha1.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003278#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Paul Bakker3c2122f2013-06-24 19:03:14 +02003281 ? "client finished"
3282 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 mbedtls_md5_finish_ret(&md5, padbuf);
3285 mbedtls_sha1_finish_ret(&sha1, padbuf + 16);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003287 ssl->handshake->tls_prf(session->master, 48, sender,
3288 padbuf, 36, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003292 mbedtls_md5_free(&md5);
3293 mbedtls_sha1_free(&sha1);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003295 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003297 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003298
3299 return 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3304#if defined(MBEDTLS_SHA256_C)
David Horstmann68014b22025-03-10 14:10:11 +00003305static int ssl_calc_finished_tls_sha256(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003307{
3308 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003309 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003310 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003311#if defined(MBEDTLS_USE_PSA_CRYPTO)
3312 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003313 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003314 psa_status_t status;
3315#else
3316 mbedtls_sha256_context sha256;
3317#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003321 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 }
Paul Bakker48916f92012-09-16 19:57:18 +00003323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003325 ? "client finished"
3326 : "server finished";
3327
3328#if defined(MBEDTLS_USE_PSA_CRYPTO)
3329 sha256_psa = psa_hash_operation_init();
3330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
3334 if (status != PSA_SUCCESS) {
3335 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
David Horstmann09072662025-03-12 12:03:13 +00003336 return mbedtls_ssl_md_error_from_psa(status);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003337 }
3338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
3340 if (status != PSA_SUCCESS) {
3341 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
David Horstmann09072662025-03-12 12:03:13 +00003342 return mbedtls_ssl_md_error_from_psa(status);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003343 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003345#else
3346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 mbedtls_sha256_init(&sha256);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003352
3353 /*
3354 * TLSv1.2:
3355 * hash = PRF( master, finished_label,
3356 * Hash( handshake ) )[0.11]
3357 */
3358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003360 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
3361 sha256.state, sizeof(sha256.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003362#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003364 mbedtls_sha256_finish_ret(&sha256, padbuf);
3365 mbedtls_sha256_free(&sha256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003366#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003368 ssl->handshake->tls_prf(session->master, 48, sender,
3369 padbuf, 32, buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003371 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakker1ef83d62012-04-11 12:09:53 +00003372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003375 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003376
3377 return 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003378}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003380
Gilles Peskined2d59372021-05-12 22:43:27 +02003381#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003382
David Horstmann68014b22025-03-10 14:10:11 +00003383static int ssl_calc_finished_tls_sha384(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003385{
3386 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003387 const char *sender;
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003388 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05003389#if defined(MBEDTLS_USE_PSA_CRYPTO)
3390 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00003391 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05003392 psa_status_t status;
3393#else
3394 mbedtls_sha512_context sha512;
3395#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003398 if (!session) {
Paul Bakker48916f92012-09-16 19:57:18 +00003399 session = ssl->session;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003400 }
Paul Bakker48916f92012-09-16 19:57:18 +00003401
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003402 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003403 ? "client finished"
3404 : "server finished";
3405
3406#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003407 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05003408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003409 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Andrzej Kurekeb342242019-01-29 09:14:33 -05003410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003411 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
3412 if (status != PSA_SUCCESS) {
3413 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
David Horstmann09072662025-03-12 12:03:13 +00003414 return mbedtls_ssl_md_error_from_psa(status);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003415 }
3416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003417 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
3418 if (status != PSA_SUCCESS) {
3419 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
David Horstmann09072662025-03-12 12:03:13 +00003420 return mbedtls_ssl_md_error_from_psa(status);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003421 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003423#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003424 mbedtls_sha512_init(&sha512);
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02003425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003426 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha512);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003429
3430 /*
3431 * TLSv1.2:
3432 * hash = PRF( master, finished_label,
3433 * Hash( handshake ) )[0.11]
3434 */
3435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003437 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
3438 sha512.state, sizeof(sha512.state));
Paul Bakker90995b52013-06-24 19:20:35 +02003439#endif
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003440 /* mbedtls_sha512_finish_ret's output parameter is declared as a
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00003441 * 64-byte buffer, but since we're using SHA-384, we know that the
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003442 * output fits in 48 bytes. This is correct C, but GCC 11.1 warns
3443 * about it.
Rodrigo Dias Corread596ca82020-11-25 00:42:28 -03003444 */
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003445#if defined(__GNUC__) && __GNUC__ >= 11
3446#pragma GCC diagnostic push
3447#pragma GCC diagnostic ignored "-Wstringop-overflow"
3448#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 mbedtls_sha512_finish_ret(&sha512, padbuf);
Gilles Peskinebb66dac2021-05-13 00:00:45 +02003450#if defined(__GNUC__) && __GNUC__ >= 11
3451#pragma GCC diagnostic pop
3452#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003454 mbedtls_sha512_free(&sha512);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003455#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003457 ssl->handshake->tls_prf(session->master, 48, sender,
3458 padbuf, 48, buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Paul Bakkerca4ab492012-04-18 14:23:57 +00003461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003462 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Paul Bakkerca4ab492012-04-18 14:23:57 +00003463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003464 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
David Horstmann68014b22025-03-10 14:10:11 +00003465
3466 return 0;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003467}
Gilles Peskined2d59372021-05-12 22:43:27 +02003468#endif /* MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003471void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003472{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003473 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Paul Bakker48916f92012-09-16 19:57:18 +00003474
3475 /*
3476 * Free our handshake params
3477 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003478 mbedtls_ssl_handshake_free(ssl);
3479 mbedtls_free(ssl->handshake);
Paul Bakker48916f92012-09-16 19:57:18 +00003480 ssl->handshake = NULL;
3481
3482 /*
Shaun Case0e7791f2021-12-20 21:14:10 -08003483 * Free the previous transform and switch in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003484 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003485 if (ssl->transform) {
3486 mbedtls_ssl_transform_free(ssl->transform);
3487 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00003488 }
3489 ssl->transform = ssl->transform_negotiate;
3490 ssl->transform_negotiate = NULL;
3491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003492 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003493}
3494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003496{
3497 int resume = ssl->handshake->resume;
3498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003502 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003504 ssl->renego_records_seen = 0;
3505 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003506#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003507
3508 /*
3509 * Free the previous session and switch in the current one
3510 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 if (ssl->session) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003513 /* RFC 7366 3.1: keep the EtM state */
3514 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003515 ssl->session->encrypt_then_mac;
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003516#endif
3517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 mbedtls_ssl_session_free(ssl->session);
3519 mbedtls_free(ssl->session);
Paul Bakker0a597072012-09-25 21:55:46 +00003520 }
3521 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003522 ssl->session_negotiate = NULL;
3523
Paul Bakker0a597072012-09-25 21:55:46 +00003524 /*
3525 * Add cache entry
3526 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003527 if (ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02003528 ssl->session->id_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 resume == 0) {
3530 if (ssl->conf->f_set_cache(ssl->conf->p_cache, ssl->session) != 0) {
3531 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
3532 }
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003533 }
Paul Bakker0a597072012-09-25 21:55:46 +00003534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003536 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3537 ssl->handshake->flight != NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003538 /* Cancel handshake timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003539 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003540
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003541 /* Keep last flight around in case we need to resend it:
3542 * we need the handshake and transform structures for that */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003543 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
3544 } else
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003545#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003547
Paul Bakker48916f92012-09-16 19:57:18 +00003548 ssl->state++;
3549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003550 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Paul Bakker48916f92012-09-16 19:57:18 +00003551}
3552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003553int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003554{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003555 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Paul Bakker1ef83d62012-04-11 12:09:53 +00003558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003559 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Paul Bakker92be97b2013-01-02 17:30:03 +01003560
David Horstmann68014b22025-03-10 14:10:11 +00003561 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4,
3562 ssl->conf->endpoint);
3563 if (ret != 0) {
3564 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
3565 return ret;
3566 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00003567
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01003568 /*
3569 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
3570 * may define some other value. Currently (early 2016), no defined
3571 * ciphersuite does this (and this is unlikely to change as activity has
3572 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
3573 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 hash_len = (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003577 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003578 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003579#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003580
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3583 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
3585 /*
3586 * In case of session resuming, invert the client and server
3587 * ChangeCipherSpec messages order.
3588 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003589 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003593 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003598 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003599#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003600 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
Paul Bakker48916f92012-09-16 19:57:18 +00003604 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003605 * Switch to our negotiated transform and session parameters for outbound
3606 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003607 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003612 unsigned char i;
3613
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614 /* Remember current epoch settings for resending */
3615 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003616 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003617
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003618 /* Set sequence_number to zero */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003619 memset(ssl->cur_out_ctr + 2, 0, 6);
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003620
3621 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003622 for (i = 2; i > 0; i--) {
3623 if (++ssl->cur_out_ctr[i - 1] != 0) {
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003624 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 }
3626 }
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003627
3628 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003629 if (i == 0) {
3630 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
3631 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003632 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003635 memset(ssl->cur_out_ctr, 0, 8);
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003637 ssl->transform_out = ssl->transform_negotiate;
3638 ssl->session_out = ssl->session_negotiate;
3639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641 if (mbedtls_ssl_hw_record_activate != NULL) {
3642 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND)) != 0) {
3643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
3644 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker07eb38b2012-12-19 14:42:06 +01003645 }
3646 }
3647#endif
3648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3651 mbedtls_ssl_send_flight_completed(ssl);
3652 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003653#endif
3654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003655 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3656 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3657 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 }
3659
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3662 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3663 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
3664 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003665 }
3666#endif
3667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003668 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003671}
3672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003674#define SSL_MAX_HASH_LEN 36
3675#else
3676#define SSL_MAX_HASH_LEN 12
3677#endif
3678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003680{
Janos Follath865b3eb2019-12-16 11:46:15 +00003681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003682 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003683 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003685 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Gilles Peskine622d8042021-12-13 14:38:40 +01003687 /* There is currently no ciphersuite using another length with TLS 1.2 */
3688#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003689 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine622d8042021-12-13 14:38:40 +01003690 hash_len = 36;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 } else
Gilles Peskine622d8042021-12-13 14:38:40 +01003692#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003693 hash_len = 12;
Gilles Peskine622d8042021-12-13 14:38:40 +01003694
David Horstmann2b857292025-03-11 18:13:02 +00003695 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
3696 if (ret != 0) {
3697 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
3698 goto exit;
3699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003701 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
3702 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003703 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 }
3705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
3707 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3708 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3709 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003710 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
3711 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 }
3713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003714 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
3715 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
3716 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3717 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3718 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003719 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3720 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003721 }
3722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003723 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
3724 buf, hash_len) != 0) {
3725 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
3726 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
3727 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003728 ret = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
3729 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 }
3731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003732#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003733 ssl->verify_data_len = hash_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 memcpy(ssl->peer_verify_data, buf, hash_len);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003735#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003737 if (ssl->handshake->resume != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003738#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003741 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003744 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003745 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003747#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003748 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 ssl->state++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003750 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003753 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3754 mbedtls_ssl_recv_flight_completed(ssl);
3755 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003756#endif
3757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003759
Gilles Peskinef91b2e52021-12-13 12:36:15 +01003760exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003761 mbedtls_platform_zeroize(buf, hash_len);
3762 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003763}
3764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003765static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003766{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003769#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
3770 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003771 mbedtls_md5_init(&handshake->fin_md5);
3772 mbedtls_sha1_init(&handshake->fin_sha1);
3773 mbedtls_md5_starts_ret(&handshake->fin_md5);
3774 mbedtls_sha1_starts_ret(&handshake->fin_sha1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003775#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3777#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003778#if defined(MBEDTLS_USE_PSA_CRYPTO)
3779 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003781#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782 mbedtls_sha256_init(&handshake->fin_sha256);
3783 mbedtls_sha256_starts_ret(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003784#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003785#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02003786#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003787#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05003788 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003789 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003790#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003791 mbedtls_sha512_init(&handshake->fin_sha512);
3792 mbedtls_sha512_starts_ret(&handshake->fin_sha512, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003793#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003796
3797 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01003798
3799#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01003800 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 mbedtls_ssl_sig_hash_set_init(&handshake->hash_algs);
Hanno Becker7e5437a2017-04-28 17:15:26 +01003802#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003807#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003809#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02003810#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003811 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02003812#if defined(MBEDTLS_SSL_CLI_C)
3813 handshake->ecjpake_cache = NULL;
3814 handshake->ecjpake_cache_len = 0;
3815#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02003816#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003817
Gilles Peskineeccd8882020-03-10 12:19:08 +01003818#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02003820#endif
3821
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02003822#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
3823 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
3824#endif
Hanno Becker75173122019-02-06 16:18:31 +00003825
3826#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
3827 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00003829#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003830}
3831
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003833{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02003835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003836 mbedtls_cipher_init(&transform->cipher_ctx_enc);
3837 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Paul Bakker84bbeb52014-07-01 14:53:22 +02003838
Hanno Beckerd56ed242018-01-03 15:32:51 +00003839#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 mbedtls_md_init(&transform->md_ctx_enc);
3841 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00003842#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003843}
3844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003846{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003847 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003848}
3849
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003850MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003852{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003853 /* Clear old handshake information if present */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 if (ssl->transform_negotiate) {
3855 mbedtls_ssl_transform_free(ssl->transform_negotiate);
3856 }
3857 if (ssl->session_negotiate) {
3858 mbedtls_ssl_session_free(ssl->session_negotiate);
3859 }
3860 if (ssl->handshake) {
3861 mbedtls_ssl_handshake_free(ssl);
3862 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003863
3864 /*
3865 * Either the pointers are now NULL or cleared properly and can be freed.
3866 * Now allocate missing structures.
3867 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 if (ssl->transform_negotiate == NULL) {
3869 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003870 }
Paul Bakker48916f92012-09-16 19:57:18 +00003871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 if (ssl->session_negotiate == NULL) {
3873 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003874 }
Paul Bakker48916f92012-09-16 19:57:18 +00003875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876 if (ssl->handshake == NULL) {
3877 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003878 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003879#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3880 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
3883 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003884#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003885
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003886 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 if (ssl->handshake == NULL ||
Paul Bakker48916f92012-09-16 19:57:18 +00003888 ssl->transform_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 ssl->session_negotiate == NULL) {
3890 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 mbedtls_free(ssl->handshake);
3893 mbedtls_free(ssl->transform_negotiate);
3894 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003895
3896 ssl->handshake = NULL;
3897 ssl->transform_negotiate = NULL;
3898 ssl->session_negotiate = NULL;
3899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00003901 }
3902
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003903 /* Initialize structures */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 mbedtls_ssl_session_init(ssl->session_negotiate);
3905 mbedtls_ssl_transform_init(ssl->transform_negotiate);
3906 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003909 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003910 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003912 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003913 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003914 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003915 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003916 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02003919 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003920#endif
3921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003922 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003923}
3924
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003925#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003926/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003927MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003928static int ssl_cookie_write_dummy(void *ctx,
3929 unsigned char **p, unsigned char *end,
3930 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003931{
3932 ((void) ctx);
3933 ((void) p);
3934 ((void) end);
3935 ((void) cli_id);
3936 ((void) cli_id_len);
3937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003939}
3940
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003941MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942static int ssl_cookie_check_dummy(void *ctx,
3943 const unsigned char *cookie, size_t cookie_len,
3944 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003945{
3946 ((void) ctx);
3947 ((void) cookie);
3948 ((void) cookie_len);
3949 ((void) cli_id);
3950 ((void) cli_id_len);
3951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003952 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003953}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02003954#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02003955
Paul Bakker5121ce52009-01-03 21:22:43 +00003956/*
3957 * Initialize an SSL context
3958 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003959void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003960{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003962}
3963
3964/*
3965 * Setup an SSL context
3966 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01003967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
3969 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00003970{
Janos Follath865b3eb2019-12-16 11:46:15 +00003971 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00003972 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3973 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003975 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00003976
3977 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003978 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003979 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02003980
3981 /* Set to NULL in case of an error condition */
3982 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02003983
Darryl Greenb33cc762019-11-28 14:29:44 +00003984#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3985 ssl->in_buf_len = in_buf_len;
3986#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003987 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
3988 if (ssl->in_buf == NULL) {
3989 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02003990 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02003991 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10003992 }
3993
Darryl Greenb33cc762019-11-28 14:29:44 +00003994#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3995 ssl->out_buf_len = out_buf_len;
3996#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003997 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
3998 if (ssl->out_buf == NULL) {
3999 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02004000 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02004001 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 }
4003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004004 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02004005
Johan Pascalb62bb512015-12-03 21:56:45 +01004006#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01004008#endif
4009
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02004011 goto error;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02004015
4016error:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004017 mbedtls_free(ssl->in_buf);
4018 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02004019
4020 ssl->conf = NULL;
4021
Darryl Greenb33cc762019-11-28 14:29:44 +00004022#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4023 ssl->in_buf_len = 0;
4024 ssl->out_buf_len = 0;
4025#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02004026 ssl->in_buf = NULL;
4027 ssl->out_buf = NULL;
4028
4029 ssl->in_hdr = NULL;
4030 ssl->in_ctr = NULL;
4031 ssl->in_len = NULL;
4032 ssl->in_iv = NULL;
4033 ssl->in_msg = NULL;
4034
4035 ssl->out_hdr = NULL;
4036 ssl->out_ctr = NULL;
4037 ssl->out_len = NULL;
4038 ssl->out_iv = NULL;
4039 ssl->out_msg = NULL;
4040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004041 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004042}
4043
4044/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004045 * Reset an initialized and used SSL context for re-use while retaining
4046 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004047 *
4048 * If partial is non-zero, keep data in the input buffer and client ID.
4049 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004050 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004051int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00004052{
Janos Follath865b3eb2019-12-16 11:46:15 +00004053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00004054#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4055 size_t in_buf_len = ssl->in_buf_len;
4056 size_t out_buf_len = ssl->out_buf_len;
4057#else
4058 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4059 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4060#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004061
Hanno Becker7e772132018-08-10 12:38:21 +01004062#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
4063 !defined(MBEDTLS_SSL_SRV_C)
4064 ((void) partial);
4065#endif
4066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004068
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004069 /* Cancel any possibly running timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004070 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#if defined(MBEDTLS_SSL_RENEGOTIATION)
4073 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004074 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004075
4076 ssl->verify_data_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
4078 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00004081
Paul Bakker7eb013f2011-10-06 12:37:39 +00004082 ssl->in_offt = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 mbedtls_ssl_reset_in_out_pointers(ssl);
Paul Bakker7eb013f2011-10-06 12:37:39 +00004084
4085 ssl->in_msgtype = 0;
4086 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004088 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004089 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004092 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004093#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004094
4095 ssl->in_hslen = 0;
4096 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004097
4098 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004099
4100 ssl->out_msgtype = 0;
4101 ssl->out_msglen = 0;
4102 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004104 if (ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED) {
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004105 ssl->split_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004106 }
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004107#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004109 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Becker19859472018-08-06 09:40:20 +01004110
Paul Bakker48916f92012-09-16 19:57:18 +00004111 ssl->transform_in = NULL;
4112 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004113
Hanno Becker78640902018-08-13 16:35:15 +01004114 ssl->session_in = NULL;
4115 ssl->session_out = NULL;
4116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004117 memset(ssl->out_buf, 0, out_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004118
David Horstmannd4f22082022-10-26 18:25:14 +01004119 int clear_in_buf = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004120#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004121 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004122 clear_in_buf = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004123 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004124#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125 if (clear_in_buf) {
Hanno Becker4ccbf062018-08-10 11:20:38 +01004126 ssl->in_left = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004127 memset(ssl->in_buf, 0, in_buf_len);
Hanno Becker4ccbf062018-08-10 11:20:38 +01004128 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004131 if (mbedtls_ssl_hw_record_reset != NULL) {
4132 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_reset()"));
4133 if ((ret = mbedtls_ssl_hw_record_reset(ssl)) != 0) {
4134 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_reset", ret);
4135 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004136 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004137 }
4138#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004140 if (ssl->transform) {
4141 mbedtls_ssl_transform_free(ssl->transform);
4142 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004143 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004144 }
Paul Bakker48916f92012-09-16 19:57:18 +00004145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 if (ssl->session) {
4147 mbedtls_ssl_session_free(ssl->session);
4148 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004149 ssl->session = NULL;
4150 }
4151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004153 ssl->alpn_chosen = NULL;
4154#endif
4155
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004156#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmannb5b1ed22022-10-27 13:21:49 +01004157 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01004158#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004159 if (partial != 0) {
David Horstmannd4f22082022-10-26 18:25:14 +01004160 free_cli_id = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004161 }
Hanno Becker4ccbf062018-08-10 11:20:38 +01004162#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 if (free_cli_id) {
4164 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004165 ssl->cli_id = NULL;
4166 ssl->cli_id_len = 0;
4167 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004168#endif
4169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004170 if ((ret = ssl_handshake_init(ssl)) != 0) {
4171 return ret;
4172 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004175}
4176
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004177/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004178 * Reset an initialized and used SSL context for re-use while retaining
4179 * all application-set variables, function pointers and data.
4180 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004182{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004184}
4185
4186/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004187 * SSL set accessors
4188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00004190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004191 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00004192}
4193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004195{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004196 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004197}
4198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004200void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004201{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004202 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004203}
4204#endif
4205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004207void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004208{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004209 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004210}
4211#endif
4212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01004214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004215void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
4216 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01004217{
4218 ssl->disable_datagram_packing = !allow_packing;
4219}
4220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
4222 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004223{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004224 conf->hs_timeout_min = min;
4225 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004226}
4227#endif
4228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004229void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00004230{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004231 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00004232}
4233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
4236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4237 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004238{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004239 conf->f_vrfy = f_vrfy;
4240 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004244void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
4245 int (*f_rng)(void *, unsigned char *, size_t),
4246 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00004247{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01004248 conf->f_rng = f_rng;
4249 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00004250}
4251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004252void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
4253 void (*f_dbg)(void *, int, const char *, int, const char *),
4254 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00004255{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004256 conf->f_dbg = f_dbg;
4257 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00004258}
4259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004260void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
4261 void *p_bio,
4262 mbedtls_ssl_send_t *f_send,
4263 mbedtls_ssl_recv_t *f_recv,
4264 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004265{
4266 ssl->p_bio = p_bio;
4267 ssl->f_send = f_send;
4268 ssl->f_recv = f_recv;
4269 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004270}
4271
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004272#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004273void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02004274{
4275 ssl->mtu = mtu;
4276}
4277#endif
4278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01004280{
4281 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004282}
4283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004284void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
4285 void *p_timer,
4286 mbedtls_ssl_set_timer_t *f_set_timer,
4287 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004288{
4289 ssl->p_timer = p_timer;
4290 ssl->f_set_timer = f_set_timer;
4291 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02004292
4293 /* Make sure we start with no timer running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02004295}
4296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
4299 void *p_cache,
4300 int (*f_get_cache)(void *, mbedtls_ssl_session *),
4301 int (*f_set_cache)(void *, const mbedtls_ssl_session *))
Paul Bakker5121ce52009-01-03 21:22:43 +00004302{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01004303 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004304 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004305 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004309#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004310int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Janos Follath865b3eb2019-12-16 11:46:15 +00004312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004314 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004315 session == NULL ||
4316 ssl->session_negotiate == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004317 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
4318 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004319 }
4320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
4322 session)) != 0) {
4323 return ret;
4324 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004325
Paul Bakker0a597072012-09-25 21:55:46 +00004326 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004328 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004330#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004332void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
4333 const int *ciphersuites)
Paul Bakker5121ce52009-01-03 21:22:43 +00004334{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004335 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
4336 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
4337 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
4338 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004339}
4340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341void mbedtls_ssl_conf_ciphersuites_for_version(mbedtls_ssl_config *conf,
4342 const int *ciphersuites,
4343 int major, int minor)
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004344{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 if (major != MBEDTLS_SSL_MAJOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004346 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004349 if (minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3) {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004350 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 }
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004352
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004353 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004354}
4355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004357void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
4358 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02004359{
4360 conf->cert_profile = profile;
4361}
4362
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004363/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004364MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
4366 mbedtls_x509_crt *cert,
4367 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004368{
niisato8ee24222018-06-25 19:05:48 +09004369 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
4372 if (new_cert == NULL) {
4373 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4374 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004375
niisato8ee24222018-06-25 19:05:48 +09004376 new_cert->cert = cert;
4377 new_cert->key = key;
4378 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004379
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004380 /* Update head is the list was null, else add to the end */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004381 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09004382 *head = new_cert;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004383 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004384 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004386 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004387 }
niisato8ee24222018-06-25 19:05:48 +09004388 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004389 }
4390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004392}
4393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004394int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004395 mbedtls_x509_crt *own_cert,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02004397{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004398 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004399}
4400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004402 mbedtls_x509_crt *ca_chain,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004403 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004404{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004405 conf->ca_chain = ca_chain;
4406 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00004407
4408#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
4409 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4410 * cannot be used together. */
4411 conf->f_ca_cb = NULL;
4412 conf->p_ca_cb = NULL;
4413#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00004414}
Hanno Becker5adaad92019-03-27 16:54:37 +00004415
4416#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004417void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
4418 mbedtls_x509_crt_ca_cb_t f_ca_cb,
4419 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00004420{
4421 conf->f_ca_cb = f_ca_cb;
4422 conf->p_ca_cb = p_ca_cb;
4423
4424 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
4425 * cannot be used together. */
4426 conf->ca_chain = NULL;
4427 conf->ca_crl = NULL;
4428}
4429#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004431
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004432#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
4434 mbedtls_x509_crt *own_cert,
4435 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004437 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
4438 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004439}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
4442 mbedtls_x509_crt *ca_chain,
4443 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02004444{
4445 ssl->handshake->sni_ca_chain = ca_chain;
4446 ssl->handshake->sni_ca_crl = ca_crl;
4447}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004449void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
4450 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02004451{
4452 ssl->handshake->sni_authmode = authmode;
4453}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02004454#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
4455
Hanno Becker8927c832019-04-03 12:52:50 +01004456#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004457void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
4458 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
4459 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01004460{
4461 ssl->f_vrfy = f_vrfy;
4462 ssl->p_vrfy = p_vrfy;
4463}
4464#endif
4465
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004466#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004467/*
4468 * Set EC J-PAKE password for current handshake
4469 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004470int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
4471 const unsigned char *pw,
4472 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004473{
4474 mbedtls_ecjpake_role role;
4475
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004476 if (ssl->handshake == NULL || ssl->conf == NULL) {
4477 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4478 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004481 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004482 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004483 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004485
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004486 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
4487 role,
4488 MBEDTLS_MD_SHA256,
4489 MBEDTLS_ECP_DP_SECP256R1,
4490 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004491}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004492#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02004493
Gilles Peskineeccd8882020-03-10 12:19:08 +01004494#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004497{
4498 /* Remove reference to existing PSK, if any. */
4499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004500 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004501 /* The maintenance of the PSK key slot is the
4502 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02004503 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004504 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004505 /* This and the following branch should never
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00004506 * be taken simultaneously as we maintain the
Hanno Beckera63ac3f2018-11-05 12:47:16 +00004507 * invariant that raw and opaque PSKs are never
4508 * configured simultaneously. As a safeguard,
4509 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004510#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004511 if (conf->psk != NULL) {
4512 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004514 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004515 conf->psk = NULL;
4516 conf->psk_len = 0;
4517 }
4518
4519 /* Remove reference to PSK identity, if any. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 if (conf->psk_identity != NULL) {
4521 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004522 conf->psk_identity = NULL;
4523 conf->psk_identity_len = 0;
4524 }
4525}
4526
Hanno Becker7390c712018-11-15 13:33:04 +00004527/* This function assumes that PSK identity in the SSL config is unset.
4528 * It checks that the provided identity is well-formed and attempts
4529 * to make a copy of it in the SSL config.
4530 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004531MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
4533 unsigned char const *psk_identity,
4534 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004535{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004536 /* Identity len will be encoded on two bytes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004537 if (psk_identity == NULL ||
4538 (psk_identity_len >> 16) != 0 ||
4539 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
4540 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02004541 }
4542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
4544 if (conf->psk_identity == NULL) {
4545 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4546 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004547
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01004548 conf->psk_identity_len = psk_identity_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02004550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02004552}
4553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
4555 const unsigned char *psk, size_t psk_len,
4556 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00004557{
Janos Follath865b3eb2019-12-16 11:46:15 +00004558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004559 /* Remove opaque/raw PSK + PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004560 ssl_conf_remove_psk(conf);
Hanno Becker7390c712018-11-15 13:33:04 +00004561
4562 /* Check and set raw PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004563 if (psk == NULL) {
4564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4565 }
4566 if (psk_len == 0) {
4567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4568 }
4569 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4571 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01004572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4574 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4575 }
Hanno Becker7390c712018-11-15 13:33:04 +00004576 conf->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004577 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00004578
4579 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004580 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
4581 if (ret != 0) {
4582 ssl_conf_remove_psk(conf);
4583 }
Hanno Becker7390c712018-11-15 13:33:04 +00004584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00004586}
4587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004589{
4590#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004591 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Ronald Croncf56a0a2020-08-04 09:51:30 +02004592 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004593 } else
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004594#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 if (ssl->handshake->psk != NULL) {
4596 mbedtls_platform_zeroize(ssl->handshake->psk,
4597 ssl->handshake->psk_len);
4598 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004599 ssl->handshake->psk_len = 0;
lhuang040a2dd6d2024-06-11 12:31:17 -07004600 ssl->handshake->psk = NULL;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004601 }
4602}
4603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004604int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
4605 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004606{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004607 if (psk == NULL || ssl->handshake == NULL) {
4608 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4609 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004611 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
4612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4613 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004615 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004617 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
4618 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
4619 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004620
4621 ssl->handshake->psk_len = psk_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004622 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624 return 0;
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004625}
4626
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
4629 psa_key_id_t psk,
4630 const unsigned char *psk_identity,
4631 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004632{
Janos Follath865b3eb2019-12-16 11:46:15 +00004633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker7390c712018-11-15 13:33:04 +00004634 /* Clear opaque/raw PSK + PSK Identity, if present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004635 ssl_conf_remove_psk(conf);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004636
Hanno Becker7390c712018-11-15 13:33:04 +00004637 /* Check and set opaque PSK */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004638 if (mbedtls_svc_key_id_is_null(psk)) {
4639 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4640 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02004641 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00004642
4643 /* Check and set PSK Identity */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004644 ret = ssl_conf_set_psk_identity(conf, psk_identity,
4645 psk_identity_len);
4646 if (ret != 0) {
4647 ssl_conf_remove_psk(conf);
4648 }
Hanno Becker7390c712018-11-15 13:33:04 +00004649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004650 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004651}
4652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004653int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
4654 psa_key_id_t psk)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004655{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004656 if ((mbedtls_svc_key_id_is_null(psk)) ||
4657 (ssl->handshake == NULL)) {
4658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4659 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004661 ssl_remove_psk(ssl);
Ronald Croncf56a0a2020-08-04 09:51:30 +02004662 ssl->handshake->psk_opaque = psk;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004663 return 0;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01004664}
4665#endif /* MBEDTLS_USE_PSA_CRYPTO */
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
4668 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
4669 size_t),
4670 void *p_psk)
Paul Bakker6db455e2013-09-18 17:29:31 +02004671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004672 conf->f_psk = f_psk;
4673 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004674}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004675#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004676
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004677#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01004678
4679#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004680int mbedtls_ssl_conf_dh_param(mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G)
Paul Bakker5121ce52009-01-03 21:22:43 +00004681{
Janos Follath865b3eb2019-12-16 11:46:15 +00004682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004684 if ((ret = mbedtls_mpi_read_string(&conf->dhm_P, 16, dhm_P)) != 0 ||
4685 (ret = mbedtls_mpi_read_string(&conf->dhm_G, 16, dhm_G)) != 0) {
4686 mbedtls_mpi_free(&conf->dhm_P);
4687 mbedtls_mpi_free(&conf->dhm_G);
4688 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004691 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004692}
Hanno Becker470a8c42017-10-04 15:28:46 +01004693#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004695int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
4696 const unsigned char *dhm_P, size_t P_len,
4697 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01004698{
Janos Follath865b3eb2019-12-16 11:46:15 +00004699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01004700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004701 mbedtls_mpi_free(&conf->dhm_P);
4702 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
4705 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
4706 mbedtls_mpi_free(&conf->dhm_P);
4707 mbedtls_mpi_free(&conf->dhm_G);
4708 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01004709 }
4710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004711 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01004712}
Paul Bakker5121ce52009-01-03 21:22:43 +00004713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004714int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00004715{
Janos Follath865b3eb2019-12-16 11:46:15 +00004716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00004717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004718 mbedtls_mpi_free(&conf->dhm_P);
4719 mbedtls_mpi_free(&conf->dhm_G);
Glenn Straussde081ce2021-12-20 01:43:17 -05004720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if ((ret = mbedtls_mpi_copy(&conf->dhm_P, &dhm_ctx->P)) != 0 ||
4722 (ret = mbedtls_mpi_copy(&conf->dhm_G, &dhm_ctx->G)) != 0) {
4723 mbedtls_mpi_free(&conf->dhm_P);
4724 mbedtls_mpi_free(&conf->dhm_G);
4725 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01004726 }
Paul Bakker1b57b062011-01-06 15:48:19 +00004727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00004729}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02004730#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004731
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004732#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
4733/*
4734 * Set the minimum length for Diffie-Hellman parameters
4735 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004736void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
4737 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02004738{
4739 conf->dhm_min_bitlen = bitlen;
4740}
4741#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
4742
Gilles Peskineeccd8882020-03-10 12:19:08 +01004743#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004744/*
4745 * Set allowed/preferred hashes for handshake signatures
4746 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
4748 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004749{
4750 conf->sig_hashes = hashes;
4751}
Gilles Peskineeccd8882020-03-10 12:19:08 +01004752#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02004753
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02004754#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004755/*
4756 * Set the allowed elliptic curves
4757 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
4759 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004760{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004761 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004762}
Hanno Becker947194e2017-04-07 13:25:49 +01004763#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004764
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01004765#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004766void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
4767 int (*f_sni)(void *, mbedtls_ssl_context *,
4768 const unsigned char *, size_t),
4769 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00004770{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004771 conf->f_sni = f_sni;
4772 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00004773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004774#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004776#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004778{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004779 size_t cur_len, tot_len;
4780 const char **p;
4781
4782 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08004783 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
4784 * MUST NOT be truncated."
4785 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004786 */
4787 tot_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004788 for (p = protos; *p != NULL; p++) {
4789 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004790 tot_len += cur_len;
4791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004792 if ((cur_len == 0) ||
4793 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
4794 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
4795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4796 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004797 }
4798
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004799 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004801 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004802}
4803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004804const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004805{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004809
Johan Pascalb62bb512015-12-03 21:56:45 +01004810#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
4812 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02004813{
4814 conf->dtls_srtp_mki_support = support_mki_value;
4815}
4816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004817int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
4818 unsigned char *mki_value,
4819 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02004820{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
4822 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02004823 }
Ron Eldor591f1622018-01-22 12:30:04 +02004824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
4826 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02004827 }
Ron Eldor591f1622018-01-22 12:30:04 +02004828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004829 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02004830 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02004832}
4833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004834int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
4835 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01004836{
Johan Pascal253d0262020-09-22 13:04:45 +02004837 const mbedtls_ssl_srtp_profile *p;
4838 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004839
Johan Pascal253d0262020-09-22 13:04:45 +02004840 /* check the profiles list: all entry must be valid,
4841 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
4843 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
4844 p++) {
4845 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004846 list_size++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02004848 /* unsupported value, stop parsing and set the size to an error value */
4849 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01004850 }
4851 }
4852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004853 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
4854 conf->dtls_srtp_profile_list = NULL;
4855 conf->dtls_srtp_profile_list_len = 0;
4856 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02004857 }
4858
Johan Pascal9bc97ca2020-09-21 23:44:45 +02004859 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02004860 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01004861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004862 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01004863}
4864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
4866 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01004867{
Johan Pascal2258a4f2020-10-28 13:53:09 +01004868 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
4869 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004871 dtls_srtp_info->mki_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01004873 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
4875 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01004876 }
Johan Pascalb62bb512015-12-03 21:56:45 +01004877}
Johan Pascalb62bb512015-12-03 21:56:45 +01004878#endif /* MBEDTLS_SSL_DTLS_SRTP */
4879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00004881{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004882 conf->max_major_ver = major;
4883 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00004884}
4885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00004887{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004888 conf->min_major_ver = major;
4889 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004890}
4891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004892#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004893void mbedtls_ssl_conf_fallback(mbedtls_ssl_config *conf, char fallback)
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004894{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01004895 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004896}
4897#endif
4898
Janos Follath088ce432017-04-10 12:42:31 +01004899#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
4901 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01004902{
4903 conf->cert_req_ca_list = cert_req_ca_list;
4904}
4905#endif
4906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004907#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004908void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004909{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004910 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004911}
4912#endif
4913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004915void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004917 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004918}
4919#endif
4920
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004921#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004922void mbedtls_ssl_conf_arc4_support(mbedtls_ssl_config *conf, char arc4)
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004923{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004924 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004925}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02004926#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004928#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004930{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
4932 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
4933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004934 }
4935
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01004936 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004937
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004938 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004939}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004943void mbedtls_ssl_conf_truncated_hmac(mbedtls_ssl_config *conf, int truncate)
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004944{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004945 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004946}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950void mbedtls_ssl_conf_cbc_record_splitting(mbedtls_ssl_config *conf, char split)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004951{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01004952 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004953}
4954#endif
4955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004956void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00004957{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004958 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00004959}
4960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004961#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004963{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004964 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004965}
4966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004967void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02004969 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004970}
4971
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004972void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
4973 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004974{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004977#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004980#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004981void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004982{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01004983 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004984}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004985#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02004986
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004987#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004988void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
4989 mbedtls_ssl_ticket_write_t *f_ticket_write,
4990 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
4991 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02004992{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02004993 conf->f_ticket_write = f_ticket_write;
4994 conf->f_ticket_parse = f_ticket_parse;
4995 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004996}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004999
Robert Cragie4feb7ae2015-10-02 13:33:37 +01005000#if defined(MBEDTLS_SSL_EXPORT_KEYS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005001void mbedtls_ssl_conf_export_keys_cb(mbedtls_ssl_config *conf,
5002 mbedtls_ssl_export_keys_t *f_export_keys,
5003 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01005004{
5005 conf->f_export_keys = f_export_keys;
5006 conf->p_export_keys = p_export_keys;
5007}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03005008
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005009void mbedtls_ssl_conf_export_keys_ext_cb(mbedtls_ssl_config *conf,
5010 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
5011 void *p_export_keys)
Ron Eldorf5cc10d2019-05-07 18:33:40 +03005012{
5013 conf->f_export_keys_ext = f_export_keys_ext;
5014 conf->p_export_keys = p_export_keys;
5015}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01005016#endif
5017
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005018#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005019void mbedtls_ssl_conf_async_private_cb(
5020 mbedtls_ssl_config *conf,
5021 mbedtls_ssl_async_sign_t *f_async_sign,
5022 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
5023 mbedtls_ssl_async_resume_t *f_async_resume,
5024 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005025 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005026{
5027 conf->f_async_sign_start = f_async_sign;
5028 conf->f_async_decrypt_start = f_async_decrypt;
5029 conf->f_async_resume = f_async_resume;
5030 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005031 conf->p_async_config_data = async_config_data;
5032}
5033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02005035{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005036 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02005037}
5038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005040{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041 if (ssl->handshake == NULL) {
5042 return NULL;
5043 } else {
5044 return ssl->handshake->user_async_ctx;
5045 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005046}
5047
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005048void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
5049 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02005052 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005053 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005054}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02005055#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01005056
Paul Bakker5121ce52009-01-03 21:22:43 +00005057/*
5058 * SSL get accessors
5059 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005061{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 if (ssl->session != NULL) {
5063 return ssl->session->verify_result;
5064 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005066 if (ssl->session_negotiate != NULL) {
5067 return ssl->session_negotiate->verify_result;
5068 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00005071}
5072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005073const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00005074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075 if (ssl == NULL || ssl->session == NULL) {
5076 return NULL;
5077 }
Paul Bakker926c8e42013-03-06 10:23:34 +01005078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005079 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00005080}
5081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00005083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005085 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5086 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 return "DTLSv1.0";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005091 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005092
5093 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005095 }
5096 }
5097#endif
5098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005099 switch (ssl->minor_ver) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005100 case MBEDTLS_SSL_MINOR_VERSION_0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005101 return "SSLv3.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 case MBEDTLS_SSL_MINOR_VERSION_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104 return "TLSv1.0";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 case MBEDTLS_SSL_MINOR_VERSION_2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005107 return "TLSv1.1";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 case MBEDTLS_SSL_MINOR_VERSION_3:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005110 return "TLSv1.2";
Paul Bakker1ef83d62012-04-11 12:09:53 +00005111
Paul Bakker43ca69c2011-01-15 17:35:19 +00005112 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005113 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00005114 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005115}
5116
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005117#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005118size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005119{
5120 size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
5121 size_t read_mfl;
5122
5123 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005124 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5125 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
5126 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005127 }
5128
5129 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 if (ssl->session_out != NULL) {
5131 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
5132 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005133 max_len = read_mfl;
5134 }
5135 }
5136
5137 // During a handshake, use the value being negotiated
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005138 if (ssl->session_negotiate != NULL) {
5139 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
5140 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005141 max_len = read_mfl;
5142 }
5143 }
5144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005146}
5147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005148size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005149{
5150 size_t max_len;
5151
5152 /*
5153 * Assume mfl_code is correct since it was checked when set
5154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005156
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005157 /* Check if a smaller max length was negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005158 if (ssl->session_out != NULL &&
5159 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
5160 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005161 }
5162
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005163 /* During a handshake, use the value being negotiated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 if (ssl->session_negotiate != NULL &&
5165 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
5166 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02005167 }
5168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005169 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005170}
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005171
5172#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173size_t mbedtls_ssl_get_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005174{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005175 return mbedtls_ssl_get_output_max_frag_len(ssl);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04005176}
5177#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02005178#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5179
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005182{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005183 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5185 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
5186 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
5187 return 0;
5188 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04005189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
5191 return ssl->mtu;
5192 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005194 if (ssl->mtu == 0) {
5195 return ssl->handshake->mtu;
5196 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005198 return ssl->mtu < ssl->handshake->mtu ?
5199 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005200}
5201#endif /* MBEDTLS_SSL_PROTO_DTLS */
5202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005203int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005204{
5205 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
5206
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02005207#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5208 !defined(MBEDTLS_SSL_PROTO_DTLS)
5209 (void) ssl;
5210#endif
5211
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005213 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005215 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005216 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005217 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005218#endif
5219
5220#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005221 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
5222 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
5223 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005224 const size_t overhead = (size_t) ret;
5225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005226 if (ret < 0) {
5227 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005228 }
5229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005230 if (mtu <= overhead) {
5231 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
5232 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5233 }
5234
5235 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005236 max_len = mtu - overhead;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005237 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005238 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02005239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005240
Hanno Becker0defedb2018-08-10 12:35:02 +01005241#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
5242 !defined(MBEDTLS_SSL_PROTO_DTLS)
5243 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005244#endif
5245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005247}
5248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005250const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00005251{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005252 if (ssl == NULL || ssl->session == NULL) {
5253 return NULL;
5254 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00005255
Hanno Beckere6824572019-02-07 13:18:46 +00005256#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005257 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00005258#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005259 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00005260#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005261}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005265int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
5266 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005267{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005268 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005269 dst == NULL ||
5270 ssl->session == NULL ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
5272 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005273 }
5274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005275 return mbedtls_ssl_session_copy(dst, ssl->session);
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005279const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005280{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005281 if (ssl == NULL) {
5282 return NULL;
5283 }
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005285 return ssl->session;
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02005286}
5287
Paul Bakker5121ce52009-01-03 21:22:43 +00005288/*
Hanno Beckera835da52019-05-16 12:39:07 +01005289 * Define ticket header determining Mbed TLS version
5290 * and structure of the ticket.
5291 */
5292
Hanno Becker94ef3b32019-05-16 12:50:45 +01005293/*
Hanno Becker50b59662019-05-28 14:30:45 +01005294 * Define bitflag determining compile-time settings influencing
5295 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01005296 */
5297
Hanno Becker50b59662019-05-28 14:30:45 +01005298#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01005299#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01005300#else
Hanno Becker3e088662019-05-29 11:10:18 +01005301#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005302#endif /* MBEDTLS_HAVE_TIME */
5303
5304#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01005305#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005306#else
Hanno Becker3e088662019-05-29 11:10:18 +01005307#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005308#endif /* MBEDTLS_X509_CRT_PARSE_C */
5309
David Horstmanneb77b6f2024-02-13 17:53:35 +00005310#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
David Horstmann11def972024-03-01 11:20:32 +00005311#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 1
David Horstmanneb77b6f2024-02-13 17:53:35 +00005312#else
David Horstmann11def972024-03-01 11:20:32 +00005313#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT 0
David Horstmanneb77b6f2024-02-13 17:53:35 +00005314#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5315
Hanno Becker94ef3b32019-05-16 12:50:45 +01005316#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01005317#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005318#else
Hanno Becker3e088662019-05-29 11:10:18 +01005319#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005320#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
5321
5322#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01005323#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005324#else
Hanno Becker3e088662019-05-29 11:10:18 +01005325#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005326#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
5327
5328#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005329#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005330#else
Hanno Becker3e088662019-05-29 11:10:18 +01005331#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005332#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
5333
5334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01005335#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01005336#else
Hanno Becker3e088662019-05-29 11:10:18 +01005337#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01005338#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
5339
Hanno Becker94ef3b32019-05-16 12:50:45 +01005340#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5341#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
5342#else
5343#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
5344#endif /* MBEDTLS_SSL_SESSION_TICKETS */
5345
Hanno Becker3e088662019-05-29 11:10:18 +01005346#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
5347#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
5348#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
5349#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
5350#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
5351#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
5352#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
David Horstmann11def972024-03-01 11:20:32 +00005353#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT 7
Hanno Becker3e088662019-05-29 11:10:18 +01005354
Hanno Becker50b59662019-05-28 14:30:45 +01005355#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 ((uint16_t) ( \
5357 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
5358 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
5359 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
5360 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
5361 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
5362 (SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << \
5363 SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT) | \
5364 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
David Horstmanneb77b6f2024-02-13 17:53:35 +00005365 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT) | \
David Horstmannf5a6fa22024-03-01 12:31:35 +00005366 (SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT << \
5367 SSL_SERIALIZED_SESSION_CONFIG_KEEP_PEER_CRT_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01005368
Chien Wongb6d57932024-02-07 21:48:12 +08005369static const unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01005370 MBEDTLS_VERSION_MAJOR,
5371 MBEDTLS_VERSION_MINOR,
5372 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005373 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
5374 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01005375};
Hanno Beckera835da52019-05-16 12:39:07 +01005376
5377/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005378 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005379 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005380 *
Hanno Becker50b59662019-05-28 14:30:45 +01005381 * opaque mbedtls_version[3]; // major, minor, patch
5382 * opaque session_format[2]; // version-specific 16-bit field determining
5383 * // the format of the remaining
5384 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01005385 *
5386 * Note: When updating the format, remember to keep
5387 * these version+format bytes.
5388 *
Hanno Beckerbe34e8e2019-06-04 09:43:16 +01005389 * // In this version, `session_format` determines
5390 * // the setting of those compile-time
5391 * // configuration options which influence
Hanno Becker50b59662019-05-28 14:30:45 +01005392 * // the structure of mbedtls_ssl_session.
David Horstmann363db772024-03-01 12:11:24 +00005393 * #if defined(MBEDTLS_HAVE_TIME)
5394 * uint64 start_time;
5395 * #endif
5396 * uint8 ciphersuite[2]; // defined by the standard
5397 * uint8 compression; // 0 or 1
5398 * uint8 session_id_len; // at most 32
5399 * opaque session_id[32];
5400 * opaque master[48]; // fixed length in the standard
5401 * uint32 verify_result;
David Horstmannfc8cacf2024-03-04 16:37:02 +00005402 * #if defined(MBEDTLS_X509_CRT_PARSE_C)
David Horstmann363db772024-03-01 12:11:24 +00005403 * #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5404 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
5405 * #else
5406 * uint8 peer_cert_digest_type;
5407 * opaque peer_cert_digest<0..2^8-1>
5408 * #endif
David Horstmannfc8cacf2024-03-04 16:37:02 +00005409 * #endif
5410 * #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
David Horstmann363db772024-03-01 12:11:24 +00005411 * opaque ticket<0..2^24-1>; // length 0 means no ticket
5412 * uint32 ticket_lifetime;
5413 * #endif
5414 * #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5415 * uint8 mfl_code; // up to 255 according to standard
5416 * #endif
5417 * #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5418 * uint8 trunc_hmac; // 0 or 1
5419 * #endif
5420 * #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5421 * uint8 encrypt_then_mac; // 0 or 1
5422 * #endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005423 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005424 * The order is the same as in the definition of the structure, except
5425 * verify_result is put before peer_cert so that all mandatory fields come
5426 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005427 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429static int ssl_session_save(const mbedtls_ssl_session *session,
5430 unsigned char omit_header,
5431 unsigned char *buf,
5432 size_t buf_len,
5433 size_t *olen)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005434{
5435 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005436 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005437#if defined(MBEDTLS_HAVE_TIME)
5438 uint64_t start;
5439#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005440#if defined(MBEDTLS_X509_CRT_PARSE_C)
5441#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5442 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005443#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5444#endif /* MBEDTLS_X509_CRT_PARSE_C */
5445
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005447 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005448 /*
5449 * Add version identifier
5450 */
5451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 used += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005454 if (used <= buf_len) {
5455 memcpy(p, ssl_serialized_session_header,
5456 sizeof(ssl_serialized_session_header));
5457 p += sizeof(ssl_serialized_session_header);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005458 }
Hanno Beckera835da52019-05-16 12:39:07 +01005459 }
5460
5461 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005462 * Time
5463 */
5464#if defined(MBEDTLS_HAVE_TIME)
5465 used += 8;
5466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005468 start = (uint64_t) session->start;
5469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005471 p += 8;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005472 }
5473#endif /* MBEDTLS_HAVE_TIME */
5474
5475 /*
5476 * Basic mandatory fields
5477 */
5478 used += 2 /* ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 + 1 /* compression */
5480 + 1 /* id_len */
5481 + sizeof(session->id)
5482 + sizeof(session->master)
5483 + 4; /* verify_result */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 if (used <= buf_len) {
5486 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005487 p += 2;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 *p++ = MBEDTLS_BYTE_0(session->compression);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 *p++ = MBEDTLS_BYTE_0(session->id_len);
5492 memcpy(p, session->id, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005493 p += 32;
5494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005495 memcpy(p, session->master, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005496 p += 48;
5497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005499 p += 4;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005500 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005501
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005502 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005503 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005504 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005505#if defined(MBEDTLS_X509_CRT_PARSE_C)
5506#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005507 if (session->peer_cert == NULL) {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005508 cert_len = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005509 } else {
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005510 cert_len = session->peer_cert->raw.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005511 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005512
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005513 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005515 if (used <= buf_len) {
5516 *p++ = MBEDTLS_BYTE_2(cert_len);
5517 *p++ = MBEDTLS_BYTE_1(cert_len);
5518 *p++ = MBEDTLS_BYTE_0(cert_len);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 if (session->peer_cert != NULL) {
5521 memcpy(p, session->peer_cert->raw.p, cert_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005522 p += cert_len;
5523 }
5524 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005525#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 if (session->peer_cert_digest != NULL) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005527 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005528 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005529 *p++ = (unsigned char) session->peer_cert_digest_type;
5530 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005531 memcpy(p, session->peer_cert_digest,
5532 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005533 p += session->peer_cert_digest_len;
5534 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005535 } else {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005536 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005538 *p++ = (unsigned char) MBEDTLS_MD_NONE;
5539 *p++ = 0;
5540 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005541 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005542#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5543#endif /* MBEDTLS_X509_CRT_PARSE_C */
5544
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005545 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005546 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005547 */
5548#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005549 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005551 if (used <= buf_len) {
5552 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
5553 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
5554 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005556 if (session->ticket != NULL) {
5557 memcpy(p, session->ticket, session->ticket_len);
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005558 p += session->ticket_len;
5559 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005560
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005561 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01005562 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005563 }
5564#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5565
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005566 /*
5567 * Misc extension-related info
5568 */
5569#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
5570 used += 1;
5571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005572 if (used <= buf_len) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005573 *p++ = session->mfl_code;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005574 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005575#endif
5576
5577#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
5578 used += 1;
5579
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005580 if (used <= buf_len) {
5581 *p++ = (unsigned char) ((session->trunc_hmac) & 0xFF);
5582 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005583#endif
5584
5585#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5586 used += 1;
5587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 if (used <= buf_len) {
5589 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
5590 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005591#endif
5592
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005593 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02005594 *olen = used;
5595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 if (used > buf_len) {
5597 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5598 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005600 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005601}
5602
5603/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005604 * Public wrapper for ssl_session_save()
5605 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005606int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
5607 unsigned char *buf,
5608 size_t buf_len,
5609 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005610{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005611 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005612}
5613
5614/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005615 * Deserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005616 *
5617 * This internal version is wrapped by a public function that cleans up in
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005618 * case of error, and has an extra option omit_header.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005619 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005620MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005621static int ssl_session_load(mbedtls_ssl_session *session,
5622 unsigned char omit_header,
5623 const unsigned char *buf,
5624 size_t len)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005625{
5626 const unsigned char *p = buf;
5627 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005628#if defined(MBEDTLS_HAVE_TIME)
5629 uint64_t start;
5630#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005631#if defined(MBEDTLS_X509_CRT_PARSE_C)
5632#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5633 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005634#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5635#endif /* MBEDTLS_X509_CRT_PARSE_C */
5636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005637 if (!omit_header) {
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005638 /*
5639 * Check version identifier
5640 */
5641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
5643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02005644 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005645
5646 if (memcmp(p, ssl_serialized_session_header,
5647 sizeof(ssl_serialized_session_header)) != 0) {
5648 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
5649 }
5650 p += sizeof(ssl_serialized_session_header);
Hanno Beckera835da52019-05-16 12:39:07 +01005651 }
Hanno Beckera835da52019-05-16 12:39:07 +01005652
5653 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005654 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005655 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005656#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005657 if (8 > (size_t) (end - p)) {
5658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5659 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661 start = ((uint64_t) p[0] << 56) |
5662 ((uint64_t) p[1] << 48) |
5663 ((uint64_t) p[2] << 40) |
5664 ((uint64_t) p[3] << 32) |
5665 ((uint64_t) p[4] << 24) |
5666 ((uint64_t) p[5] << 16) |
5667 ((uint64_t) p[6] << 8) |
5668 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005669 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005670
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005671 session->start = (time_t) start;
5672#endif /* MBEDTLS_HAVE_TIME */
5673
5674 /*
5675 * Basic mandatory fields
5676 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005677 if (2 + 1 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
5678 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5679 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005681 session->ciphersuite = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005682 p += 2;
5683
5684 session->compression = *p++;
5685
5686 session->id_len = *p++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005687 memcpy(session->id, p, 32);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005688 p += 32;
5689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005690 memcpy(session->master, p, 48);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005691 p += 48;
5692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005693 session->verify_result = ((uint32_t) p[0] << 24) |
5694 ((uint32_t) p[1] << 16) |
5695 ((uint32_t) p[2] << 8) |
5696 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005697 p += 4;
5698
5699 /* Immediately clear invalid pointer values that have been read, in case
5700 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005701#if defined(MBEDTLS_X509_CRT_PARSE_C)
5702#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5703 session->peer_cert = NULL;
5704#else
5705 session->peer_cert_digest = NULL;
5706#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5707#endif /* MBEDTLS_X509_CRT_PARSE_C */
5708#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5709 session->ticket = NULL;
5710#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5711
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005712 /*
5713 * Peer certificate
5714 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005715#if defined(MBEDTLS_X509_CRT_PARSE_C)
5716#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5717 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005718 if (3 > (size_t) (end - p)) {
5719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5720 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005722 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005723 p += 3;
5724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (cert_len != 0) {
Janos Follath865b3eb2019-12-16 11:46:15 +00005726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005728 if (cert_len > (size_t) (end - p)) {
5729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5730 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005732 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005734 if (session->peer_cert == NULL) {
5735 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5736 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005737
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005738 mbedtls_x509_crt_init(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005739
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005740 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
5741 p, cert_len)) != 0) {
5742 mbedtls_x509_crt_free(session->peer_cert);
5743 mbedtls_free(session->peer_cert);
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005744 session->peer_cert = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005745 return ret;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005746 }
5747
5748 p += cert_len;
5749 }
5750#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5751 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005752 if (2 > (size_t) (end - p)) {
5753 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5754 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005755
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005756 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
5757 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005759 if (session->peer_cert_digest_len != 0) {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005760 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005761 mbedtls_md_info_from_type(session->peer_cert_digest_type);
5762 if (md_info == NULL) {
5763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5764 }
5765 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
5766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5767 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005769 if (session->peer_cert_digest_len > (size_t) (end - p)) {
5770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5771 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005772
5773 session->peer_cert_digest =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005774 mbedtls_calloc(1, session->peer_cert_digest_len);
5775 if (session->peer_cert_digest == NULL) {
5776 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5777 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005779 memcpy(session->peer_cert_digest, p,
5780 session->peer_cert_digest_len);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005781 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005782 }
5783#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5784#endif /* MBEDTLS_X509_CRT_PARSE_C */
5785
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005786 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005787 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005788 */
5789#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005790 if (3 > (size_t) (end - p)) {
5791 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5792 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005794 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005795 p += 3;
5796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005797 if (session->ticket_len != 0) {
5798 if (session->ticket_len > (size_t) (end - p)) {
5799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5800 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005802 session->ticket = mbedtls_calloc(1, session->ticket_len);
5803 if (session->ticket == NULL) {
5804 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
5805 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005807 memcpy(session->ticket, p, session->ticket_len);
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005808 p += session->ticket_len;
5809 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005811 if (4 > (size_t) (end - p)) {
5812 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5813 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005815 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
5816 ((uint32_t) p[1] << 16) |
5817 ((uint32_t) p[2] << 8) |
5818 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005819 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005820#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5821
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005822 /*
5823 * Misc extension-related info
5824 */
5825#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005826 if (1 > (size_t) (end - p)) {
5827 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5828 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005829
5830 session->mfl_code = *p++;
5831#endif
5832
5833#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005834 if (1 > (size_t) (end - p)) {
5835 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5836 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005837
5838 session->trunc_hmac = *p++;
5839#endif
5840
5841#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005842 if (1 > (size_t) (end - p)) {
5843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5844 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02005845
5846 session->encrypt_then_mac = *p++;
5847#endif
5848
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02005849 /* Done, should have consumed entire buffer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005850 if (p != end) {
5851 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5852 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005854 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02005855}
5856
5857/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02005858 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005859 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005860int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
5861 const unsigned char *buf,
5862 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005863{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005864 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005866 if (ret != 0) {
5867 mbedtls_ssl_session_free(session);
5868 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005870 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02005871}
5872
5873/*
Paul Bakker1961b702013-01-25 14:49:24 +01005874 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005875 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005876int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005880 if (ssl == NULL || ssl->conf == NULL) {
5881 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5882 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005885 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5886 ret = mbedtls_ssl_handshake_client_step(ssl);
5887 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005888#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005889#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005890 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
5891 ret = mbedtls_ssl_handshake_server_step(ssl);
5892 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005893#endif
5894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005895 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01005896}
5897
5898/*
5899 * Perform the SSL handshake
5900 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005901int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01005902{
5903 int ret = 0;
5904
Hanno Beckera817ea42020-10-20 15:20:23 +01005905 /* Sanity checks */
5906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005907 if (ssl == NULL || ssl->conf == NULL) {
5908 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5909 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005910
Hanno Beckera817ea42020-10-20 15:20:23 +01005911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005912 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5913 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
5914 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
5915 "mbedtls_ssl_set_timer_cb() for DTLS"));
5916 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01005917 }
5918#endif /* MBEDTLS_SSL_PROTO_DTLS */
5919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005920 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01005921
Hanno Beckera817ea42020-10-20 15:20:23 +01005922 /* Main handshake loop */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005923 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5924 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01005925
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005926 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01005927 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005928 }
Paul Bakker1961b702013-01-25 14:49:24 +01005929 }
5930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005931 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005933 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005934}
5935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936#if defined(MBEDTLS_SSL_RENEGOTIATION)
5937#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005938/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005939 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005940 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005941MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005942static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005943{
Janos Follath865b3eb2019-12-16 11:46:15 +00005944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005946 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005947
5948 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5950 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005952 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5953 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5954 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005955 }
5956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005957 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005959 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005961#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005962
5963/*
5964 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 * - any side: calling mbedtls_ssl_renegotiate(),
5966 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
5967 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005968 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005969 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005970 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005971 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005972int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00005973{
Janos Follath865b3eb2019-12-16 11:46:15 +00005974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00005975
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005976 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00005977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005978 if ((ret = ssl_handshake_init(ssl)) != 0) {
5979 return ret;
5980 }
Paul Bakker48916f92012-09-16 19:57:18 +00005981
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005982 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5983 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005985 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5986 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5987 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005988 ssl->handshake->out_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005989 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005990 ssl->handshake->in_msg_seq = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005991 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005992 }
5993#endif
5994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
5996 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005997
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005998 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5999 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6000 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00006001 }
6002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006003 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00006004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006005 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00006006}
6007
6008/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006009 * Renegotiate current connection on client,
6010 * or request renegotiation on server
6011 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006012int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006015
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006016 if (ssl == NULL || ssl->conf == NULL) {
6017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6018 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006020#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006021 /* On server, just send the request */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006022 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6023 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6024 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6025 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006028
6029 /* Did we already try/start sending HelloRequest? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006030 if (ssl->out_left != 0) {
6031 return mbedtls_ssl_flush_output(ssl);
6032 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006034 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006039 /*
6040 * On client, either start the renegotiation process or,
6041 * if already in progress, continue the handshake
6042 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006043 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
6044 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6045 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006046 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006047
6048 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
6049 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
6050 return ret;
6051 }
6052 } else {
6053 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6055 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006056 }
6057 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006060 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006065static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006066{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006069 while (cur != NULL) {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006070 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006071 mbedtls_free(cur);
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006072 cur = next;
6073 }
6074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006075#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006077void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00006078{
Gilles Peskine9b562d52018-04-25 20:32:43 +02006079 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
6080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006081 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006082 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006083 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006084
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006085#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006086 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
6087 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02006088 handshake->async_in_progress = 0;
6089 }
6090#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
6091
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006092#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6093 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006094 mbedtls_md5_free(&handshake->fin_md5);
6095 mbedtls_sha1_free(&handshake->fin_sha1);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006096#endif
6097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6098#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006100 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006101#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006102 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006103#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006104#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02006105#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05006106#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006107 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05006108#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006109 mbedtls_sha512_free(&handshake->fin_sha512);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006110#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05006111#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02006112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
6113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006115 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00006116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006118 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02006119#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02006120#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006121 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006122#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006123 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02006124 handshake->ecjpake_cache = NULL;
6125 handshake->ecjpake_cache_len = 0;
6126#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02006127#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006128
Janos Follath4ae5c292016-02-10 11:27:43 +00006129#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
6130 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02006131 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006132 mbedtls_free((void *) handshake->curves);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006133#endif
6134
Gilles Peskineeccd8882020-03-10 12:19:08 +01006135#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006136 if (handshake->psk != NULL) {
6137 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
6138 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01006139 }
6140#endif
6141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6143 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006144 /*
6145 * Free only the linked list wrapper, not the keys themselves
6146 * since the belong to the SNI callback
6147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006148 if (handshake->sni_key_cert != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006149 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006151 while (cur != NULL) {
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006152 next = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006153 mbedtls_free(cur);
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006154 cur = next;
6155 }
6156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006158
Gilles Peskineeccd8882020-03-10 12:19:08 +01006159#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006160 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
6161 if (handshake->ecrs_peer_cert != NULL) {
6162 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
6163 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00006164 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02006165#endif
6166
Hanno Becker75173122019-02-06 16:18:31 +00006167#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
6168 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006169 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00006170#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006173 mbedtls_free(handshake->verify_cookie);
6174 mbedtls_ssl_flight_free(handshake->flight);
6175 mbedtls_ssl_buffering_free(ssl);
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006176#endif
6177
Hanno Becker4a63ed42019-01-08 11:39:35 +00006178#if defined(MBEDTLS_ECDH_C) && \
6179 defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006180 psa_destroy_key(handshake->ecdh_psa_privkey);
Hanno Becker4a63ed42019-01-08 11:39:35 +00006181#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
6182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006183 mbedtls_platform_zeroize(handshake,
6184 sizeof(mbedtls_ssl_handshake_params));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006185
6186#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6187 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
6188 * processes datagrams and the fact that a datagram is allowed to have
6189 * several records in it, it is possible that the I/O buffers are not
6190 * empty at this stage */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006191 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
6192 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006193#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006194}
6195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006196void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00006197{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006198 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006199 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006200 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006203 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02006204#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006205
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02006206#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006207 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02006208#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006210 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00006211}
6212
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006213#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006214
6215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6216#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
6217#else
6218#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
6219#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6220
6221#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6222#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
6223#else
6224#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
6225#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6226
6227#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6228#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
6229#else
6230#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
6231#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6232
6233#if defined(MBEDTLS_SSL_ALPN)
6234#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
6235#else
6236#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
6237#endif /* MBEDTLS_SSL_ALPN */
6238
6239#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
6240#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
6241#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
6242#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
6243
6244#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006245 ((uint32_t) ( \
6246 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
6247 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
6248 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
6249 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
6250 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
6251 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
6252 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
6253 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006254
Chien Wongb6d57932024-02-07 21:48:12 +08006255static const unsigned char ssl_serialized_context_header[] = {
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006256 MBEDTLS_VERSION_MAJOR,
6257 MBEDTLS_VERSION_MINOR,
6258 MBEDTLS_VERSION_PATCH,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006259 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6260 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
6261 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6262 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
6263 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006264};
6265
Paul Bakker5121ce52009-01-03 21:22:43 +00006266/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006267 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006268 *
6269 * The format of the serialized data is:
6270 * (in the presentation language of TLS, RFC 8446 section 3)
6271 *
6272 * // header
6273 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006274 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006275 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006276 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02006277 * Note: When updating the format, remember to keep these
6278 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02006279 *
6280 * // session sub-structure
6281 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
6282 * // transform sub-structure
6283 * uint8 random[64]; // ServerHello.random+ClientHello.random
6284 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
6285 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
6286 * // fields from ssl_context
6287 * uint32 badmac_seen; // DTLS: number of records with failing MAC
6288 * uint64 in_window_top; // DTLS: last validated record seq_num
6289 * uint64 in_window; // DTLS: bitmask for replay protection
6290 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
6291 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
6292 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
6293 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
6294 *
6295 * Note that many fields of the ssl_context or sub-structures are not
6296 * serialized, as they fall in one of the following categories:
6297 *
6298 * 1. forced value (eg in_left must be 0)
6299 * 2. pointer to dynamically-allocated memory (eg session, transform)
6300 * 3. value can be re-derived from other data (eg session keys from MS)
6301 * 4. value was temporary (eg content of input buffer)
6302 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006303 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006304int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
6305 unsigned char *buf,
6306 size_t buf_len,
6307 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006308{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006309 unsigned char *p = buf;
6310 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006311 size_t session_len;
6312 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006313
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006314 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006315 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
6316 * this function's documentation.
6317 *
6318 * These are due to assumptions/limitations in the implementation. Some of
6319 * them are likely to stay (no handshake in progress) some might go away
6320 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02006321 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006322 /* The initial handshake must be over */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006323 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6324 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
6325 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006326 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006327 if (ssl->handshake != NULL) {
6328 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
6329 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006330 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006331 /* Double-check that sub-structures are indeed ready */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006332 if (ssl->transform == NULL || ssl->session == NULL) {
6333 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
6334 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006335 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006336 /* There must be no pending incoming or outgoing data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006337 if (mbedtls_ssl_check_pending(ssl) != 0) {
6338 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
6339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006340 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006341 if (ssl->out_left != 0) {
6342 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
6343 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006344 }
Dave Rodgmana03396a2022-12-06 16:30:38 +00006345 /* Protocol must be DTLS, not TLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006346 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6347 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
6348 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006349 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006350 /* Version must be 1.2 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006351 if (ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3) {
6352 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6353 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006354 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006355 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
6356 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
6357 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006358 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006359 /* We must be using an AEAD ciphersuite */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006360 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
6361 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
6362 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006363 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006364 /* Renegotiation must not be enabled */
6365#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006366 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
6367 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
6368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03006369 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02006370#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006371
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006372 /*
6373 * Version and format identifier
6374 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006375 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006376
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006377 if (used <= buf_len) {
6378 memcpy(p, ssl_serialized_context_header,
6379 sizeof(ssl_serialized_context_header));
6380 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006381 }
6382
6383 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006384 * Session (length + data)
6385 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006386 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
6387 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
6388 return ret;
6389 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006390
6391 used += 4 + session_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006392 if (used <= buf_len) {
6393 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006394 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006396 ret = ssl_session_save(ssl->session, 1,
6397 p, session_len, &session_len);
6398 if (ret != 0) {
6399 return ret;
6400 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006401
6402 p += session_len;
6403 }
6404
6405 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006406 * Transform
6407 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006408 used += sizeof(ssl->transform->randbytes);
6409 if (used <= buf_len) {
6410 memcpy(p, ssl->transform->randbytes,
6411 sizeof(ssl->transform->randbytes));
6412 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006413 }
6414
6415#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6416 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006417 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006418 *p++ = ssl->transform->in_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006419 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006420 p += ssl->transform->in_cid_len;
6421
6422 *p++ = ssl->transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006423 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006424 p += ssl->transform->out_cid_len;
6425 }
6426#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6427
6428 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006429 * Saved fields from top-level ssl_context structure
6430 */
6431#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
6432 used += 4;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006433 if (used <= buf_len) {
6434 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006435 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006436 }
6437#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6438
6439#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
6440 used += 16;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006441 if (used <= buf_len) {
6442 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006443 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006444
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006445 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006446 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006447 }
6448#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6449
6450#if defined(MBEDTLS_SSL_PROTO_DTLS)
6451 used += 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006452 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006453 *p++ = ssl->disable_datagram_packing;
6454 }
6455#endif /* MBEDTLS_SSL_PROTO_DTLS */
6456
6457 used += 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006458 if (used <= buf_len) {
6459 memcpy(p, ssl->cur_out_ctr, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006460 p += 8;
6461 }
6462
6463#if defined(MBEDTLS_SSL_PROTO_DTLS)
6464 used += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006465 if (used <= buf_len) {
6466 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani2f98d792021-08-20 11:44:44 +01006467 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006468 }
6469#endif /* MBEDTLS_SSL_PROTO_DTLS */
6470
6471#if defined(MBEDTLS_SSL_ALPN)
6472 {
6473 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006474 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006475 : 0;
6476
6477 used += 1 + alpn_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006478 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006479 *p++ = alpn_len;
6480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006481 if (ssl->alpn_chosen != NULL) {
6482 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006483 p += alpn_len;
6484 }
6485 }
6486 }
6487#endif /* MBEDTLS_SSL_ALPN */
6488
6489 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006490 * Done
6491 */
6492 *olen = used;
6493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006494 if (used > buf_len) {
6495 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
6496 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006498 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006500 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006501}
6502
6503/*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006504 * Helper to get TLS 1.2 PRF from ciphersuite
6505 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
6506 */
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006507#if defined(MBEDTLS_SHA256_C) || \
6508 (defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006509typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
6510 const char *label,
6511 const unsigned char *random, size_t rlen,
6512 unsigned char *dstbuf, size_t dlen);
6513static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006514{
6515 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006516 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006518 if (ciphersuite_info == NULL) {
6519 return NULL;
6520 }
Andrzej Kurek84fc52c2022-10-25 04:18:30 -04006521
6522#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006523 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
6524 return tls_prf_sha384;
6525 } else
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006526#endif
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006527#if defined(MBEDTLS_SHA256_C)
6528 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006529 if (ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
6530 return tls_prf_sha256;
6531 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006532 }
6533#endif
6534#if !defined(MBEDTLS_SHA256_C) && \
6535 (!defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA512_NO_SHA384))
6536 (void) ciphersuite_info;
6537#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006538 return NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006539}
6540
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006541#endif /* MBEDTLS_SHA256_C ||
6542 (MBEDTLS_SHA512_C && !MBEDTLS_SHA512_NO_SHA384) */
6543
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006544/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006545 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006546 *
6547 * This internal version is wrapped by a public function that cleans up in
6548 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006549 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02006550MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006551static int ssl_context_load(mbedtls_ssl_context *ssl,
6552 const unsigned char *buf,
6553 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006554{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006555 const unsigned char *p = buf;
6556 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006557 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00006558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006559 tls_prf_fn prf_func = NULL;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006560
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006561 /*
6562 * The context should have been freshly setup or reset.
6563 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02006564 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006565 * renegotiating, or if the user mistakenly loaded a session first.)
6566 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006567 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
6568 ssl->session != NULL) {
6569 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006570 }
6571
6572 /*
6573 * We can't check that the config matches the initial one, but we can at
6574 * least check it matches the requirements for serializing.
6575 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006576 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006577 ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
6578 ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
6579 ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
6580 ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
6581#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02006582 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006583#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006584 0) {
6585 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02006586 }
6587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006588 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006589
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006590 /*
6591 * Check version identifier
6592 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006593 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
6594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006595 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006596
6597 if (memcmp(p, ssl_serialized_context_header,
6598 sizeof(ssl_serialized_context_header)) != 0) {
6599 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
6600 }
6601 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006602
6603 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006604 * Session
6605 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006606 if ((size_t) (end - p) < 4) {
6607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6608 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006610 session_len = ((size_t) p[0] << 24) |
6611 ((size_t) p[1] << 16) |
6612 ((size_t) p[2] << 8) |
6613 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006614 p += 4;
6615
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006616 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006617 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006618 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006619 ssl->session_in = ssl->session;
6620 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006621 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006623 if ((size_t) (end - p) < session_len) {
6624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6625 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006627 ret = ssl_session_load(ssl->session, 1, p, session_len);
6628 if (ret != 0) {
6629 mbedtls_ssl_session_free(ssl->session);
6630 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02006631 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006632
6633 p += session_len;
6634
6635 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006636 * Transform
6637 */
6638
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006639 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00006640 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006641 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006642 ssl->transform_in = ssl->transform;
6643 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02006644 ssl->transform_negotiate = NULL;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006646 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
6647 if (prf_func == NULL) {
6648 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6649 }
Andrzej Kurekf9412f72022-10-18 07:30:19 -04006650
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006651 /* Read random bytes and populate structure */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006652 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
6653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6654 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006656 ret = ssl_populate_transform(ssl->transform,
6657 ssl->session->ciphersuite,
6658 ssl->session->master,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006659#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
6660#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006661 ssl->session->encrypt_then_mac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006662#endif
6663#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006664 ssl->session->trunc_hmac,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006665#endif
6666#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
6667#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006668 ssl->session->compression,
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006669#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006670 prf_func,
6671 p, /* currently pointing to randbytes */
6672 MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
6673 ssl->conf->endpoint,
6674 ssl);
6675 if (ret != 0) {
6676 return ret;
6677 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006679 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006680
6681#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6682 /* Read connection IDs and store them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006683 if ((size_t) (end - p) < 1) {
6684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6685 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006686
6687 ssl->transform->in_cid_len = *p++;
6688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006689 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
6690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6691 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006693 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006694 p += ssl->transform->in_cid_len;
6695
6696 ssl->transform->out_cid_len = *p++;
6697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006698 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
6699 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6700 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006702 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02006703 p += ssl->transform->out_cid_len;
6704#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6705
6706 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006707 * Saved fields from top-level ssl_context structure
6708 */
6709#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006710 if ((size_t) (end - p) < 4) {
6711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6712 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006714 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
6715 ((uint32_t) p[1] << 16) |
6716 ((uint32_t) p[2] << 8) |
6717 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006718 p += 4;
6719#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
6720
6721#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006722 if ((size_t) (end - p) < 16) {
6723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6724 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006726 ssl->in_window_top = ((uint64_t) p[0] << 56) |
6727 ((uint64_t) p[1] << 48) |
6728 ((uint64_t) p[2] << 40) |
6729 ((uint64_t) p[3] << 32) |
6730 ((uint64_t) p[4] << 24) |
6731 ((uint64_t) p[5] << 16) |
6732 ((uint64_t) p[6] << 8) |
6733 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006734 p += 8;
6735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006736 ssl->in_window = ((uint64_t) p[0] << 56) |
6737 ((uint64_t) p[1] << 48) |
6738 ((uint64_t) p[2] << 40) |
6739 ((uint64_t) p[3] << 32) |
6740 ((uint64_t) p[4] << 24) |
6741 ((uint64_t) p[5] << 16) |
6742 ((uint64_t) p[6] << 8) |
6743 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006744 p += 8;
6745#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
6746
6747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006748 if ((size_t) (end - p) < 1) {
6749 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6750 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006751
6752 ssl->disable_datagram_packing = *p++;
6753#endif /* MBEDTLS_SSL_PROTO_DTLS */
6754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006755 if ((size_t) (end - p) < 8) {
6756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6757 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006759 memcpy(ssl->cur_out_ctr, p, 8);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006760 p += 8;
6761
6762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006763 if ((size_t) (end - p) < 2) {
6764 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6765 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006767 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006768 p += 2;
6769#endif /* MBEDTLS_SSL_PROTO_DTLS */
6770
6771#if defined(MBEDTLS_SSL_ALPN)
6772 {
6773 uint8_t alpn_len;
6774 const char **cur;
6775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006776 if ((size_t) (end - p) < 1) {
6777 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6778 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006779
6780 alpn_len = *p++;
6781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006782 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006783 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006784 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
6785 if (strlen(*cur) == alpn_len &&
Waleed Elmelegy98ebf482024-03-15 14:29:24 +00006786 memcmp(p, *cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006787 ssl->alpn_chosen = *cur;
6788 break;
6789 }
6790 }
6791 }
6792
6793 /* can only happen on conf mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006794 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
6795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6796 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02006797
6798 p += alpn_len;
6799 }
6800#endif /* MBEDTLS_SSL_ALPN */
6801
6802 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006803 * Forced fields from top-level ssl_context structure
6804 *
6805 * Most of them already set to the correct value by mbedtls_ssl_init() and
6806 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
6807 */
6808 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
6809
6810 ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
6811 ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
6812
Hanno Becker361b10d2019-08-30 10:42:49 +01006813 /* Adjust pointers for header fields of outgoing records to
6814 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006815 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01006816
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006817#if defined(MBEDTLS_SSL_PROTO_DTLS)
6818 ssl->in_epoch = 1;
6819#endif
6820
6821 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
6822 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00006823 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
6824 * inappropriately. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006825 if (ssl->handshake != NULL) {
6826 mbedtls_ssl_handshake_free(ssl);
6827 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02006828 ssl->handshake = NULL;
6829 }
6830
6831 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02006832 * Done - should have consumed entire buffer
6833 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006834 if (p != end) {
6835 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6836 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006838 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02006839}
6840
6841/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02006842 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006843 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006844int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
6845 const unsigned char *buf,
6846 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006847{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006848 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006850 if (ret != 0) {
6851 mbedtls_ssl_free(context);
6852 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006854 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006855}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02006856#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02006857
6858/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006859 * Free an SSL context
6860 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006861void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006862{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006863 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006864 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006865 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006866
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006867 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006869 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006870#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6871 size_t out_buf_len = ssl->out_buf_len;
6872#else
6873 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
6874#endif
6875
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006876 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
6877 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006878 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006879 }
6880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006881 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02006882#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
6883 size_t in_buf_len = ssl->in_buf_len;
6884#else
6885 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
6886#endif
6887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006888 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
6889 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05006890 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006891 }
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006894 if (ssl->compress_buf != NULL) {
6895 mbedtls_platform_zeroize(ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN);
6896 mbedtls_free(ssl->compress_buf);
Paul Bakker16770332013-10-11 09:59:44 +02006897 }
6898#endif
6899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006900 if (ssl->transform) {
6901 mbedtls_ssl_transform_free(ssl->transform);
6902 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00006903 }
6904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006905 if (ssl->handshake) {
6906 mbedtls_ssl_handshake_free(ssl);
6907 mbedtls_ssl_transform_free(ssl->transform_negotiate);
6908 mbedtls_ssl_session_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006910 mbedtls_free(ssl->handshake);
6911 mbedtls_free(ssl->transform_negotiate);
6912 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00006913 }
6914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006915 if (ssl->session) {
6916 mbedtls_ssl_session_free(ssl->session);
6917 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01006918 }
6919
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02006920#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine3a2f75d2025-02-12 23:28:48 +01006921 mbedtls_ssl_free_hostname(ssl);
Paul Bakker0be444a2013-08-27 21:55:01 +02006922#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006925 if (mbedtls_ssl_hw_record_finish != NULL) {
6926 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_finish()"));
6927 mbedtls_ssl_hw_record_finish(ssl);
Paul Bakker05ef8352012-05-08 09:17:57 +00006928 }
6929#endif
6930
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02006931#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006932 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006933#endif
6934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006935 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00006936
Paul Bakker86f04f42013-02-14 11:20:09 +01006937 /* Actually clear after last debug message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006938 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00006939}
6940
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006941/*
Tom Cosgrove49f99bc2022-12-04 16:44:21 +00006942 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006943 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006944void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006945{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006946 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006947}
6948
Gilles Peskineeccd8882020-03-10 12:19:08 +01006949#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006950static const int ssl_preset_default_hashes[] = {
Gilles Peskinec54010c2021-05-12 22:52:09 +02006951#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006952 MBEDTLS_MD_SHA512,
Gilles Peskinec54010c2021-05-12 22:52:09 +02006953#endif
6954#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006955 MBEDTLS_MD_SHA384,
6956#endif
6957#if defined(MBEDTLS_SHA256_C)
6958 MBEDTLS_MD_SHA256,
6959 MBEDTLS_MD_SHA224,
6960#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +02006961#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006962 MBEDTLS_MD_SHA1,
6963#endif
6964 MBEDTLS_MD_NONE
6965};
Simon Butcherc97b6972015-12-27 23:48:17 +00006966#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01006967
Chien Wongb6d57932024-02-07 21:48:12 +08006968static const int ssl_preset_suiteb_ciphersuites[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006969 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
6970 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
6971 0
6972};
6973
Gilles Peskineeccd8882020-03-10 12:19:08 +01006974#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Chien Wongb6d57932024-02-07 21:48:12 +08006975static const int ssl_preset_suiteb_hashes[] = {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006976 MBEDTLS_MD_SHA256,
6977 MBEDTLS_MD_SHA384,
6978 MBEDTLS_MD_NONE
6979};
6980#endif
6981
6982#if defined(MBEDTLS_ECP_C)
Chien Wongb6d57932024-02-07 21:48:12 +08006983static const mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01006984#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006985 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006986#endif
6987#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006988 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01006989#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02006990 MBEDTLS_ECP_DP_NONE
6991};
6992#endif
6993
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006994/*
Tillmann Karras588ad502015-09-25 04:27:22 +02006995 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006996 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01006997int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
6998 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02006999{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007000#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00007001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02007002#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007003
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02007004 /* Use the functions here so that they are covered in tests,
7005 * but otherwise access member directly for efficiency */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007006 mbedtls_ssl_conf_endpoint(conf, endpoint);
7007 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007008
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007009 /*
7010 * Things that are common to all presets
7011 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007012#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007013 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007014 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
7015#if defined(MBEDTLS_SSL_SESSION_TICKETS)
7016 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
7017#endif
7018 }
7019#endif
7020
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007021#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007022 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02007023#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007024
7025#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7026 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
7027#endif
7028
7029#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
7030 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
7031#endif
7032
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01007033#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7034 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
7035#endif
7036
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007037#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007038 conf->f_cookie_write = ssl_cookie_write_dummy;
7039 conf->f_cookie_check = ssl_cookie_check_dummy;
7040#endif
7041
7042#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
7043 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
7044#endif
7045
Janos Follath088ce432017-04-10 12:42:31 +01007046#if defined(MBEDTLS_SSL_SRV_C)
7047 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
7048#endif
7049
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007050#if defined(MBEDTLS_SSL_PROTO_DTLS)
7051 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
7052 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
7053#endif
7054
7055#if defined(MBEDTLS_SSL_RENEGOTIATION)
7056 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007057 memset(conf->renego_period, 0x00, 2);
7058 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007059#endif
7060
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007061#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007062 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
7063 const unsigned char dhm_p[] =
7064 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
7065 const unsigned char dhm_g[] =
7066 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01007067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007068 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
7069 dhm_p, sizeof(dhm_p),
7070 dhm_g, sizeof(dhm_g))) != 0) {
7071 return ret;
7072 }
7073 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02007074#endif
7075
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007076 /*
7077 * Preset-specific defaults
7078 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007079 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007080 /*
7081 * NSA Suite B
7082 */
7083 case MBEDTLS_SSL_PRESET_SUITEB:
7084 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
7085 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
7086 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7087 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7088
7089 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007090 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7091 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7092 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7093 ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007094
7095#if defined(MBEDTLS_X509_CRT_PARSE_C)
7096 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007097#endif
7098
Gilles Peskineeccd8882020-03-10 12:19:08 +01007099#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007100 conf->sig_hashes = ssl_preset_suiteb_hashes;
7101#endif
7102
7103#if defined(MBEDTLS_ECP_C)
7104 conf->curve_list = ssl_preset_suiteb_curves;
7105#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02007106 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007107
7108 /*
7109 * Default
7110 */
7111 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007112 conf->min_major_ver = (MBEDTLS_SSL_MIN_MAJOR_VERSION >
7113 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION) ?
7114 MBEDTLS_SSL_MIN_MAJOR_VERSION :
7115 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
7116 conf->min_minor_ver = (MBEDTLS_SSL_MIN_MINOR_VERSION >
7117 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION) ?
7118 MBEDTLS_SSL_MIN_MINOR_VERSION :
7119 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007120 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
7121 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
7122
7123#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007124 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007125 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007126 }
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007127#endif
7128
7129 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007130 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
7131 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
7132 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
7133 mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007134
7135#if defined(MBEDTLS_X509_CRT_PARSE_C)
7136 conf->cert_profile = &mbedtls_x509_crt_profile_default;
7137#endif
7138
Gilles Peskineeccd8882020-03-10 12:19:08 +01007139#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +01007140 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02007141#endif
7142
7143#if defined(MBEDTLS_ECP_C)
7144 conf->curve_list = mbedtls_ecp_grp_id_list();
7145#endif
7146
7147#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
7148 conf->dhm_min_bitlen = 1024;
7149#endif
7150 }
7151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007152 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007153}
7154
7155/*
7156 * Free mbedtls_ssl_config
7157 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007158void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007159{
7160#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007161 mbedtls_mpi_free(&conf->dhm_P);
7162 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007163#endif
7164
Gilles Peskineeccd8882020-03-10 12:19:08 +01007165#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007166 if (conf->psk != NULL) {
7167 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
7168 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00007169 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007170 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09007171 }
7172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007173 if (conf->psk_identity != NULL) {
7174 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
7175 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00007176 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007177 conf->psk_identity_len = 0;
7178 }
7179#endif
7180
7181#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007182 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007183#endif
7184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007185 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02007186}
7187
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007188#if defined(MBEDTLS_PK_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007189 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007190/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007193unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007194{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007196 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
7197 return MBEDTLS_SSL_SIG_RSA;
7198 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007201 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
7202 return MBEDTLS_SSL_SIG_ECDSA;
7203 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007204#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007205 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02007206}
7207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007208unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007209{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007210 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007211 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007212 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007213 case MBEDTLS_PK_ECDSA:
7214 case MBEDTLS_PK_ECKEY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007215 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007216 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007217 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007218 }
7219}
7220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007221mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007222{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007223 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#if defined(MBEDTLS_RSA_C)
7225 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007226 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_ECDSA_C)
7229 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007230 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007231#endif
7232 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007233 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007234 }
7235}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02007236#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007237
Hanno Becker7e5437a2017-04-28 17:15:26 +01007238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Gilles Peskineeccd8882020-03-10 12:19:08 +01007239 defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007240
7241/* Find an entry in a signature-hash set matching a given hash algorithm. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007242mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find(mbedtls_ssl_sig_hash_set_t *set,
7243 mbedtls_pk_type_t sig_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007244{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007245 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007246 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007247 return set->rsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007248 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007249 return set->ecdsa;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007250 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007251 return MBEDTLS_MD_NONE;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007252 }
7253}
7254
7255/* Add a signature-hash-pair to a signature-hash set */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007256void mbedtls_ssl_sig_hash_set_add(mbedtls_ssl_sig_hash_set_t *set,
7257 mbedtls_pk_type_t sig_alg,
7258 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007259{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007260 switch (sig_alg) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007261 case MBEDTLS_PK_RSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007262 if (set->rsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007263 set->rsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007264 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007265 break;
7266
7267 case MBEDTLS_PK_ECDSA:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007268 if (set->ecdsa == MBEDTLS_MD_NONE) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01007269 set->ecdsa = md_alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007270 }
Hanno Becker7e5437a2017-04-28 17:15:26 +01007271 break;
7272
7273 default:
7274 break;
7275 }
7276}
7277
7278/* Allow exactly one hash algorithm for each signature. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007279void mbedtls_ssl_sig_hash_set_const_hash(mbedtls_ssl_sig_hash_set_t *set,
7280 mbedtls_md_type_t md_alg)
Hanno Becker7e5437a2017-04-28 17:15:26 +01007281{
7282 set->rsa = md_alg;
7283 set->ecdsa = md_alg;
7284}
7285
7286#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
Gilles Peskineeccd8882020-03-10 12:19:08 +01007287 MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Becker7e5437a2017-04-28 17:15:26 +01007288
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007289/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007290 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02007291 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007292mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007293{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007294 switch (hash) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_MD5_C)
7296 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007297 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#if defined(MBEDTLS_SHA1_C)
7300 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007301 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SHA256_C)
7304 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007305 return MBEDTLS_MD_SHA224;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007307 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007308#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007309#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007311 return MBEDTLS_MD_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007312#endif
7313#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007315 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007316#endif
7317 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007318 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02007319 }
7320}
7321
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007322/*
7323 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
7324 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007325unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007326{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007327 switch (md) {
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007328#if defined(MBEDTLS_MD5_C)
7329 case MBEDTLS_MD_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007330 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007331#endif
7332#if defined(MBEDTLS_SHA1_C)
7333 case MBEDTLS_MD_SHA1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007334 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007335#endif
7336#if defined(MBEDTLS_SHA256_C)
7337 case MBEDTLS_MD_SHA224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007338 return MBEDTLS_SSL_HASH_SHA224;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007339 case MBEDTLS_MD_SHA256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007340 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007341#endif
Gilles Peskined2d59372021-05-12 22:43:27 +02007342#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007343 case MBEDTLS_MD_SHA384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007344 return MBEDTLS_SSL_HASH_SHA384;
Gilles Peskinec54010c2021-05-12 22:52:09 +02007345#endif
7346#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007347 case MBEDTLS_MD_SHA512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007348 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007349#endif
7350 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007351 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007352 }
7353}
7354
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007355#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007356/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007357 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007358 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007359 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007360int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007364 if (ssl->conf->curve_list == NULL) {
7365 return -1;
7366 }
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02007367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007368 for (gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++) {
7369 if (*gid == grp_id) {
7370 return 0;
7371 }
7372 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007374 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01007375}
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007376
7377/*
7378 * Same as mbedtls_ssl_check_curve() but takes a TLS ID for the curve.
7379 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007380int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007381{
7382 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007383 mbedtls_ecp_curve_info_from_tls_id(tls_id);
7384 if (curve_info == NULL) {
7385 return -1;
7386 }
7387 return mbedtls_ssl_check_curve(ssl, curve_info->grp_id);
Manuel Pégourié-Gonnard298d6cc2022-02-14 11:34:47 +01007388}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02007389#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007390
Gilles Peskineeccd8882020-03-10 12:19:08 +01007391#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007392/*
7393 * Check if a hash proposed by the peer is in our list.
7394 * Return 0 if we're willing to use it, -1 otherwise.
7395 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007396int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
7397 mbedtls_md_type_t md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007398{
7399 const int *cur;
7400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007401 if (ssl->conf->sig_hashes == NULL) {
7402 return -1;
7403 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007405 for (cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++) {
7406 if (*cur == (int) md) {
7407 return 0;
7408 }
7409 }
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007411 return -1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007412}
Gilles Peskineeccd8882020-03-10 12:19:08 +01007413#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02007414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007416int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
7417 const mbedtls_ssl_ciphersuite_t *ciphersuite,
7418 int cert_endpoint,
7419 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007420{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007421 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007423 int usage = 0;
7424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007426 const char *ext_oid;
7427 size_t ext_len;
7428#endif
7429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
7431 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007432 ((void) cert);
7433 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007434 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007435#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007438 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007439 /* Server part of the key exchange */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007440 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 case MBEDTLS_KEY_EXCHANGE_RSA:
7442 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007443 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007444 break;
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
7447 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
7448 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
7449 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007450 break;
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
7453 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007454 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007455 break;
7456
7457 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 case MBEDTLS_KEY_EXCHANGE_NONE:
7459 case MBEDTLS_KEY_EXCHANGE_PSK:
7460 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
7461 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02007462 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007463 usage = 0;
7464 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007465 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
7467 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007468 }
7469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007470 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007471 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007472 ret = -1;
7473 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007474#else
7475 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007479 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007481 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
7482 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007484 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007485 }
7486
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007487 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01007488 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01007489 ret = -1;
7490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02007492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007493 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02007494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02007496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007497int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Simon Butcher99000142016-10-13 17:21:01 +01007498{
7499#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007500 if (ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3) {
Simon Butcher99000142016-10-13 17:21:01 +01007501 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007502 }
Simon Butcher99000142016-10-13 17:21:01 +01007503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007504 switch (md) {
Simon Butcher99000142016-10-13 17:21:01 +01007505#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
7506#if defined(MBEDTLS_MD5_C)
7507 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +01007508 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +01007509#endif
7510#if defined(MBEDTLS_SHA1_C)
7511 case MBEDTLS_SSL_HASH_SHA1:
7512 ssl->handshake->calc_verify = ssl_calc_verify_tls;
7513 break;
7514#endif
7515#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Gilles Peskined2d59372021-05-12 22:43:27 +02007516#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
Simon Butcher99000142016-10-13 17:21:01 +01007517 case MBEDTLS_SSL_HASH_SHA384:
7518 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
7519 break;
7520#endif
7521#if defined(MBEDTLS_SHA256_C)
7522 case MBEDTLS_SSL_HASH_SHA256:
7523 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
7524 break;
7525#endif
7526 default:
7527 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7528 }
7529
7530 return 0;
7531#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
7532 (void) ssl;
7533 (void) md;
7534
7535 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
7536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
7537}
7538
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007539#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7540 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007541int mbedtls_ssl_get_key_exchange_md_ssl_tls(mbedtls_ssl_context *ssl,
7542 unsigned char *output,
7543 unsigned char *data, size_t data_len)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007544{
7545 int ret = 0;
7546 mbedtls_md5_context mbedtls_md5;
7547 mbedtls_sha1_context mbedtls_sha1;
7548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007549 mbedtls_md5_init(&mbedtls_md5);
7550 mbedtls_sha1_init(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007551
7552 /*
7553 * digitally-signed struct {
7554 * opaque md5_hash[16];
7555 * opaque sha_hash[20];
7556 * };
7557 *
7558 * md5_hash
7559 * MD5(ClientHello.random + ServerHello.random
7560 * + ServerParams);
7561 * sha_hash
7562 * SHA(ClientHello.random + ServerHello.random
7563 * + ServerParams);
7564 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007565 if ((ret = mbedtls_md5_starts_ret(&mbedtls_md5)) != 0) {
7566 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007567 goto exit;
7568 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007569 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5,
7570 ssl->handshake->randbytes, 64)) != 0) {
7571 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007572 goto exit;
7573 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007574 if ((ret = mbedtls_md5_update_ret(&mbedtls_md5, data, data_len)) != 0) {
7575 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007576 goto exit;
7577 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007578 if ((ret = mbedtls_md5_finish_ret(&mbedtls_md5, output)) != 0) {
7579 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md5_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007580 goto exit;
7581 }
7582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007583 if ((ret = mbedtls_sha1_starts_ret(&mbedtls_sha1)) != 0) {
7584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_starts_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007585 goto exit;
7586 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007587 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1,
7588 ssl->handshake->randbytes, 64)) != 0) {
7589 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007590 goto exit;
7591 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007592 if ((ret = mbedtls_sha1_update_ret(&mbedtls_sha1, data,
7593 data_len)) != 0) {
7594 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_update_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007595 goto exit;
7596 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007597 if ((ret = mbedtls_sha1_finish_ret(&mbedtls_sha1,
7598 output + 16)) != 0) {
7599 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha1_finish_ret", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007600 goto exit;
7601 }
7602
7603exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007604 mbedtls_md5_free(&mbedtls_md5);
7605 mbedtls_sha1_free(&mbedtls_sha1);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007607 if (ret != 0) {
7608 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7609 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7610 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007612 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007613
7614}
7615#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
7616 MBEDTLS_SSL_PROTO_TLS1_1 */
7617
7618#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
7619 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007620
7621#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007622int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7623 unsigned char *hash, size_t *hashlen,
7624 unsigned char *data, size_t data_len,
7625 mbedtls_md_type_t md_alg)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007626{
Andrzej Kurek814feff2019-01-14 04:35:19 -05007627 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +00007628 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007629 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(md_alg);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007631 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007633 if ((status = psa_hash_setup(&hash_operation,
7634 hash_alg)) != PSA_SUCCESS) {
7635 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007636 goto exit;
7637 }
7638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007639 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
7640 64)) != PSA_SUCCESS) {
7641 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007642 goto exit;
7643 }
7644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007645 if ((status = psa_hash_update(&hash_operation,
7646 data, data_len)) != PSA_SUCCESS) {
7647 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007648 goto exit;
7649 }
7650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007651 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
7652 hashlen)) != PSA_SUCCESS) {
7653 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
7654 goto exit;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007655 }
7656
7657exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007658 if (status != PSA_SUCCESS) {
7659 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7660 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
David Horstmann09072662025-03-12 12:03:13 +00007661
7662 return mbedtls_ssl_md_error_from_psa(status);
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007663 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007664 return 0;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007665}
7666
7667#else
7668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007669int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
7670 unsigned char *hash, size_t *hashlen,
7671 unsigned char *data, size_t data_len,
7672 mbedtls_md_type_t md_alg)
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007673{
7674 int ret = 0;
7675 mbedtls_md_context_t ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007676 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
7677 *hashlen = mbedtls_md_get_size(md_info);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007679 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Andrzej Kurek814feff2019-01-14 04:35:19 -05007680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007681 mbedtls_md_init(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007682
7683 /*
7684 * digitally-signed struct {
7685 * opaque client_random[32];
7686 * opaque server_random[32];
7687 * ServerDHParams params;
7688 * };
7689 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007690 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
7691 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007692 goto exit;
7693 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007694 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
7695 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007696 goto exit;
7697 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007698 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
7699 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007700 goto exit;
7701 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007702 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
7703 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007704 goto exit;
7705 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007706 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
7707 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007708 goto exit;
7709 }
7710
7711exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007712 mbedtls_md_free(&ctx);
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007714 if (ret != 0) {
7715 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7716 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
7717 }
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007719 return ret;
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007720}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -05007721#endif /* MBEDTLS_USE_PSA_CRYPTO */
7722
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +01007723#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
7724 MBEDTLS_SSL_PROTO_TLS1_2 */
7725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#endif /* MBEDTLS_SSL_TLS_C */